#include #include #include int main() { int H, W; std::cin >> H >> W; std::vector> A(H, std::vector(W)); for (int h = 0; h < H; ++h) { for (int w = 0; w < W; ++w) { int a; std::cin >> a; A.at(h).at(w) = bool(a); } } const std::vector> dhw = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; std::vector> nonvisited(H, std::vector(W, true)); int ans = 0; for (int h0 = 0; h0 < H; ++h0) { for (int w0 = 0; w0 < W; ++w0) { if (A.at(h0).at(w0) && nonvisited.at(h0).at(w0)) { ans++; std::stack> stk; stk.emplace(h0, w0); while (!stk.empty()) { const auto [h, w] = stk.top(); stk.pop(); for (const auto &[dh, dw]: dhw) { const int hdh = h + dh; const int wdw = w + dw; if (0 <= hdh && hdh < H && 0 <= wdw && wdw < W && A.at(hdh).at(wdw) && nonvisited.at(hdh).at(wdw)) { stk.emplace(hdh, wdw); } } } } } } std::cout << ans << std::endl; }