#include "bits/stdc++.h" using namespace std; using ll = long long; using ld = long double; const int INF = (1 << 30) - 1; const ll INF64 = ((ll)1 << 62) - 1; const double PI = 3.1415926535897932384626433832795; const int dx[] = { 0, 1, 0, -1 }; const int dy[] = { -1, 0, 1, 0 }; int h, w; vector> a; void bfs(int sy, int sx) { queue> que; que.emplace(sy, sx); a[sy][sx] = 0; while (!que.empty()) { pair now = que.front(); que.pop(); for (int i = 0; i < 4; i++) { int ny = now.first + dy[i]; int nx = now.second + dx[i]; if (nx < 0 || w <= nx || ny < 0 || h <= ny || a[ny][nx] == 0) { continue; } a[ny][nx] = 0; que.emplace(ny, nx); } } } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> h >> w; a.resize(h, vector(w)); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> a[i][j]; } } int ans = 0; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (a[i][j] != 1) { continue; } bfs(i, j); ans++; } } cout << ans << endl; return 0; }