#include using namespace std; #define REP(i,n) for(int i=0; i<(int)(n); i++) #define FOR(i,b,e) for (int i=(int)(b); i<(int)(e); i++) #define ALL(x) (x).begin(), (x).end() const double PI = acos(-1); int h, w; int bd[3000][3000]; int dy[] = {0, 1, 0, -1}; int dx[] = {1, 0, -1, 0}; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> h >> w; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) cin >> bd[i][j]; int cnt = 0; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (bd[i][j]) { ++cnt; queue > q; q.emplace(i, j); bd[i][j] = 0; while (!q.empty()) { int y, x; tie(y, x) = q.front(); q.pop(); for (int k = 0; k < 4; k++) { int yt = y + dy[k]; int xt = x + dx[k]; if (yt < 0 || yt >= h) continue; if (xt < 0 || xt >= w) continue; if (bd[yt][xt]) { bd[yt][xt] = 0; q.emplace(yt, xt); } } } } } } cout << cnt << endl; return 0; }