#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } const long long MAX = 5100000; const long long INF = 1LL << 60; const long long mod = 1000000007LL; //const long long mod = 998244353LL; using namespace std; typedef unsigned long long ull; typedef long long ll; ll H, W; vector> vi; ll dh[4] = { 1,0,-1,0 }; ll dw[4] = { 0,1,0,-1 }; void dfs(ll h, ll w) { vi[h][w] = 0; for (ll i = 0; i < 4; i++) { ll nh = h + dh[i]; ll nw = w + dw[i]; if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue; if (vi[nh][nw]) { dfs(nh, nw); vi[nh][nw]=false; } } } int main() { /* cin.tie(nullptr); ios::sync_with_stdio(false); */ scanf("%lld %lld", &H, &W); vi = vector>(H, vector(W)); for (ll i = 0; i < H; i++) for (ll j = 0; j < W; j++) { int x; scanf("%d", &x); vi[i][j] = x; } ll res = 0; for (ll i = 0; i < H; i++) { for (ll j = 0; j < W; j++) { if (vi[i][j]) { dfs(i, j); res++; } } } cout << res << endl; return 0; }