/* 思路:
1、DFS和BFS都可以
实现深度遍历递归形式和栈形式
*/
/* 注意点:
m(rows)*n(columns): 1<=m,n<=100;
end: m=0
@:oil *:no oil
*/
#include <cstdio>
#include <cstring>
const int MAX = 110;
int m, n;
char grid[MAX][MAX];
int visit[MAX][MAX];
int dir_x[] = {-1, -1, -1, 0, 1, 1, 1, 0};
int dir_y[] = {-1, 0, 1, 1, 1, 0, -1, -1};
const int DIR = sizeof(dir_x) / sizeof(dir_x[0]);
void dfs(int x, int y) {
visit[x][y] = 1;
for(int i=0; i<DIR; i++) {
int t_x = x + dir_x[i];
int t_y = y + dir_y[i];
if(grid[t_x][t_y] != '@' || visit[t_x][t_y] || x<0 || y<0 || x>=m || y>=n) continue;
visit[t_x][t_y] = 1;
dfs(t_x, t_y);
}
}
//dfs栈
int st_x[MAX*MAX];
int st_y[MAX*MAX];
void dfs_st(int x, int y) {
int top = 0;
visit[x][y] = 1;
st_x[top] = x; st_y[top++] = y;
while(top > 0) {
x = st_x[top-1]; y = st_y[top-1];
int found = false;
for(int i=0; i<DIR; i++) {
int t_x = x + dir_x[i];
int t_y = y + dir_y[i];
if(grid[t_x][t_y] != '@' || visit[t_x][t_y] || x<0 || y<0 || x>=m || y>=n) continue;
visit[t_x][t_y] = 1;
st_x[top] = t_x; st_y[top++] = t_y;
found = true;
break;
}
if(!found) top--;
}
}
int main ()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
while(scanf("%d%d\n", &m, &n) == 2 &&
m!=0) {
for(int i=0; i<m; i++) gets(grid[i]);
memset(visit, 0, sizeof(visit));
int c = 0;
for(int i=0; i<m; i++) {
for(int j=0; j<n; j++) {
if(grid[i][j] == '@' && !visit[i][j]) {
dfs_st(i, j);//dfs(i, j);
c++;
}
}
}
printf("%d\n", c);
}
return 0;
}
08-24