#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long unsigned int ll; #define EPS (1e-7) #define INF (1e9) #define PI (acos(-1)) //入力 int h, w; int a[3000][3001]; int reached[3000][3001]; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; void dfs(int y, int x) { a[y][x] = 0; reached[y][x] = 1; for (int i = 0; i < 4; i++) { int nx = x + dx[i]; int ny = y + dy[i]; if (0 <= nx && nx < w && 0 <= ny && ny < h && a[ny][nx] == 1 && !(reached[ny][nx])) dfs(ny, nx); } return; } void solve() { int ans = 0; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (a[i][j] == 1) { dfs(i, j); ans++; } } } printf("%d\n", ans); } int main() { cin >> h >> w; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> a[i][j]; } } solve(); }