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<bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using msi = map<string, int>;
using mii = map<int, int>;
using psi = pair<string, int>;
using pii = pair<int, int>;
using vlai = valarray<int>;
#define rep(i,n) for(int i=0;i<n;i++)
#define range(i,s,n) for(int i=s;i<n;i++)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define fs first
#define sc second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define INF 1E9
#define EPS 1E-9
#define MOD (ll)(1E9+7)
#define PI 3.1415926535897932384

template <class T>ostream &operator<<(std::ostream &o, const vector<T> &v)
{
	rep(i, v.size()) {
		o << (i > 0 ? " " : "") << v[i];
	}
	return o;
}



int main() {
	int w, h;
	int dx[4] = { -1,0,1,0 };
	int dy[4] = { 0,-1,0,1 };
	char input;
	while (1) {
		pair<int, int> start;
		cin >> w >> h;
		if (w == 0) break;
		vector<vector<int>> field(h + 2, vector<int>(w + 2, 1));
		for (int i = 1; i < h + 1; i++) {
			for (int j = 1; j < w + 1; j++) {
				cin >> input;
				if (input == '.') {
					field[i][j] = 0;
				}
				else if (input == '@') {
					start.first = i;
					start.second = j;
					field[i][j] = 0;
				}
			}
		}
		/*
		rep(i, h + 2) {
			rep(j, w + 2) {
				cout << field[i][j] << " " ;
			}
			cout << endl;
		}
		*/

		queue<pair<int, int>> que;
		int ans = 0;
		que.emplace(start.first, start.second);
		while (!que.empty()) {
			pair<int, int> now = que.front();
			que.pop();
			if (field[now.first][now.second] == 1) continue;
			ans++;
			field[now.first][now.second] = 1;
			rep(k, 4) {
				//cout << now.first + dy[k] << " " << now.second + dx[k] << endl;
				//cout << field[now.first + dy[k]][now.second + dx[k]] << endl;
				
				if (field[now.first + dy[k]][now.second + dx[k]] == 0) {
					que.emplace(now.first + dy[k], now.second + dx[k]);
				}
				
			}
		}
		
		cout << ans << endl;
	}
}

C++: 
#define B(a,x,b) ((a)<=(x)&&(x)<(b))

char T[999];
int x,y,cx,cy,i,c;

void R(int a,int b){
	if(B(0,a,x)&&B(0,b,y)&&T[b*20+a]){
		T[b*20+a]=0;i++;R(a-1,b);R(a+1,b);R(a,b-1);R(a,b+1);
	}
}

main(){
	for(;scanf("%d%d",&x,&y),x;){
		for(i=0;i<x*y;i++){
			int _x=i%x, _y=i/x;
			for(;(c=getchar())!='\n';);
			switch(c){
				case '@': cx=_x;cy=_y;
				case '.': T[_y*20+_x]=1; break;
				case '#': T[_y*20+_x]=0; break;
			}
		}
		i=0;
		R(cx,cy);
		printf("%d\n",i);
	}
	return 0;
}

C++: #include <stdio.h>
#include <iostream>
#include <string>

using namespace std;
void checkPiece(int x, int y);//, char map[][]);
void check(int x, int y);
char map[22][22];
int flag = 1;

int main(){
	int W, H;
	while(1){
		int count = 0;
		int x, y;
		cin>>W>>H;
		if(W == 0 && H == 0) { return 0; }
		//char map[H+2][W+2];
		
		for (int i = 0; i < H + 2; ++i)
		{
			for (int j = 0; j < W + 2; ++j)
			{
				map[j][i] = '#';
			}
		}

		for (int i = 1; i < H + 1; ++i)
		{
			for (int j = 1; j < W + 1; ++j)
			{
				cin>>map[j][i];
				if(map[j][i]=='@'){x = j; y = i;}
			}
		}

		//cout<<map[2][1]<<y<<endl;
		check(x, y);
	while(flag){
		flag = 0;
		for (int i = 1; i < H + 1; ++i)
		{
			for (int j = 1; j < W + 1; ++j)
			{
				//cout<<i<<endl;
				check(j, i);
			}
		}
		//flag = 0;
		//cout<<flag<<endl;
	}
/*
	for (int i = 0; i < H + 2; ++i)
		{
			for (int j = 0; j < W + 2; ++j)
			{
				cout<<map[j][i];
			}
			cout<<endl;
		}
*/
		for (int i = 0; i < H + 2; ++i)
		{
			for (int j = 0; j < W + 2; ++j)
			{
				if(map[j][i] == '@'){ count++; }
			}
		}
		cout<<count<<endl;

	}
	return 0;

}

void checkPiece(int x, int y){//, char map[][]){
	if(map[x][y] == '.') { map[x][y] = '@'; }
	if(map[x][y] == '@'){
		if(map[x+1][y] != '#') return checkPiece(x+1, y);
		else if(map[x-1][y] != '#') return checkPiece(x-1, y);
		else if(map[x][y+1] != '#') return checkPiece(x, y+1);
		else if(map[x][y-1] != '#') return checkPiece(x, y-1);
	}
	return;
}

void check(int x, int y){
	if(map[x][y] == '@'){
		if(map[x+1][y] != '#' && map[x+1][y] == '.') { map[x+1][y] = '@'; flag = 1;}
		if(map[x-1][y] != '#' && map[x-1][y] == '.') { map[x-1][y] = '@'; flag = 1;}
		if(map[x][y+1] != '#' && map[x][y+1] == '.') { map[x][y+1] = '@'; flag = 1;}
		if(map[x][y-1] != '#' && map[x][y-1] == '.') { map[x][y-1] = '@'; flag = 1;}
	}
	return;
}

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

const int dx[] = {1,0,-1,0};
const int dy[] = {0,-1,0,1};
queue<pair<int,int> > M;
int W,H;
int q[20+5][20+5] = {};	
char p[20+5][20+5];

int main() {
	while(cin >> W >> H && W != 0){
		int count = 0;
		for (int i = 1; i <= H; i++){
			for (int j = 1; j <= W; j++){
				cin >> p[i][j];
				if (p[i][j] == '@' ){
					M.push(make_pair(i,j));
					count = 1;
					q[i][j] = 1;
				}
			}
		}
		while(!M.empty()){
			W = M.front().first;
			H = M.front().second;
			M.pop();
			for (int k = 0; k <= 3; k++)
			{		
				if (p[W+dx[k]][H+dy[k]] == '.' && q[W+dx[k]][H+dy[k]] == 0)
				{
					M.push(make_pair(W + dx[k], H + dy[k]));
					q[W + dx[k]][H + dy[k]] = 1;
					count += 1;
				}
			}
		}
		cout << count << endl;
	}
}

C++: #include<iostream>
using namespace std;
bool b[21][21];
int w,h,count=0;

void dfs(int x, int y){
 b[y][x]=1;count++;
 int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
 int nx,ny;
 for(int i=0;i<4;i++){
  nx=dx[i]+x;
  ny=dy[i]+y;
  if(0<=nx&&nx<w&&0<=ny&&ny<h)
   if(b[ny][nx]==0)dfs(nx,ny);
  }
 }

int main(){
char a;
int sx,sy;
while(1){
cin>>w>>h;
count=0;
b[21][21]={};
for(int i=0;i<h;i++)
for(int j=0;j<w;j++){
cin>>a;
if(a=='#')b[i][j]=1;
if(a=='@'){sx=j;sy=i;}
}
dfs(sx,sy);
cout<<count<<endl;
}
 return 0;
}