Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

var (
	sc = bufio.NewScanner(os.Stdin)
)

type Color int

const (
	ColorBlack Color = iota
	ColorRed
)

type Node struct {
	ID    int
	X, Y  int
	Color Color
	Nexts []*Node
}

func main() {
	for sc.Scan() {
		if sc.Text() == "0 0" {
			break
		}
		chunks := strings.Split(sc.Text(), " ")
		w, _ := strconv.Atoi(chunks[0])
		h, _ := strconv.Atoi(chunks[1])

		// Initialize map
		m := map[int]map[int]*Node{}
		for x := 1; x <= w; x++ {
			m[x] = map[int]*Node{}
			for y := 1; y <= h; y++ {
				m[x][y] = &Node{ID: x + y*w, X: x, Y: y}
			}
		}
		for x := 1; x <= w; x++ {
			for y := 1; y <= h; y++ {
				nexts := []*Node{}
				if n, ok := m[x-1][y]; ok {
					nexts = append(nexts, n)
				}
				if n, ok := m[x][y-1]; ok {
					nexts = append(nexts, n)
				}
				if n, ok := m[x+1][y]; ok {
					nexts = append(nexts, n)
				}
				if n, ok := m[x][y+1]; ok {
					nexts = append(nexts, n)
				}
				m[x][y].Nexts = nexts
			}
		}

		// Set colors
		queue := []*Node{}
		touchedIDs := map[int]struct{}{}
		for y := 1; y <= h; y++ {
			sc.Scan()
			for i, c := range sc.Text() {
				switch c {
				case '.', '@':
					n := m[i+1][y]
					n.Color = ColorBlack
					if c == '@' {
						queue = append(queue, n)
						touchedIDs[n.ID] = struct{}{}
					}
				case '#':
					m[i+1][y].Color = ColorRed
				}
			}
		}

		// Search
		visitedIDs := map[int]struct{}{}
		for {
			if len(queue) == 0 {
				break
			}
			n := queue[0]
			queue = queue[1:]
			visitedIDs[n.ID] = struct{}{}
			for _, next := range n.Nexts {
				if next.Color == ColorRed {
					continue
				}
				if _, ok := touchedIDs[next.ID]; !ok {
					queue = append(queue, next)
					touchedIDs[n.ID] = struct{}{}
				}
			}
		}
		fmt.Println(len(visitedIDs))
	}
}

C++: #include <vector>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <cassert>
using namespace std;
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define EXIST2(s,e) (find(ALL(s),(e))!=(s).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define EACH(t,i,c) for(t::iterator i=(c).begin(); i!=(c).end(); ++i)
const double EPS = 1e-10;
const double PI  = acos(-1.0);

int w,h;
int dx[]={-1,0,1,0};
int dy[]={0,-1,0,1};

int dfs(vs &f, vvi &visited,int x,int y){
	int ret=1;
	visited[y][x]=1;
	REP(i,4){
		int xx=x+dx[i],yy=y+dy[i];
		if(xx>=0&&yy>=0&&xx<w&&yy<h){
			if(f[yy][xx]!='#'&&!visited[yy][xx]){
				ret+=dfs(f,visited,xx,yy);
			}
		}
	}
	return ret;
}

int main(){
	while(cin>>w>>h,w){
		vs f(h,string(w,' '));
		int sx,sy;
		REP(i,h){
			REP(j,w){
				cin>>f[i][j];
				if(f[i][j]=='@'){
					sx=j;sy=i;
				}
			}
		}
		vvi visited(h,vi(w));
		cout<<dfs(f,visited,sx,sy)<<endl;
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
  int mp[21][21];
  int w,h;
  int x[4]={1,-1,0,0};
  int y[4]={0,0,1,-1};
  int m;
  bool b;
  char s;
  int ans;
  while(1){
    cin>>w>>h;
    if(w==0 && h==0) break;
    ans=0;
    for(int i=0;i<21;i++){
      for(int j=0;j<21;j++){
	mp[i][j]=0;
      }
    }
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	cin>>s;
	if(s=='#') mp[i][j]=1;
	else if(s=='@') mp[i][j]=2;
      }
    }
    b=true;
    while(b){
      b=false;
      for(int i=0;i<h;i++){
	for(int j=0;j<w;j++){
	  if(mp[i][j]==2){
	    /*for(int k=0;k<4;k++){*/
	      if(mp[i+1][j]==0){
		mp[i+1][j]=2;
		b=true;
	      }
	      if(mp[i-1][j]==0){
		mp[i-1][j]=2;
		b=true;
	      }
	      if(mp[i][j+1]==0){
		mp[i][j+1]=2;
		b=true;
	      }
	      if(mp[i][j-1]==0){
		mp[i][j-1]=2;
		b=true;
	      }
	  }
	}
      }
    }
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	/*cout<<mp[i][j]<<" ";*/
	if(mp[i][j]==2) ans++;
      }
      /* cout<<endl;*/
    }
    cout<<ans<<endl;
  }
  return 0;
}

