#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; 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){ //DEB; //SHOW(i,j); ++ans; queue q; q.push(tii{i,j}); while(!q.empty()){ int a,b; tie(a,b) = q.front(); q.pop(); in[a][b] = 0; rep(x,4){ int ni = a + vec[x]; int nj = b + vec[x+1]; //SHOW(ni,nj); //cerr << in[ni][nj] << endl; if(ni < 0 || ni >= h || nj < 0 || nj >= w) continue; if(in[ni][nj] == 1)q.push(tii{ni,nj}); } } } cout << ans << endl; }