#include using namespace std; constexpr int LAND = 10'000'000; struct UnionFind{ vector data; UnionFind(int size) : data(size, -1){} void unite(int x, int y){ x = root(x); y = root(y); if (x != y){ if (data[y] < data[x]) swap(x, y); data[x] += data[y]; data[y] = x; } } int root(int x) {return data[x] < 0 ? x : data[x] = root(data[x]);} int size(int x) {return -data[root(x)];} }; int main() { cin.tie(0); ios::sync_with_stdio(false); int H, W; cin >> H >> W; UnionFind uf(H * W); vector pre(W); vector cur(W); for (int h = 0 ; h < H; ++h){ int hW = h * W; for (auto & c : cur) cin >> c; for (int w = 0; w < W; ++w){ if (cur[w] == '1'){ if (w > 0 and cur[w - 1] == '1') uf.unite(hW + w, hW + w - 1); if (h > 0 and pre[w] == '1') uf.unite(hW + w, hW + w - W); }else{ uf.data[hW + w] = LAND; } } swap(pre, cur); } // count the number of ponds int ans = 0; for (auto v : uf.data) if (v < 0) ++ans; cout << ans << endl; return 0; }