#include "bits/stdc++.h" #define ALL(obj) (obj).begin(),(obj).end() #define RALL(obj) (obj).rbegin(),(obj).rend() #define REP(i, n) for(int i = 0; i < (int)(n); i++) #define REPR(i, n) for(int i = (int)(n); i >= 0; i--) #define FOR(i,n,m) for(int i = (int)(n); i < int(m); i++) using namespace std; typedef long long ll; const int MOD = 1e9 + 7; const int INF = MOD - 1; const ll LLINF = 4e18; void dfs(int i, int j, const vector>& F, vector>& used) { int h = F.size(); int w = F[0].size(); if (F[i][j] == 1 && !used[i][j]) { used[i][j] = true; if (i < h - 1) dfs(i + 1, j, F, used); if (j < w - 1) dfs(i, j + 1, F, used); if (i > 0) dfs(i - 1, j, F, used); if (j > 0) dfs(i, j - 1, F, used); } } int main() { int h, w; cin >> h >> w; vector> F(h, vector(w)); vector> used(h, vector(w, false)); REP(i, h) { REP(j, w) { cin >> F[i][j]; } } int ans = 0; REP(i, h) { REP(j, w) { if (F[i][j] == 1 && !used[i][j]) { dfs(i, j,F,used); ans++; } } } cout << ans << endl; getchar(); getchar(); }