DFS
import java.io.*;
class Main{
static BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
static int N = 25, row, col;
static char[][] a = new char[N][N];
static int[][] dir = new int[][]{{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
public static void main(String[] args) throws Exception{
while(true){
String[] ss = read.readLine().split(" ");
row = Integer.valueOf(ss[1]);
col = Integer.valueOf(ss[0]);
if(row == 0 && col == 0) break;
boolean[][] st = new boolean[row][col];
int x = 0, y = 0;
for(int i = 0; i < row; i++){
String s = read.readLine();
for(int j = 0; j < col; j++){
a[i][j] = s.charAt(j);
if(a[i][j] == '@'){
x = i; y = j;
}
}
}
System.out.println(dfs(x, y, st));
}
}
public static int dfs(int x, int y, boolean[][] st){
int res = 1;
st[x][y] = true;
for(int i = 0; i < 4; i++){
int nx = x + dir[i][0], ny = y + dir[i][1];
if(nx < 0 || nx >= row || ny < 0 || ny >= col) continue;
if(st[nx][ny] || a[nx][ny] == '#') continue;
res += dfs(nx, ny, st);
}
return res;
}
}
BFS
import java.io.*;
import java.util.*;
class Main{
static BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
static int N = 25;
static char[][] a = new char[N][N];
static int[][] dir = new int[][]{{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
public static void main(String[] args) throws Exception{
while(true){
String[] ss = read.readLine().split(" ");
int row = Integer.valueOf(ss[1]);
int col = Integer.valueOf(ss[0]);
if(row == 0 && col == 0) break;
boolean[][] st = new boolean[row][col];
Queue<int[]> q = new LinkedList();
int x1 = 0, y1 = 0;
for(int i = 0; i < row; i++){
String s = read.readLine();
for(int j = 0; j < col; j++){
a[i][j] = s.charAt(j);
if(a[i][j] == '@'){
x1 = i; y1 = j;
}
}
}
int cnt = 0;
st[x1][y1] = true;
q.offer(new int[]{x1, y1});
while(!q.isEmpty()){
int[] poll = q.poll();
cnt++;
for(int i = 0; i < 4; i++){
int nx = dir[i][0] + poll[0];
int ny = dir[i][1] + poll[1];
if(nx < 0 || nx >= row || ny < 0 || ny >= col) continue;
if(st[nx][ny] || a[nx][ny] == '#') continue;
st[nx][ny] = true;
q.offer(new int[]{nx, ny});
}
}
System.out.println(cnt);
}
}
}
bfs没有注释……——……