/* https://yukicoder.me/problems/no/697 */ #include using namespace std; using ll=long long; #define rep2(i, a, n) for(int i = (a); i < (n); i++) #define rep(i, n) rep2(i,0,n) int main(){ cin.tie(nullptr);ios_base::sync_with_stdio(false); int h,w; cin>>h>>w; vector> hoge(h,vector(w)); rep(i,h)rep(j,w)cin>>hoge[i][j]; //移動4方向のベクトル int dx[4] = { 0, 1, 0, -1 }, dy[4] = { -1, 0, 1, 0 }; int ans=0,x,y,xx,yy; rep(sy,h){ rep(sx,w){ if(hoge[sy][sx]==1){ ans++; queue> que; que.push({sx,sy}); hoge[sy][sx]=0; while(que.size()){ x=que.front().first; y=que.front().second; que.pop(); rep(i,4){ xx=x+dx[i]; yy=y+dy[i]; if (0 <= xx and xx < w and 0 <= yy and yy < h) if (hoge[yy][xx] == 1) { que.push({xx,yy}); hoge[yy][xx] = 0; } } } } } } cout<