#include #include #include #include #include using namespace std; //frog int A[3030][3030]; int dx[4] = { 0,1,0,-1 }; int dy[4] = { 1,0,-1,0 }; int main() { int h, w; cin >> h >> w; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { scanf("%d", &A[i][j]); } } int ans = 0, nx, ny; queue> que; pair P; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (A[i][j] == 1) { ans++; A[i][j] = 0; que.push(make_pair(i, j)); while (!que.empty()) { P = que.front(); que.pop(); for (int k = 0; k < 4; k++){ nx = P.first + dx[k]; ny = P.second + dy[k]; if (0 <= nx && nx < h && 0 <= ny && ny < w && A[nx][ny] == 1) { A[nx][ny] = 0; que.push(make_pair(nx, ny)); } } } } } } cout << ans << endl; return 0; }