#include #include #include #include using namespace std; int main() { int h, w; cin >> h >> w; vector> grid(h); for (int i = 0; i < h; ++i) { grid[i].resize(w); for (int j = 0; j < w; ++j) { cin >> grid[i][j]; } } set> sensor; for (int j = 0; j < h; ++j) { for (int i = 0; i < w; ++i) { if (grid[j][i] == "1") { sensor.insert({i, j}); } } } int count = 0; set> visited; while (!sensor.empty()) { queue> q; q.push(*sensor.begin()); sensor.erase(sensor.begin()); while (!q.empty()) { int x = q.front().first; int y = q.front().second; q.pop(); if (sensor.count({x,y})){ sensor.erase({x,y}); } int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; for (int i = 0; i < 4; ++i) { int nx = x + dx[i]; int ny = y + dy[i]; if (nx < 0 || nx >= w || ny < 0 || ny >= h) continue; if (grid[ny][nx] == "0") continue; if(visited.count({nx, ny}) == 0 && grid[ny][nx] == "1"){ visited.insert({nx, ny}); q.push({nx, ny}); } } } count++; } cout << count << endl; return 0; }