#include using namespace std; using ll = long long; template using vt = vector; template using vvt = vector>; template using ttt = tuple; using tii = tuple; using vi = vector; #define rep(i,n) for(int i=0;i<(int)(n);i++) #define pb push_back #define mt make_tuple #define ALL(a) (a).begin(),(a).end() #define FST first #define SEC second #define DEB cerr<<"!"<0){if((n&1)==1)r=r*x%m;x=x*x%m;n>>=1;}return r%m;} inline ll lcm(ll d1, ll d2){return d1 / __gcd(d1, d2) * d2;} /*Coding Space*/ bool in[3000][3000]={}; int vec[] = {1,0,-1,0,1}; int h,w; void dfs(int i, int j){ in[i][j] = 2; rep(dx,4){ int ni = i + vec[dx]; int nj = j + vec[dx+1]; if(ni < 0 || ni >= h || nj < 0 || nj >= w) continue; if(in[ni][nj] == 1) dfs(ni, nj); } } int main(){ cin >> h >> w; rep(i,h)rep(j,w) cin >> in[i][j]; int ans = 0; rep(i,h)rep(j,w)if(in[i][j] == 1){ ++ans; dfs(i,j); cerr << i << " " << j << endl; } cout << ans << endl; }