You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1009 B

#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
typedef pair<int, int> PII;
const int N = 110;
int g[N][N];
int n, m;
int dx[] = {-1, 0, 1, 0}; //上右下左
int dy[] = {0, 1, 0, -1}; //上右下左
int ans;
queue<PII> q;
void bfs() {
while (q.size()) {
auto u = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
int tx = u.x + dx[i], ty = u.y + dy[i];
if (g[tx][ty]) {
g[tx][ty] = 0;
q.push({tx, ty});
}
}
}
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> g[i][j];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (g[i][j]) {
ans++;
g[i][j] = 0;
q.push({i, j});
bfs();
}
}
}
printf("%d", ans);
return 0;
}