#include #define rep(i,n) for(int i = 0; i < (n); ++i) #define srep(i,s,t) for (int i = s; i < t; ++i) #define drep(i,n) for(int i = (n)-1; i >= 0; --i) using namespace std; typedef long long int ll; typedef pair P; #define yn {puts("Yes");}else{puts("No");} #define MAX_N 200005 int main() { ios_base::sync_with_stdio(false); cin.tie(0); int h, w; cin >> h >> w; int a[h+2][w+2]; rep(i,h+2){ rep(j,w+2){ if(i == 0 || i == h + 1 || j == 0 || j == w + 1){ a[i][j] = 0; }else{ cin >> a[i][j]; } } } int ans = 2; int dx[4] = {-1, 1, 0, 0}; int dy[4] = {0, 0, -1, 1}; rep(i,h+2){ rep(j,w+2){ if(a[i][j] == 1){ queue

que; a[i][j] = ans; que.push(P(i, j)); while(que.size()){ int x = que.front().first; int y = que.front().second; que.pop(); rep(k,4){ if(a[x+dx[k]][y+dy[k]] == 1){ a[x+dx[k]][y+dy[k]] = ans; que.push(P(x+dx[k], y+dy[k])); } } } ans++; } } } ans -= 2; cout << ans << endl; return 0; }