#include #include #include #include using namespace std; int H, W; int dxy[5] = {-1, 0, 1, 0, -1}; bool v[3005][3005]; int ans; void dfs(int y, int x) { if (v[y][x] == false) return; v[y][x] = 0; for (int i = 1; i < 5; ++i) { int ny = y + dxy[i], nx = x + dxy[i - 1]; if (ny >= 0 && nx >= 0 && ny < H && nx < W && v[ny][nx] != false) { dfs(ny, nx); } } return; } int main(void) { cin >> H >> W; for (int i = 0; i < H; ++i) { for (int j = 0; j < W; ++j) { int a; cin >> a; if (a == 1) v[i][j] = true; else v[i][j] = false; } } for (int i = 0; i < H; ++i) { for (int j = 0; j < W; ++j) { if (v[i][j]) { dfs(i, j); ans++; } } } cout << ans << endl; return 0; }