#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define ll long long #define ld long double #define EPS 0.0000000001 #define INF 1e9 #define LINF (ll)INF*INF #define MOD 1000000007 #define rep(i,n) for(int i=0;i<(n);i++) #define loop(i,a,n) for(int i=a;i<(n);i++) #define all(in) in.begin(),in.end() #define shosu(x) fixed< vi; typedef vector vs; typedef pair pii; typedef vector vp; int gcd(int a, int b){ if(b==0) return a; return gcd(b,a%b); } int lcm(int a, int b){ return a/gcd(a,b)*b; } int dx[] = {0, 1, 0, -1}; int dy[] = {1, 0, -1, 0}; vp v; vector a,used; queue q; signed main(void) { int h,w; cin >> h >> w; a = used = vector (h,vi(w)); rep(i,h)rep(j,w)cin >> a[i][j]; int ans = 0; rep(i,h)rep(j,w)if(a[i][j]){ q.push(pii(i,j)); used[i][j]++; while(q.size()){ int x = q.front().first; int y = q.front().second; v.push_back(pii(x,y)); q.pop(); rep(k,4){ int nx = x + dx[k]; int ny = y + dy[k]; if(nx < 0 || nx >= h || ny < 0 || ny >= w)continue; if(a[nx][ny] == 0 || used[nx][ny])continue; used[nx][ny]++; q.push(pii(nx,ny)); } } rep(k,v.size()){ a[v[k].first][v[k].second]--; used[v[k].first][v[k].second]--; } ans++; v.clear(); } cout << ans << endl; }