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++: #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 < w*h; i++) {
					if (p[i / h][i%h] == 0 && (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 < 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;
						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>
#include<cstdio>
#include<vector>

using namespace std;

int w,h;
int ans=1;
char tile[21][21];

void saiki(int x,int y){
	if(x<0 | x>h | y<0 | y>w)return;
	tile[x][y]='#';
	if(tile[x-1][y]=='.')ans++,saiki(x-1,y);
	if(tile[x+1][y]=='.')ans++,saiki(x+1,y);
	if(tile[x][y-1]=='.')ans++,saiki(x,y-1);
	if(tile[x][y+1]=='.')ans++,saiki(x,y+1);
}

int main(void){
	int a,b; 
	while(1){
		scanf("%d %d",&w,&h);
		if(w==0 && h==0)break;
		ans=1;
		memset(tile,0,sizeof(tile));
		for(a=0;a<h;a++){
			scanf("%s",tile[a]);
		}
		for(a=0;a<h;a++){
			for(b=0;b<w;b++){
				if(tile[a][b]=='@')saiki(a,b);
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

char rb[21][21];
bool ch[21][21]={0};
int x,y,h,w,ans;
int d[4]={0,1,0,-1};

void solve(int nx,int ny);

int main(){
  int i,j;
  while(1){
    cin >> w >> h;
    if(w+h==0)break;
    ans = 0;
    for(i=0;i<h;i++)for(j=0;j<w;j++)ch[i][j]=0;
    for(i=0;i<h;i++)for(j=0;j<w;j++){
	cin >> rb[i][j];
	if(rb[i][j]=='@'){
	  x=i;
	  y=j;
	  ch[i][j]=1;
	  rb[i][j]='#';
	}
      }
    solve(x,y);
    cout << ans+1 << endl;
  }
  return 0;
}

void solve(int nx,int ny){
  int nex,ney,i;
  for(i=0;i<4;i++){
    nex = nx+d[i];
    ney = ny+d[i^1];
    if(nex>=0&&nex<h&&ney>=0&&ney<w&&rb[nex][ney]=='.'&&ch[nex][ney]==0){
      ans++;
      ch[nex][ney]=1;
      rb[nex][ney]='#';
      solve(nex,ney);
    }
  }
}

C++: #include<iostream>

using namespace std;

int main(void) {
	int x, y, ans;
	bool bit;
	
	while(1) {
		cin >> x >> y;
		if(x == 0 && y == 0) { break; }
		char map[y][x];
		
		for(int i=0; i<y; i++) { cin >> map[i]; }
		do {
			bit = false;
			for(int i=0; i<y; i++) {
				for(int j=0; j<x; j++) {
					if(map[i][j] == '@') {
						if(j>0 && map[i][j-1] != '#' && map[i][j-1] != '@') { map[i][j-1] = '@'; bit = true; }
						if(i>0 && map[i-1][j] != '#' && map[i-1][j] != '@') { map[i-1][j] = '@'; bit = true; }
						if(j<x-1 && map[i][j+1] != '#' && map[i][j+1] != '@') { map[i][j+1] = '@'; bit = true; }
						if(i<y-1 && map[i+1][j] != '#' && map[i+1][j] != '@') { map[i+1][j] = '@'; bit = true; }
					}
				}
			}
		} while(bit);
		ans = 0;
		for(int i=0; i<y; i++) {
			for(int j=0; j<x; j++) {
				if(map[i][j] == '@') { ans++; }
			}
		}
		cout << ans << endl;
	}
	
	return 0;
}

C++: #include<iostream>
#include<queue>

using namespace std;

struct node{
  node(){};
  int x,y;
  node(int y,int x):y(y),x(x){}
};

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

int main(){
  int w,h,i,j;
  while(cin >> w >> h && w){
    char tile[h][w];
    int ans = 0;
    struct node now,next;
    queue<struct node> que;
    for(i=0;i<h;i++){
      for(j=0;j<w;j++){
        cin >> tile[i][j];
        if(tile[i][j]=='@'){
          que.push(node(i,j));
          tile[i][j]='#';
        }
      }
    }
    while(!que.empty()){
      now = que.front();
      ans++;
      que.pop();
      for(i=0;i<4;i++){
        int ddx = dx[i] + now.x;
        int ddy = dy[i] + now.y;
        if(ddx>w-1 || ddx<0 || ddy>h-1 || ddy<0)continue;
        if(tile[ddy][ddx]=='.'){
          que.push(node(ddy,ddx));
          tile[ddy][ddx] = '#';
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}