#include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; #define MOD 1000000007 int main() { int h, w; cin >> h >> w; vector > a(h, vector(w)); for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { cin >> a[i][j]; } } queue > que; int ans = 0; for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { if (a[i][j] == 1) { ans++; que.push(make_pair(i, j)); a[i][j] = 0; while (!que.empty()) { int y = que.front().first; int x = que.front().second; que.pop(); for (int c = -1; c <= 1; ++c) { for (int d = -1; d <= 1; ++d) { if (c * c + d * d == 1) { int ny = y + c, nx = x + d; if (0 <= ny && ny < h && 0 <= nx && nx < w) { if (a[ny][nx] == 1) { que.push(make_pair(ny, nx)); a[ny][nx] = 0; } } } } } } } } } cout << ans << endl; return 0; }