样例过了,却全WAqwq
#include <bits/stdc++.h>
#define init ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define MX 500
using namespace std;
typedef long long ll;
ll n, m, q[MX*MX+1], q1[MX*MX+1], a[MX+1][MX+1], v[MX+1][MX+1], h, t, ans;
ll dx[4] = {1, 0, 0, -1}, dy[4] = {0, 1, -1, 0};
bool flag;
void __init__() {
init;
cin >> n >> m;
for (ll i = 1; i <= n; i++) {
for (ll j = 1; j <= m; j++) {
char x;
cin >> x;
if (x == '0')
a[i][j] = 0;
else
a[i][j] = 1;
}
}
return ;
}
void bfs(int x, int y) {
t++;
q[t] = x;
q1[t] = y;
v[x][y] = 1;
if(!a[x][y]) a[x][y] = 2;
while (h < t) {
h++;
for (ll i = 0; i < 4; i++) {
ll xa = q[h]+dx[i], ya = q1[h]+dy[i];
if (xa>-1 && ya>-1 && xa<=n+1 && ya<=m+1 && !v[xa][ya] && !a[xa][ya]) {
t++;
q[t] = xa;
q1[t] = ya;
v[xa][ya] = 1;
a[xa][ya] = 2;
bfs(xa, ya);
}
}
}
}
int main() {
__init__();
bfs(0, 0);
for (ll i = 1; i <= n; i++)
for (ll j = 1; j <= m; j++)
if (!a[i][j]) ans++;
cout << ans;
return 0;
}