fengyun@fengyun-server:~/learn/acm/poj$ cat 1979.cpp
#include<cstdio>
#include<iostream>
#include<string>
#include<algorithm>
#include<iterator>
#include<sstream>//istringstream
#include<cstring>
#include<queue>
using namespace std;
char map[21][21];
char visited[21][21];
int w, h;
int ans;
void dfs(int i, int j)
{
if(i<0||i>=h||j<0||j>=w)
return;
if(map[i][j]=='#')
return;
if(map[i][j]=='.'&&visited[i][j]==0)
{
ans++;
visited[i][j]=1;
dfs(i-1,j);
dfs(i+1,j);
dfs(i,j+1);
dfs(i,j-1);
}
}
int dir[4][2]={
{-1, 0},
{1, 0},
{0, 1},
{0, -1},
};
void bfs(int i, int j)
{
queue<int> q;
q.push(i*w+j);
visited[i][j]=1;
while(!q.empty())
{
//pop
//add neighbors
int ni,nj;
ni=q.front()/w;
nj=q.front()%w;
q.pop();
ans++;
for(int k=0;k<4;k++)
{
int row=ni+dir[k][0];
int col=nj+dir[k][1];
if(row>=0&&row<h&&col>=0&&col<w&&map[row][col]=='.'&&visited[row][col]==0)
{
visited[row][col]=1;
q.push(row*w+col);
}
}
}
}
int main()
{
while(cin>>w>>h && (w||h))
{
int x, y;
memset(visited, 0, sizeof(visited));
ans=0;
for(int i=0;i<h;i++)
{
for(int j=0;j<w;j++)
{
cin>>map[i][j];
if(map[i][j]=='@')
{
x=i;
y=j;
map[i][j]='.';
}
}
}
//dfs(x,y);
bfs(x,y);
cout<<ans<<endl;
}
return 0;
}