#include #include #include #include using namespace std; vector dy = {1, 0, -1, 0}; vector dx = {0, 1, 0, -1}; int main(){ int H, W; cin >> H >> W; vector> A(H + 2, vector(W + 2, 0)); for (int i = 1; i <= H; i++){ for (int j = 1; j <= W; j++){ cin >> A[i][j]; } } vector> used(H + 2, vector(W + 2, false)); int ans = 0; for (int i = 1; i <= H; i++){ for (int j = 1; j <= W; j++){ if (A[i][j] == 1 && !used[i][j]){ used[i][j] = true; ans++; queue> Q; Q.push(make_pair(i, j)); while (!Q.empty()){ int y = Q.front().first; int x = Q.front().second; Q.pop(); for (int k = 0; k < 4; k++){ int y2 = y + dy[i]; int x2 = x + dx[i]; if (A[y2][x2] == 1 && !used[y2][x2]){ used[y2][x2] = true; Q.push(make_pair(y2, x2)); } } } } } } cout << ans << endl; }