#include #include #include using namespace std; int main() { constexpr int d[]={-1,0,1,0,-1}; int H, W;cin >> H >> W; vector> f(H, vector(W)); for(int i = 0; i < H; i++){ for(int j = 0; j < W; j++){cin >> f[i][j];} } using P = pair; auto in = [&](const int y, const int x){return y >= 0 and y < H and x >= 0 and x < W and f[y][x]==1;}; int ans = 0; for(int i = 0; i < H; i++){ for(int j = 0; j < W; j++){ if(f[i][j]==0){continue;} queue

q; ans++,f[i][j]=0,q.push({i,j}); while(not q.empty()){ const int y = q.front().first,x=q.front().second; q.pop(); for(int k = 0; k < 4; k++){ const int ny = y+d[k],nx = x+d[k+1]; if(in(ny,nx)){f[ny][nx]=0,q.push({ny,nx});} } } } } cout << ans << endl; return 0; }