#define _USE_MATH_DEFINES #include "bits/stdc++.h" using namespace std; #define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i)) #define rep(i,j) FOR(i,0,j) #define each(x,y) for(auto &(x):(y)) #define mp make_pair #define MT make_tuple #define all(x) (x).begin(),(x).end() #define debug(x) cout<<#x<<": "<<(x)< >(b,vector(c,d)) #define vvv(a,b,c,d,e) vector > >(b,vv(a,c,d,e)) using ll = long long; using pii = pair; using vi = vector; using vll = vector; const int D[] = { 0,-1,0,1,0 }; bool A[3001][3001]; void dfs(int y, int x) { A[y][x] = false; rep(i, 4) { int ny = y + D[i], nx = x + D[i + 1]; if (A[ny][nx])dfs(ny, nx); } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(20); int H, W, re = 0; cin >> H >> W; rep(y, H)rep(x, W)cin >> A[y + 1][x + 1]; rep(y, H)rep(x, W)if (A[y + 1][x + 1]) { re++; dfs(y + 1, x + 1); } cout << re << endl; }