#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 dw[4] = {0,0,1,-1}; ll dh[4] = {1,-1,0,0}; rep(i,0,H){ rep(j,0,W){ if(check[i][j] == 1 || A[i][j] == 0) continue; ans++; queue que; que.push(make_pair(i,j)); while(not que.empty()){ ll h = que.front().first; ll w = que.front().second; que.pop(); if(check[h][w] == 1) continue; check[h][w] = 1; rep(k,0,4){ ll hh = h + dh[k]; ll ww = w + dw[k]; if(hh >= 0 && hh < H && ww >= 0 && ww < W){ if(check[hh][ww] == 0 && A[hh][ww] == 1){ que.push(make_pair(hh, ww)); } } } } } } print(ans); }