#include #include #include #include using namespace std; int H, W; int dxy[5] = {-1, 0, 1, 0, -1}; vector> v(3010); int ans; void dfs(int y, int x) { if (v[y][x] == 0) return; v[y][x] = 0; for (int i = 0; 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] != 0) { 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; v[i].push_back(a); } } 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; }