#include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef unsigned long long ull; const ll MOD = 1000000007; #define rep(i,n) for(int i=0;i=0;i--) #define all(x) (x).begin(),(x).end() int H, W; bool IsInRange(int w, int h) { return 0 <= w && w <= W - 1 && 0 <= h && h <= H - 1; } int main() { cin >> H >> W; vector> A(W, vector(H, 0)); rep(h, H) rep(w, W) cin >> A[w][h]; ll count = 0; rep(h, H) { rep(w, W) { if (A[w][h] != 1) continue; count++; queue> q; q.emplace(w, h); //幅優先探索 while (!q.empty()) { auto p = q.front(); q.pop(); auto x = p.first; auto y = p.second; if (A[x][y] != 1) continue; A[x][y] = -1; if (IsInRange(x - 1, y))q.emplace(x - 1, y); if (IsInRange(x + 1, y))q.emplace(x + 1, y); if (IsInRange(x, y - 1))q.emplace(x, y - 1); if (IsInRange(x, y + 1))q.emplace(x, y + 1); } } } cout << count << endl; return 0; }