#include "bits/stdc++.h" #define ALL(obj) (obj).begin(),(obj).end() #define RALL(obj) (obj).rbegin(),(obj).rend() #define REP(i, n) for(int i = 0; i < (int)(n); i++) #define REPR(i, n) for(int i = (int)(n); i >= 0; i--) #define FOR(i,n,m) for(int i = (int)(n); i < int(m); i++) using namespace std; typedef long long ll; const int MOD = 1e9 + 7; const int INF = MOD - 1; const ll LLINF = 4e18; int main() { int h, w; cin >> h >> w; vector> F(h, vector(w)); REP(i, h) { REP(j, w) { cin >> F[i][j]; } } int ans = 0; REP(i, h) { REP(j, w) { if (F[i][j] == 1) { stack> sta; sta.push({i,j}); while (!sta.empty()) { pair p = sta.top(); sta.pop(); int x = p.first, y = p.second; F[x][y] = 0; if (x < h - 1 && F[x + 1][y] == 1) sta.push({x + 1, y}); if (y < w - 1 && F[x][y + 1] == 1) sta.push({x, y + 1}); if (x > 0 && F[x - 1][y] == 1) sta.push({x - 1, y}); if (y > 0 && F[x][y - 1] == 1) sta.push({x, y - 1}); } ans++; } } } cout << ans << endl; getchar(); getchar(); }