#include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; const int MAX_H = 3000; const int MAX_W = 3000; const int DY[4] = {-1, 0, 1, 0}; const int DX[4] = {0, 1, 0, -1}; int H, W; int A[MAX_H][MAX_W]; void f(int y, int x) { queue que; que.push(y * W + x); while (!que.empty()) { int z = que.front(); int y = z / W; int x = z % W; A[y][x] = 0; que.pop(); for (int i = 0; i < 4; ++i) { int ny = y + DY[i]; int nx = x + DX[i]; int nz = ny * W + nx; if (ny < 0 || nx < 0 || H <= ny || W <= nx) continue; if (A[ny][nx] == 0) continue; que.push(nz); } } } int main() { cin >> H >> W; fprintf(stderr, "H: %d, W: %d\n", H, W); for (int y = 0; y < H; ++y) { for (int x = 0; x < W; ++x) { cin >> A[y][x]; } } int ans = 0; for (int y = 0; y < H; ++y) { for (int x = 0; x < W; ++x) { if (A[y][x] == 0) continue; ++ans; f(y, x); } } cout << ans << endl; return 0; }