#include using namespace std; typedef long long ll; typedef pair lpair; const ll MOD = 1e9 + 7; const ll INF = 1e18; #define rep(i,m,n) for(ll i = (m); i < (n); i++) #define rrep(i,m,n) for(ll i = (m); i >= (n); i--) #define print(x) cout << (x) << endl; #define print2(x,y) cout << (x) << " " << (y) << endl; #define printa(x,n) for(ll i = 0; i < n; i++){ cout << (x[i]) << " ";} cout<> H >> W; rep(i,0,H){ rep(j,0,W){ cin >> A[i][j]; } } ll ans = 0; ll dx[4] = {0,0,1,-1}; ll dy[4] = {1,-1,0,0}; rep(h,0,H){ rep(w,0,W){ if(check[h][w] == 1 || A[h][w] == 0) continue; // print2(h,w); ans++; queue q; q.push(make_pair(h,w)); while(!q.empty()){ lpair l1 = q.front(); q.pop(); if(check[l1.first][l1.second]) continue; check[l1.first][l1.second] = 1; rep(k,0,4){ ll hy = l1.first + dy[k]; ll wx = l1.second + dx[k]; if(hy >= 0 && hy < H && wx >= 0 && wx < W){ if(check[hy][wx] == 0 && A[hy][wx] == 1){ q.push(make_pair(hy, wx)); } } } } } } print(ans); }