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++: #if 0
????????£?????????????´?1??????
pdf??????????????????????????????????????????????????§?????????????????????
???????????§??????????????§?????????????????°?????????????????£??¨????????§?????????
??¢?????\????????????????????????????????£??¨??????????????????
#endif

#include <iostream>
#include <queue>
using namespace std;
class pos
{
	public:
	int x;
	int y;
	pos(int,int);
};

pos::pos(int a,int b)
{
	x=a;y=b;
}

int main()
{
	int w,h;
	int g[21][21];
/*	for(int i=0;i<21;i++)
		for(int j=0;j<21;j++)
			g[i][j]=-1;
*/	while(1)
	{
		cin>>w>>h;
		if(w==0&&h==0) break;
		for(int i=0;i<h;i++)
			for(int j=0;j<w;j++)
				g[i][j]=-1;
		int psx,psy;
		bool visited[21][21];
		for(int i=0;i<h;i++)
			for(int j=0;j<w;j++)
			{
				char s;
				cin>>s;
				switch(s)
				{
					case '.':
						g[i][j]=0;break;
					case '#':
						g[i][j]=1;break;
					case '@':
						g[i][j]=2;psx=i;psy=j;break;
				}
				visited[i][j]=false;
			}

/*		for(int i=0;i<h;i++)
		{
			for(int j=0;j<w;j++)
				cout<<g[i][j];
			cout<<endl;
		}
*/
		int num=0;
		queue<pos> Q;
		Q.push(pos(psx,psy));
		num++;
		visited[psx][psy]=true;
		while(!Q.empty())
		{
			pos now=Q.front();
			Q.pop();
	
			for(int i=-1;i<=1;i+=2)
			{
				int newx=now.x+i;
				int newy=now.y;
				if(newx>=0&&newx<h&&newy>=0&&newy<w&&g[newx][newy]==0&&visited[newx][newy]==false)
				{
					Q.push(pos(newx,newy));		
					num++;
					visited[newx][newy]=true;
				}
			}
			for(int i=-1;i<=1;i+=2)
			{                
				int newx=now.x;
				int newy=now.y+i;
				if(newx>=0&&newx<h&&newy>=0&&newy<w&&g[newx][newy]==0&&visited[newx][newy]==false)
				{
					Q.push(pos(newx,newy));
					num++;
					visited[newx][newy]=true;
				}
			}

		}
		cout<<num<<endl;
								


	}
}

C++: #include<iostream>
#include<string>
#include<stack>
using namespace std;
int w,h;
string map[40];
int vec[4][2]={{0,1},{0,-1},{-1,0},{1,0}};

struct pos{
	pos(int X,int Y){
		x=X,y=Y;
	}
	int x,y;
};

int move(int x,int y){
	int ans=0;
	map[x][y]='#';
	stack<pos> bfs;
	bfs.push(pos(x,y));
	while(!bfs.empty()){
		pos n=bfs.top();bfs.pop();
		ans++;
		for(int i=0;i<4;i++){
			if((n.x+vec[i][0]>=0)&&(n.y+vec[i][1]>=0)&&(n.x+vec[i][0]<h)&&(n.y+vec[i][1]<w)){
				if(map[n.x+vec[i][0]][n.y+vec[i][1]]=='.'){
					map[n.x+vec[i][0]][n.y+vec[i][1]]='#';
					bfs.push(pos(n.x+vec[i][0],n.y+vec[i][1]));
				}
			}
		}
		
	}
	
	
	return ans;
}

int main(){
	while(cin>>w>>h,w){
		int fx,fy;
		for(int i=0;i<h;i++){
			cin>>map[i];
		}
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(map[i][j]=='@'){
					fx=i;
					fy=j;
					break;
				}
			}
		}
		int ans=move(fx,fy);
		cout<<ans<<endl;
		
	}
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
bool c[20][20] = {} ;
int cnt,w, h, p[20][20] = {};

int main() {
	int j, f,cx,cy;
	char in[20];
	while (1) {
		for (int i = 0; i < 400; i++) {
			c[i / 20][i % 20] = p[i / 20][i % 20] = 0;
		}
		cnt = 0;
		f = 1;
		scanf("%d %d", &w, &h);
		if (w ==0&& h == 0)
			break;
		scanf("\n", &j);
		for (int i = 0; i < h; i++) {
			scanf("%s", &in);
			for (j = 0; j < w; j++) {
				switch (in[j]) {
				case '#':
					c[j][i] = 0;
					p[j][i] = -1;
					break;
				case '@':
					cx = j;
					cy = i;
					p[j][i] = 1;
				case '.':
					c[j][i] = 1;
				}
			}
		}

		/*for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++)
				printf("%3d", c[j][i]);
			printf("\n");
		}*/

		while (f == 1) {
			f = 0;
			if (w != 1 && h != 1) {
				for (int i = 0; i < w*h; i++) {
					if (p[i / h][i%h] == 0 && (p[i / h + 1][i % h] == 1 || p[i / h - 1][i % h] == 1 || p[i / h][i % h + 1] == 1 || p[i / h][i % h - 1] == 1)) {
						p[i / h][i%h] = 1;
						f = 1;
					}
				}
			}
			if (w == 1 && h != 1) {
				for (int i = 0; i < h; i++) {
					if (p[0][i] == 0 && (p[0][i + 1] == 1 || p[0][i - 1] == 1)) {
						p[0][i] = 1;
						f = 1;
					}
				}
			}

			if (w != 1 && h == 1) {
				for (int i = 0; i < w; i++) {
					if (p[i][0] == 0 && (p[i + 1][0] == 1 || p[i - 1][0] == 1)) {
						p[i][0] = 1;
						f = 1;
					}
				}
			}


			/*for (int i = 0; i < h; i++) {
				for (int j = 0; j < w; j++)
					printf("%3d", p[j][i]);
				printf("\n");
			}
			printf("\n");*/
		}

		for (int i = 0; i < w*h; i++) {
			if (p[i / h][i%h] == 1)
				cnt++;
		}
		printf("%d\n", cnt);
	}
	return 0;
}

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

#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)

int w,h;
char f[32][32];

int fill(int x, int y){
	if( x<0 || y<0 || x>=w || y>=h || f[y][x]=='#' )return 0;
	f[y][x] = '#';
	return 1 + fill(x-1,y) + fill(x+1,y) + fill(x,y+1) + fill(x,y-1);
}

int main(){
	int x,y;
	while(cin>>w>>h,w|h){
		rep(i,h)cin>>f[i];
		rep(i,h)rep(j,w)if( f[i][j]=='@' ){
			x=j; y=i;
		}
		printf("%d\n",fill(x,y));
	}
	return 0;
}

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

int count;
int w,h;
int x,y;
char map[20][20];

void tannkenn(int x,int y);

int main()
{
	while(cin>>w>>h,w&&h){
	count=0;
	for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                cin >> map[i][j];
				if(map[i][j]=='@'){
					x=i;
					y=j;
				}
			}
	}

	tannkenn(x,y);
	cout<<count<<endl;
	}
	return 0;
}

void tannkenn(int x,int y)
{
	count++;
	map[x][y]='#';

	if(x+1<h && map[x+1][y]=='.')
		tannkenn(x+1,y);
	if(y+1<w && map[x][y+1]=='.')
		tannkenn(x,y+1);
	if(x-1>=0 && map[x-1][y]=='.')
		tannkenn(x-1,y);
	if(y-1>=0 && map[x][y-1]=='.')
		tannkenn(x,y-1);
}