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 <string>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <utility>
#include <iomanip>

#define ll long long int
#define pb push_back
#define mk make_pair
#define pq priority_queue

using namespace std;
typedef pair<int, int> P;
typedef pair<ll, int> Pl;
const int inf = 1e9;
const ll linf = 1LL << 50;

int h, w;
int sx, sy;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};
bool used[21][21];
int color[21][21];

void dfs(int x, int y){
		for(int i = 0; i < 4; i++){
				int nx = x + dx[i];
				int ny = y + dy[i];
				if(nx >= 0 && nx < h && ny >= 0 && ny < w && !used[nx][ny] && color[nx][ny] == 1){
						used[nx][ny] = true;
						dfs(nx, ny);
				}
		}
}

int main(int argc, char const* argv[])
{
	while(true){
			cin >> w >> h;
			if(w == 0)break;
			for(int i = 0; i < h; i++){
					string s;
					cin >> s;
					for(int j = 0; j < w; j++){
							if(s[j] == '.' || s[j] == '@')color[i][j] = 1;
							else color[i][j] = -1;
							if(s[j] == '@'){
									sx = i; sy = j;
							}
							used[i][j] = false;
					}
			}
			dfs(sx, sy);
			int res = 0;
			for(int i = 0; i < h; i++){
					for(int j = 0; j < w; j++){
							if(used[i][j] == 1)res++;
					}
			}
			cout << res << endl;
	}
	return 0;
}

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

const int MAX = 20;
int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,1,0,-1 };
int map[MAX][MAX];
int ans = 1;

void dfs(int x, int y)
{
	map[x][y] = 2;

	for (int i = 0; i < 4; i++) {
		int nx = x + dx[i];
		int ny = y + dy[i];

		if (nx >= 0 && nx < MAX&&ny >= 0 && ny < MAX) {
			if (map[nx][ny] == 1) {
				ans++;
				dfs(nx, ny);
			}
		}
	}
}

int main()
{
	while (true) {
		ans = 1;
		int W, H;
		cin >> W >> H;

		if (W == 0 && H == 0) {
			break;
		}

		for (int i = 0; i < MAX; i++) {
			for (int j = 0; j < MAX; j++) {
				map[i][j] = 0;
			}
		}

		int x, y;

		for (int i = 0; i < H; i++) {
			string str;
			cin >> str;

			for (int j = 0; j < W; j++) {
				if (str.substr(j, 1) == ".") {
					map[i][j] = 1;
				}
				else if (str.substr(j, 1) == "@") {
					map[i][j] = 2;
					x = i;
					y = j;
				}
			}
		}

		dfs(x, y);

		cout << ans << endl;
	}

	return 0;
}

C++: #include<cstdio>
char table[20][20];
int n,m,si,sj;
int solve(int a,int b){
	int xx[]={a,a+1,a,a-1},yy[]={a+1,a,a-1,a},res=1;
	if(table[a][b]!='.')return 0;
	table[a][b]='#';
	for(int i=0;i<4;i++){
		if(xx[i]<0 || xx[i]>=m || yy[i]<0 || yy[i]>=n)continue;
		res+=solve(xx[i],yy[i]);
	}
	return res;
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=0;i<m;i++){
		for(int j=0;j<n;j++){
			scanf(" %c",&table[i][j]);
			if(table[i][j]=='@')si=i,sj=j,table[i][j]='.';
		}	
	}
	printf("%d\n",solve(si,sj));
	return 0;
}

C++: #include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
#include<climits>
#include<string>
#include<set>
#include<numeric>
#include<map>
#include<iostream>
using namespace std;
#define rep(i,n) for(int i = 0;i<((int)(n));i++)
#define reg(i,a,b) for(int i = ((int)(a));i<=((int)(b));i++)
#define irep(i,n) for(int i = ((int)(n)-1);i>=0;i--)
#define ireg(i,a,b) for(int i = ((int)(b));i>=((int)(a));i--)
typedef long long ll;
typedef pair<ll, ll> mp;

int W,H,f[20][20],ans[1000],cnt=0,sx,sy;
string m[20];

void find(int x,int y){
	if(x>=W || x<0 || y>=H || y<0)return;
	string t;
	t = m[x][y];
	if(t=="#")return;
	if(f[x][y]!=0)return;
	f[x][y] = 1;
	ans[cnt]++;
	find(x+1,y);
	find(x-1,y);
	find(x,y+1);
	find(x,y-1);
}

int main(void){
	while(1){
		cin>>W>>H;
		if(W==0 && H==0)break;
		ans[cnt]=0;
		rep(i,H){
			string s;
			cin>>s;
			rep(j,W){
				f[j][i]=0;
				m[j][i]=s[j];
				string t;
				t=m[j][i];
				if(t=="@"){
					sx=j;
					sy=i;
				}
			}
		}
		find(sx,sy);
		cnt++;
	}
	rep(i,cnt)cout<<ans[i]<<endl;
	return 0;
}

C++: /*
15 1
##@........##..
*/
#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <bitset>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,n) for (int i=(n)-1;i>=0;i--)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define REPS(i,f,n) for (int i=(f)-1;i>=(n);i--)
int dx[4] = {1, 0, 0, -1};
int dy[4] = {0, 1, -1, 0};

char board[21][21];
int w, h;
int x, y;// ?????¨?????????

void slove(void){
	int cnt = 0;
	queue<pint> q;
	q.push(make_pair(y, x));//?????¨????????????queue?????\??????

	while(!q.empty()){
		pint now = q.front(); q.pop();
		rep(i, 4){
			int nowy = now.fi + dy[i];
			int nowx = now.se + dx[i];
			if (0 <= nowy && nowy <= h - 1 && 0 <= nowx && nowx <= w - 1){
				if (board[nowy][nowx] == '.'){
					cnt++;
					board[nowy][nowx] = 0;
					//printf("board %c\n", board[nowy][nowx]);
					q.push(make_pair(nowy, nowx));
				}
			}
		}
		/*
		rep(i, h){
			rep(j, w){
				printf("%c", board[i][j]);
			}
			printf("\n");
		}
		*/
	}
	printf("%d\n", cnt + 1);//?§?????????´????????\??????
	return;
}

void scan(int w, int h){
	rep(i, h){
		rep(j, w){
			cin >> board[i][j];
			if (board[i][j] == '@'){
				y = i; x = j;
			}
		}
		getchar();
	}

	slove();
	return;
}

int main(void){
	rep(i, 1000){
		cin >> w >> h;

		if (w == 0) break;
		scan(w, h);
	}
	return 0;
}