#include using namespace std; using ll = long long; using P = pair; const int dh[] = {1, -1, 0, 0}; const int dw[] = {0, 0, 1, -1}; bool f[3005][3005]; void dfs(int ch, int cw) { stack

s; s.emplace(ch, cw); while (!s.empty()) { P p = s.top(); s.pop(); f[p.first][p.second] = false; for (int i = 0; i < 4; i++) { int nh = p.first + dh[i], nw = p.second + dw[i]; if (!f[nh][nw]) continue; dfs(nh, nw); } } } int main() { cin.tie(0); ios::sync_with_stdio(false); int H, W; cin >> H >> W; for (int i = 1; i <= H; i++) { for (int j = 1; j <= W; j++) { cin >> f[i][j]; } } int ans = 0; for (int i = 1; i <= H; i++) { for (int j = 1; j <= W; j++) { if (f[i][j]) { dfs(i, j); ans++; } } } cout << ans << endl; return 0; }