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 <queue>
#include <set>
using namespace std;

const int MAX_SIZE = 20;
typedef pair<int, int> P;

int calc(char f[][MAX_SIZE], queue<P> q, int W, int H) {
  set<P> s;
  while (q.size()) {
    P p = q.front();
    q.pop();
    s.insert(p);
    int di[] = {-1, 1, 0, 0};
    int dj[] = {0, 0, -1, 1};
    for (int k = 0; k < 4; ++k) {
      int i = p.first  + di[k];
      int j = p.second + dj[k];
      if (i >= 0 && j >= 0 && i < H && j < W && f[i][j] == '.') {
        pair<set<P>::iterator, bool> r = s.insert(P(i, j));
        if (r.second) q.push(*r.first);
      }
    }
  }
  return s.size();
}
int main() {
  int W, H;
  char f[MAX_SIZE + 1][MAX_SIZE + 1];
  while (true) {
    queue<P> q;
    cin >> W >> H;
    if (W == 0 && H == 0) break;
    for (int i = 0; i < H; ++i) {
      cin >> f[i];
      for (int j = 0; j < W; ++j) {
        if (f[i][j] == '@') {
          q.push(P(i, j));
        }
      }
    }
    cout << calc(f, q, W, H) << endl;
  }
}

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

#define rep(i, n) for(int i = 0 ; i < n ; i++)
using namespace std;
void dfs(int x, int y);

int cnt = 0;
string data[20];
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
int w, h;

int main()
{
    while(true){
      cin >> w >> h;
      if(w == 0 && h == 0) break;
      
      int sx, sy;
      for(int i = 0 ; i < h ; i++){
	cin >> data[i];
	for(int j = 0 ; j < w ; j++){
	  if(data[i][j] == '@'){
	    sx = i;
	    sy = j;
	  }
	}
      }
      dfs(sx, sy);
      cout << cnt << '\n';
      cnt = 0;
  }
}

void dfs(int x, int y)
{
  data[x][y] = '#';
  cnt++;

  rep(i, 4){
    int nx = x + dx[i], ny = y + dy[i];
    if(0 <= nx && nx < w && 0 <= ny && ny < h && data[nx][ny] == '.'){
      dfs(nx, ny);
    }
      }
}

C++: #include<iostream>
#include<queue>
#include<utility>
using namespace std;
int main(){
while(true){
        int w,h;
        cin >>w >>h;
        if(w==0&&h==0)break;
        char map[w][h];
        bool is_reached[w][h];
        queue<pair<int,int> >recentpos;
        int ans =0;
        for(int j =0;j<h;++j){
                for(int i = 0;i<w;++i){
                        char temp;
                        cin >>temp;
                        map[i][j]=temp;
                        is_reached[i][j]=false;
                        if(temp=='@'){
                                recentpos.push(pair<int,int>(i,j));
                                is_reached[i][j]=true;
                                ans++;
                      }
                }
        }
        while(!recentpos.empty()){
                int old_x=recentpos.front().first;
                int old_y=recentpos.front().second;
                recentpos.pop();
                for(int dx =-1;dx<2;dx++){
                        for(int dy=-1;dy<2;dy++){
                                if(dx*dy!=0)continue;
                                int x=old_x+dx;
                                int y=old_y+dy;
                                if(x<0||x>=w||y<0||y>=h)continue;
                                if(is_reached[x][y]||map[x][y]=='#')continue;
                                recentpos.push(pair<int,int>(x,y));
                                is_reached[x][y]=true;
                                ans++;
                        }
                }
        }
        cout<<ans<<'\n';
        }
        return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>

#define rep(idx, max)	for(int idx = 0, idx##Max = max; idx < idx##Max; idx ++)
#define rrep(idx, min, max)	for(int idx = min, idx##Max = max; idx <= idx##Max; idx ++)
#define MAX_W	32
#define MAX_H	32

typedef std::vector<int>	VEC;

int	g_map[MAX_W * MAX_H];
int	g_step;

typedef struct _POINT
{
	int x, y;
	_POINT() {}
	_POINT(int _x, int _y) : x(_x), y(_y) {}
} POINT;

enum
{
	RED_BLOCK = -1,
	BLACK_BLOCK = 0,
};

void PrintMap()
{
	rep(y, MAX_H)
	{
		rep(x, MAX_W)
		{
			::printf("%2d|", g_map[MAX_W * y + x]);
		}
		::putchar('\n');
	}
	::putchar('\n');
}

void Solve(POINT &pt, int step)
{
	static int dx[] = { +1, 0, -1, 0};
	static int dy[] = { 0, +1, 0, -1};

	//PrintMap();

	g_map[MAX_W * pt.y + pt.x] = step + 1;
	g_step++;

	rep(i, 4)
	{
		POINT ptn(pt.x + dx[i], pt.y + dy[i]);
		if (g_map[MAX_W * ptn.y + ptn.x] == BLACK_BLOCK)
		{
			Solve(ptn, step + 1);
		}
	}
}

int main(int nArgs, char **lplpszArgs)
{
	int	width, height;
	int	map[MAX_W * MAX_H];

	while (std::cin >> width >> height, width)
	{
		POINT start;

		// Init map info
		rep(y, MAX_H)
		{
			rep(x, MAX_W)
			{
				map[MAX_W * y + x] = RED_BLOCK;
			}
		}

		// Input map info
		rrep(y, 1, height)
		{
			char szLine[0x100];
			::scanf("%s", szLine + 1);
			rrep(x, 1, width)
			{
				switch(szLine[x])
				{
				case '.':	map[MAX_W * y + x] = BLACK_BLOCK;	break;
				case '#':	map[MAX_W * y + x] = RED_BLOCK;	break;
				case '@':
					map[MAX_W * y + x] = BLACK_BLOCK; 
					start = POINT(x, y);
					break;
				}
			}
		}
		//::printf("Start: (%d, %d)\n", start.x, start.y);
		g_step = 0;
		::memcpy(g_map, map, sizeof(map));
		::Solve(start, 0);
		std::cout << g_step << std::endl;
	}
}

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){
		for(a=0;a<21;a++){
			for(b=0;b<21;b++){
				tile[a][b]='\0';
			}
		}
		scanf("%d %d",&w,&h);
		if(w==0 && h==0)break;
		ans=1;
		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;
}