C++: #include <iostream> 
#include <string> 
#include <vector> 
#include <cmath> 
#include <algorithm> 
#include <cstdlib> 
#include <ctime> 
#include <cstdio> 
#include <functional> 
#include <set> 
#include <sstream> 
#include <cctype>
#include <stack>
#include <queue>
#include <cstring>
#include <map>
 
using namespace std;

bool vis[30][30];
int cnt;
int dy[]={-1,0,1,0};
int dx[]={0,1,0,-1};
int w,h;

void dfs(vector<string> &vs,int y,int x){

	if(vis[y][x]) return;
	vis[y][x]=true;
	cnt++;

	for(int i=0;i<4;i++){
		int ny=y+dy[i],nx=x+dx[i];
		if( 0<=ny && ny<h && 0<=nx && nx<w && vs[ny][nx]=='.') dfs(vs,ny,nx);
	}

}

int main(){

	while(cin>>w>>h,w||h){

		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++) vis[i][j]=false;
		}
		cnt=0;

		vector<string> vs(h);
		int x,y;
		for(int i=0;i<h;i++){
			cin>>vs[i];
			for(int j=0;j<w;j++){
				if(vs[i][j]=='@') y=i,x=j;
			}
		}

		dfs(vs,y,x);
		cout<<cnt<<endl;

	}

	return 0;
}

C++: #include <iostream>
#include <algorithm>

using namespace std;

int w,h;
char map[20][20];
int cnt;

int dirx[4] = {1,0,-1,0};
int diry[4] = {0,1,0,-1};

bool inField(int x,int y){
	return (0<=x) && (x<w) && (0<=y) && (y<h);
}

//深さ優先探索
void dfs(int posx,int posy){
	cnt ++;
	map[posy][posx] = '#';
	for(int i=0;i<4;i++){
		int nextx = posx + dirx[i];
		int nexty = posy + diry[i];
		if(inField(nextx,nexty) && map[nexty][nextx] != '#'){
			dfs(nextx,nexty);
		}
	}
	
}
//名簿
int main(){
	while(true){
		cin >> w >> h;
		if(w==0){
			break;
		}
		cnt =0;
		int posx,posy;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				cin >> map[i][j];
				if(map[i][j] == '@'){
					posx = j;
					posy = i;
					map[i][j] = '.';
				}
				
			}
		}
		dfs(posx,posy);
		cout << cnt << endl;
	}
}

C++: #include <iostream>
#include <vector>
#include <string>

using namespace std;

int w, h;
int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};
int dfs(vector<string>& mp, int x, int y){
  int cnt = 0;  
  for(int i=0;i<4;++i){
	int tx = x + dx[i];
	int ty = y + dy[i];
	if(tx < 0 || w <= tx || ty < 0 || h <= ty) continue;
	if(mp[ty][tx] == '#') continue;
	mp[ty][tx] = '#';
	cnt += dfs(mp, tx, ty) + 1;
  }
  return cnt;
}

int main(){
  while(true){
	if(!(cin >> w >> h)) return 0;
	if(w == 0 && h == 0) break;
	vector<string> vs(h);
	for(int i=0;i<h;++i)
	  cin >> vs[i];
	int x, y;
	for(int i=0;i<h;++i)
	  for(int j=0;j<w;++j)
		if(vs[i][j] == '@') x=j, y=i;
	vs[y][x] = '.';
	cout << dfs(vs,x,y) << endl;
  }
  return 0;
}

C++: #include<iostream>
#include<queue>
#include<utility>
#define INF 100000
using namespace std;
typedef pair<int,int> pii;
typedef queue<pii> que;
int w,h;
int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
string map[30];

int bfs(pii p){
  que q;
  int nx,ny,ans=1;
  q.push(p);
  while(q.size()!=0){
    for(int i=0;i<4;i++){
      nx=q.front().first+dx[i];ny=q.front().second+dy[i];
      if(0<=nx && nx<w && 0<=ny && ny<h && map[ny][nx]=='.'){
	map[ny][nx]='$';
	//cout<<nx<<ny<<endl;
	q.push(make_pair(nx,ny));
	ans++;
      }
    }
    q.pop();
  }
  return ans;
}

int main(){
  pii start;
  while(cin>>w>>h,h){
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	cin>>map[i][j];
	if(map[i][j]=='@') start=make_pair(j,i);
      }
    }
    cout<<bfs(start)<<endl;
  }
  return 0;
}