#include using namespace std; vector dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1}; int h, w; void bfs(int i, int j, vector> &a) { queue> q; q.push(make_pair(i, j)); a.at(i).at(j) = false; while (!q.empty()) { pair p = q.front(); q.pop(); for (int k = 0; k < 4; k++) { if (p.first + dx[k] < 0 || p.first + dx[k] >= h || p.second + dy[k] < 0 || p.second + dy[k] >= w) continue; if (a.at(p.first + dx[k]).at(p.second + dy[k])) { a.at(p.first + dx[k]).at(p.second + dy[k]) = false; q.push(make_pair(p.first + dx[k], p.second + dy[k])); } } } return; } int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> h >> w; vector> a(h, vector(w)); int x; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> x; a.at(i).at(j) = (x ? true : false); } } int ans = 0; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (a.at(i).at(j)) { bfs(i, j, a); ans++; } } } cout << ans << '\n'; }