#include using namespace std; int h, w; const int MAX = 3010; int A[MAX][MAX]; int dx[4] = {-1, 1, 0, 0}; int dy[4] = {0, 0, -1, 1}; bool seen[MAX][MAX]; int main(){ cin.tie(0); ios::sync_with_stdio(false); cin >> h >> w; for(int i = 0; i < h; i++){ for(int j = 0; j < w; j++){ cin >> A[i][j]; } } int ans = 0; for(int i = 0; i < h; i++){ for(int j = 0; j < w; j++){ if(A[i][j] == 1 and !seen[i][j]){ ans++; queue> q; seen[i][j] = true; q.push({i, j}); while(!q.empty()){ pair p = q.front(); q.pop(); for(int k = 0; k < 4; k++){ int nx = dx[k] + p.first; int ny = dy[k] + p.second; if(nx < 0 or nx >= h or ny < 0 or ny >= w) continue; if(seen[nx][ny] or A[nx][ny] == 0) continue; seen[nx][ny] = true; q.push({nx, ny}); } } } } } cout << ans << endl; return 0; }