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 <cstdio>
#include <iomanip>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <stack>
#include <utility>
#include <numeric>
#include <algorithm>
#include <functional>
#include <cctype>
#include <complex>
#include <string>
#include <sstream>
#include <fstream>
#include <cassert>
using namespace std;

//common

typedef int  i32;
typedef long long i64,ll;
typedef long double ld;

#define BR "\n"
#define ALL(c) (c).begin(),(c).end()
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define FOR(i,l,r) for(int i=(int)l;i<(int)(r);++i)
#define EACH(it,o) for(auto it = (o).begin(); it != (o).end(); ++it)
#define IN(l,v,r) ((l)<=(v) && (v)<(r))

//config
//#define NDEBUG
//#define INF 1<<30
//#define EPS 1e-8
//const ll MOD =100000007;

//debug
#ifdef NDEBUG
#define DUMP(x)
#define DUMPLN(x)
#define DEBUG(x)
#define DEBUGLN(x)
#define LINE()
#define LINELN()
#define CHECK(exp,act)
#define STOP(e)
#else
#define DUMP(x)  cerr << #x << " = " << (x)
#define DUMPLN(x)  DUMP(x) <<endl
#define DEBUG(x) DUMP(x) << LINE() << " " << __FILE__
#define DEBUGLN(x) DEBUG(x)<<endl
#define LINE()    cerr<< " (L" << __LINE__ << ")"
#define LINELN()    LINE()<<endl
#define CHECK(exp,act)  if(exp!=act){DUMPLN(exp);DEBUGLN(act);}
#define STOP(e)  CHECK(e,true);if(!(e)) exit(1);
#endif

template<class T> inline string toString(const vector<T>& x) {
	stringstream ss;
	REP(i,x.size()){
		if(i!=0)ss<<" ";
		ss<< x[i];
	}
	return ss.str();
}

template<class T> inline string toString(const vector<vector<T> >& map) {
	stringstream ss;
	REP(i,map.size()){
		if(i!=0)ss<<BR;
		ss<< toString(map[i]);
	}
	return ss.str();
}
template<class K,class V>  string toString(map<K,V>& x) {
	string res;stringstream ss;
	for(auto& p:x)ss<< p.first<<":" << p.second<<" ";
	return ss.str();
}

string BITtoString(int bit){
    stringstream ss;
    while(bit!=0){ss<<(bit%2);bit/=2;}
    string res=ss.str();reverse(ALL(res));
    return res;
}

template<typename T,typename V> inline T pmod(T v,V MOD){
	return (v%MOD+MOD)%MOD;
}
#define nextInt(n) scanf("%d",&n)
#define defInt(n) int n;nextInt(n)
#define nextLong(n) scanf("%lld",&n)
#define defLong(n) ll n;nextLong(n)
#define nextDouble(n) scanf("%lf",&n)


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

class Main{
	public:

	int N;
	void run(){
		// ifstream cin("D2");
//		ofstream cout( "D2.out" );
		
		while(true){
			int W,H;cin >> W >> H;if(W==0 && H ==0)break;
			vector<string> board(H);
			REP(y,H)cin >> board[y];

			vector<vector<bool>> passed(H,vector<bool>(W));
			queue<pair<int,int>> que;
			REP(y,H)REP(x,W){
				if(board[y][x]=='@'){
					que.push(make_pair(y,x));
				}
			}

			int res=0;
			while(!que.empty()){
				pair<int,int> pos=que.front();que.pop();
				if(passed[pos.first][pos.second])continue;
				passed[pos.first][pos.second]=true;
				res++;

				REP(d,4){
					int ny=pos.first+dy[d],nx=pos.second+dx[d];
					if(IN(0,ny,H) && IN(0,nx,W)){
						if(board[ny][nx]=='.'){
							que.push(make_pair(ny,nx));							
						}
					}
				}
			}

			cout << res <<endl;
		}
	}


};
int main(){
	cout <<fixed<<setprecision(15);	
	ios::sync_with_stdio(false);
	Main().run();
	return 0;
}

C++: #include<map>
#include<set>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<string>
#include<vector>
#include<complex>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<numeric>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<functional>
#include<cctype>

#define mp make_pair
#define pb push_back
#define REP(i,a,n) for(int i = a;i < (n);i++)
#define rep(i,n) for(int i = 0;i < (n);i++)
#define all(s) s.begin(), s.end()
#define rall(s) s.rbegin(), s.rend()

using namespace std;

typedef long long LL;
typedef vector<int> VI;
typedef vector<vector<int> > VII;
typedef pair<int,int> PII;

const int DX[]={1,0,-1,0},DY[]={0,-1,0,1};
int W,H;
vector<string> room;

int dfs(int y,int x){
	int res = 0;
	rep(i,4){
		if(y + DY[i] < 0 || y + DY[i] >= H || x + DX[i] < 0 || x + DX[i] >= W || room[y + DY[i]][x + DX[i]] == '#') continue;
		room[y + DY[i]][x + DX[i]] = '#';
		res++;
		res += dfs(y + DY[i],x + DX[i]);
	}
	return res;
}

int main(){
	cin.tie(0);
   	ios::sync_with_stdio(false);

	int sy,sx,ans;

	while(1){
		cin >> W >> H;
		if(W == 0 && H == 0) break;
		room.resize(H);

		rep(i,H){
			string s;
			cin >> s;
			room[i] = s;
		}

		rep(i,H){
			rep(j,W){
				if(room[i][j] == '@'){
					sy = i;
					sx = j;
					room[i][j] = '#';
				}
			}
		}

		ans = dfs(sy,sx);

		cout << ans + 1 << endl;
	}

	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

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

#define rep(i,n) for(int i=0;i<(n);++i)
#define repi(i,a,b) for(int i=int(a);i<(b);i++)
#define all(x) x.begin(),x.end()

const ll mod = 1e9+7;
const ll INF = 1e9;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

bool valid(int lx,int ux,int ly,int uy,int x,int y){
	return (lx<=x&&x<ux&&ly<=y&&y<uy);
}

int main()
{
	int w,h;
	while(cin>>w>>h,w!=0&&h!=0){
		vector<string> s(h);
		rep(i,h)cin>>s[i];

		queue<P> que;
		rep(i,h)rep(j,w)if(s[i][j]=='@')s[i][j]='x',que.push(P(i,j));
		while(!que.empty()){
			P p = que.front();que.pop();
			int y=p.first,x=p.second;
			rep(i,4){
				if(valid(0,w,0,h,x+dx[i],y+dy[i])){
					if(s[y+dy[i]][x+dx[i]]=='.'){
						s[y+dy[i]][x+dx[i]]='x';
						que.push(P(y+dy[i],x+dx[i]));
					}
				}
			}
		}
		int ans=0;
		rep(i,h)rep(j,w)if(s[i][j]=='x')ans++;
		cout<<ans<<endl;
	}
	return 0;
}