#include using namespace std; #define rep(i, j, n) for(int i=j;i pi; template using vt = vector; template using vvt = vector>; i64 gcd(i64 n, i64 m) {return (m == 0? n : gcd(m, n % m));} i64 lcm(i64 n, i64 m) {return (n / gcd(n, m) * m);} int dx[] = {0, 1, 0, -1}; int dy[] = {1, 0, -1, 0}; int main() { cin.tie(0); ios::sync_with_stdio(false); int h, w; cin >> h >> w; vvt a(h, vt(w)); rep(i, 0, h) rep(j, 0, w) cin >> a[i][j]; int ans = 0; rep(sy, 0, h) { rep(sx, 0, w) { if(!a[sy][sx]) continue; ans++; queue que; que.push({sy, sx}); a[sy][sx] = 0; while(!que.empty()) { int y = que.front().first, x = que.front().second; que.pop(); rep(i, 0, 4) { int ny = y + dy[i], nx = x + dx[i]; if(nx < 0 || nx >= w || ny < 0 || ny >= h) continue; if(a[ny][nx]) { que.push({ny, nx}); a[ny][nx] = 0; } } } } } cout << ans << endl; }