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 <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <complex>
using namespace std;

typedef long long ll;
typedef pair<int,int> P;

#define all(c) (c).begin(),(c).end()
#define pb push_back
#define mp make_pair
#define ERASE(v,i) (v).erase(remove(all(v),i),(v).end())
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define each(it,c) for(typeof((c).begin()) it=(c).begin();it!=(c).end();++it)
#define debug(x) cerr<<#x<<" = "<<(x)<<endl;
#define LINE cerr<<"LINE: "<<__LINE__<<endl;

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();}
template<class T> void preview(T a,T b){for(T it=a;it!=b;++it)cerr<<*it<<" ";cerr<<endl;}

const int INF = 100000000;
const double PI = acos(-1.0), EPS = 1e-10;

int main() {
	for(int W, H; cin >> W >> H, W || H;) {
		vector<string> field(H);
		rep(i,H) cin >> field[i];
		// First = y, Second = x;
		P s;
		rep(y,H) rep(x,W) {
			if(field[y][x] == '@') {
				s.first = y;
				s.second = x;
			}
		}

		bool reach[20][20];
		memset(reach, 0, sizeof reach);

		int ans = 1;
		reach[s.first][s.second] = true;

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

		queue<P> que;
		que.push(s);
		while(!que.empty()) {
			P here = que.front();
			que.pop();

			rep(i,4) {
				int ny = here.first + dy[i];
				int nx = here.second + dx[i];
				if(0 <= ny && ny < H && 0 <= nx && nx < W && field[ny][nx] == '.' && reach[ny][nx] == false) {
					reach[ny][nx] = true;
					ans++;
					que.push(mp(ny, nx));
				}
			}
		}

		cout << ans << endl;
	}
	return 0;
}

C++: //include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <fstream>
#include <complex>
#include <math.h>

using namespace std;

//conversion
//------------------------------------------
inline long toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
inline long toLong(string s) {long v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long, long> PLL;
typedef long long LL;
//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

//constant
//--------------------------------------------

//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))

const long double EPS = 1e-12;
const int INF = 10000;

char mp[22][22];

int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
bool flag[22][22];

int bfs(int s,int t){
	int sum=0;
	queue<PII>pi;
	pi.push(MP(s,t));
	flag[s][t]=true;
	while(!pi.empty()){
		PII p=pi.front();pi.pop();
		
		sum++;
		REP(i,4){
			int y=p.first+dy[i],x=p.second+dx[i];
			if(mp[y][x]!='#'&&!flag[y][x]){
				flag[y][x]=true;
				pi.push(MP(y,x));
			}
		}
	}
	return sum;
}
int main(){
	int W,H;
	while(cin>>W>>H){
		if(W==0&&H==0)break;
		REP(i,22)REP(j,22){
			mp[i][j]='#';
		}
		CLR(flag);
		int y,x;
		REP(i,H)REP(j,W){
			cin>>mp[i+1][j+1];
			if(mp[i+1][j+1]=='@'){
				mp[i+1][j+1]='.';
				y=i+1;x=j+1;
			}
		}
		
		//cout<<"!"<<y<<" "<<x<<endl;
		cout<<bfs(y,x)<<endl;
	}
	return 0;
}

C++: #include<iostream>
#include<queue>
using namespace std;

int point(int x,int y,int w,int h){
  int s;
  s= x*w*y*h;
  return s;
}

int point_x(int x,int w){
  int s;
  s=x/w;
  return s;
}

int point_y(int x,int w){
  int s;
  s=x%w;
  return s;
}

int main(){
  int w,h;
  cin>>w>>h;
  while(w!=0){
    int a[21][21]={};
    int sx,sy;
    for(int i=0;i<w;i++){
      for(int j=0;j<h;j++){
	char s;
	cin>>s;
	if(s=='.'){
	  a[i][j]=1;
	}else if(s=='#'){
	  a[i][j]=-1;
	}else{
	  a[i][j]=2;
	  sx=i;
	  sy=j;
	}
      }
    }

    int d[21][21];
    for(int i=0;i<21;i++){
      for(int j=0;j<21;j++){
	d[i][j]=-1;
      }
    }
    queue<int>q;
    d[sx][sy]=0;
    int str;
    str=point(sx,sy,w,h);
    q.push(str);
    while(! q.empty()){
      int cur =q.front();
      q.pop();
      int x,y;
      x=point_x(cur,w);
      y=point_y(cur,w);
      if(x>=1){
	if(a[x-1][y]==1&&d[x-1][y]==-1){
	  d[x-1][y]=0;
	  q.push(point(x-1,y,w,h));
	}
      }
      if(x<=w-2){
	if(a[x+1][y]==1&&d[x+1][y]==-1){
	  d[x+1][y]=0;
	  q.push(point(x+1,y,w,h));
	}
      }
      if(y>=1){
	if(a[x][y-1]==1&&d[x][y-1]==-1){
	  d[x][y-1]=0;
	  q.push(point(x,y-w,w,h));
	}
      }
      if(y<=h-2){
	if(a[x][y+1]==1&&d[x][y+1]==-1){
	  d[x][y-1]=0;
	  q.push(point(x,y+w,w,h));
	}
      }
    }

    int t=0;
    for(int i=0;i<w;i++){
      for(int j=0;j<h;j++){
	if(d[i][j]==0){
	  t+=1;
	}
      }
    }
    cout << t<< endl;
    cin>>w>>h;
  }
}