#include using namespace std; int main(){ cin.tie(0); ios::sync_with_stdio(false); int H,W; cin >> H >> W; int A[3000][3000]; int ans = 0; for(int i = 0;i < H;i++){ for(int j = 0;j < W; j++){ cin >> A[i][j]; } } for(int i = 0;i < H;i++){ for(int j = 0;j < W; j++){ if(A[i][j]==0)continue; //0なら処理を飛ばす queue> q; q.push({i,j}); //queueに座標を保存 while(!q.empty()){ int X = q.front().first; int Y = q.front().second; q.pop(); if(A[X][Y] == 0)continue; //0なら処理を飛ばす A[X][Y] = 0; //今の要素を0にする; q.push({X + 1,Y}); //queueに周りの座標を格納 q.push({X - 1,Y}); q.push({X,Y + 1}); q.push({X,Y - 1}); } ans++; } } cout << ans << endl; return 0; }