#include #include #include using namespace std; using P = pair; int dx[] = {1, -1, 0, 0}; int dy[] = {0, 0, 1, -1}; int main(){ int h, w; cin >> h >> w; vector> s(h, vector(w)); for(auto &p: s){ for(auto &pp: p) cin >> pp; } vector> visited(h, vector(w, -1)); int ans = 0; for(int i = 0; i < h; i++){ for(int j = 0; j < w; j++){ if(s[i][j] == 1 && visited[i][j] == -1){ queue

Q; Q.push(make_pair(i, j)); visited[i][j] = ans; while(Q.size()){ auto cur = Q.front(); Q.pop(); for(int k = 0; k < 4; k++){ int nx = cur.first+dx[k]; int ny = cur.second+dy[k]; if(0 <= nx && nx < h && 0 <= ny && ny < w && s[nx][ny] == 1 && visited[nx][ny] == -1){ visited[nx][ny] = ans; Q.push(make_pair(nx, ny)); } } } ans++; } } } cout << ans << endl; return 0; }