#include using namespace std; #define rep(i,n) for(int i = 0; i < (n);i++) #define sz(x) int(x.size()) typedef long long ll; typedef pair P; int dy[] = {1,0,-1,0}; int dx[] = {0,1,0,-1}; int main(){ int h, w; cin >> h >> w; vector> a(h,vector(w)); rep(i,h) rep(j,w) cin >> a[i][j]; auto bfs = [&](int y, int x) { queue

que; que.push({y,x}); a[y][x] = 0; while (!que.empty()) { P p = que.front(); que.pop(); rep(i,4) { int ny = p.first + dy[i], nx = p.second + dx[i]; if (ny < 0 || nx < 0 || ny >= h || nx >= w) continue; if (a[ny][nx] == 0) continue; que.push({ny,nx}); a[ny][nx] = 0; } } }; int res = 0; rep(i,h) rep(j,w) if (a[i][j] == 1) bfs(i,j), res++; cout << res << endl; return 0; }