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.

52 lines
1.1 KiB

#include <bits/stdc++.h>
using namespace std;
const int N = 30;
struct Node {
int x;
int y;
};
char g[N][N];
int n, m;
int dx[] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
bool st[N][N];
int cnt;
void bfs(int x, int y) {
queue<Node> q;
q.push({x, y});
while (q.size()) {
Node t = q.front();
q.pop();
cnt++;
for (int i = 0; i < 4; i++) {
int tx = t.x + dx[i], ty = t.y + dy[i];
if (tx < 0 || tx >= n || ty < 0 || ty >= m) continue;
if (st[tx][ty]) continue;
if (g[tx][ty] != '.') continue;
st[tx][ty] = true;
q.push({tx, ty});
}
}
}
int main() {
while (cin >> m >> n, n || m) {
memset(st, 0, sizeof st);
cnt = 0;
int x, y;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> g[i][j];
if (g[i][j] == '@') x = i, y = j;
}
}
bfs(x, y);
printf("%d\n", cnt);
}
return 0;
}