#include #define rep(i,a,b) for(int i=a;i=b;i--) #define fore(i,a) for(auto &i:a) #define all(x) (x).begin(),(x).end() #pragma GCC optimize ("-O3") using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); } typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60; templatebool chmax(T &a, const T &b) { if (abool chmin(T &a, const T &b) { if (b> H >> W; rep(y, 0, H) rep(x, 0, W) cin >> A[y][x]; int ans = 0; rep(sy, 0, H) rep(sx, 0, W) if (A[sy][sx] == 1) { ans++; queue> que; que.push({ sx, sy }); A[sy][sx] = 0; while (!que.empty()) { int x = que.front().first; int y = que.front().second; que.pop(); rep(i, 0, 4) { int xx = x + dx[i]; int yy = y + dy[i]; if (0 <= xx and xx < W and 0 <= yy and yy < H) if (A[yy][xx] == 1) { que.push({ xx, yy }); A[yy][xx] = 0; } } } } cout << ans << endl; }