#include using namespace std; typedef long long ll; #define rep(i,n) for(int i=0;i<(n);i++) #define reps(i,f,n) for(int i=(f);i<(n);i++) int h, w; int a[3010][3010]; const int dy[] = {1, 0, -1, 0}, dx[] = {0, 1, 0, -1}; int main(int argc, char const *argv[]) { cin >> h >> w; rep(i, h)rep(j, w) cin >> a[i][j]; int ans = 0; rep(i, h)rep(j, w) { if (a[i][j] == 0) continue; queue> que; que.push(make_pair(i, j)); a[i][j] = 0; ans++; while(!que.empty()) { int y = que.front().first, x = que.front().second; que.pop(); rep(k, 4) { int ny = y + dy[k], nx = x + dx[k]; if (!(0 <= ny && ny < h && 0 <= nx && nx < w)) continue; if (a[ny][nx] == 1) { a[ny][nx] = 0; que.push(make_pair(ny, nx)); } } } } printf("%d\n", ans); return 0; }