#include using namespace std; using ll = long long; using ul = unsigned long; using ull = unsigned long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int h, w; cin >> h >> w; vector > a(h, vector(w)); for (auto&& it : a) { for (auto&& it2 : it) cin >> it2; } int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 }; queue > q; int res{ 0 }; for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { if (a[i][j]) { q.push(make_pair(i, j)); a[i][j] = 0; ++res; } while (!q.empty()) { int x, y; tie(y, x) = q.front(); for (int k = 0; k < 4; ++k) { int xx = x + dx[k]; int yy = y + dy[k]; if (xx < 0 || xx >= w) continue; if (yy < 0 || yy >= h) continue; if (a[yy][xx]) { q.push(make_pair(yy, xx)); a[yy][xx] = 0; } } q.pop(); } } } cout << res << "\n"; return 0; }