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<iomanip>
#include<cstdio>
#include<string>
#include<cstring>
#include<climits>
#include<algorithm>
#include<vector>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<deque>
#include<tuple>
#include<list>
#include<unordered_map>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vii vector<int>
#define vll vector<ll>
#define lb lower_bound
#define pb push_back
#define mp make_pair
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep2(i,a,b) for(ll i=a;i<b;i++)
#define repr(i,n) for(ll i=n-1;i>=0;i--)
#define all(x) x.begin(),x.end()
// #define int ll
using namespace std;
const int INF (1 << 30);
const ll LLINF (1LL << 55LL);
const int MOD = 1000000007;
const int MAX = 510000;

ll dx[4] ={1,0,-1,0} , dy[4] ={0,1,0,-1};
ll h,w;
ll nowx,nowy;
char field[30][30];

void bfs(){
    ll d[30][30];
    queue<pll> que;
    rep(i,h)rep(j,w) d[i][j]=0;
    d[nowx][nowy]=1;
    que.push(pll(nowx,nowy));
    ll ans=1;
    while(!que.empty()){
        pll p = que.front();
        que.pop();
        rep(i,4){
            ll nx = p.first + dx[i] , ny = p.second + dy[i];
            if( nx>=0 && nx <h && ny >=0 && ny < w  && d[nx][ny] == 0 ){
                if(field[nx][ny]=='.'){
                    que.push(pll(nx,ny));
                    ans++;
                }
                d[nx][ny]=1;
            }
        }
    }
    cout<<ans<<endl;
    return ;
}


int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    
    while(true){
        cin>>w>>h;
        if(w==0&&h==0) break;
        rep(i,h)rep(j,w){
            cin>>field[i][j];
            if(field[i][j]=='@') {
                nowx=i,nowy=j;
            }
        }
        bfs();
    }
    return 0;
}

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

bool flags[21][21];
char maps[21][21];
int W, H;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};

int dfs(int x, int y)
{
  if(x < 0 || y < 0 || x >= H || y >= W || flags[x][y] || maps[x][y] == '#')return 0;

  flags[x][y] = true;
  int sum = 0;
  for(int i = 0; i < 4; i++){
    sum += dfs(x + dx[i], y + dy[i]);
  }
  
  return sum + 1;
}

int main()
{
  while(cin >> W >> H && !((W * H) == 0))
    {
      memset(maps, 0, 21 * 21 * sizeof(char));
      memset(flags, 0, 21 * 21 * sizeof(bool));

      int sx, sy;

      for(int i = 0; i < H; i++) {
        for(int j = 0; j < W; j++) {
          cin >> maps[i][j];
          if(maps[i][j] == '@'){
            sx = i;sy = j;
          }
        }
      }
      cout << dfs(sx, sy) << endl;
    }
  return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) { return n ? 1 + __builtin_popcount(n & (n - 1)) : 0; }
#endif

#define foreach(it, c) for (__typeof__((c).begin()) it=(c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount

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

template <class T> void max_swap(T& a, const T& b) { a = max(a, b); }
template <class T> void min_swap(T& a, const T& b) { a = min(a, b); }

typedef long long ll;
typedef pair<int, int> pint;

const double PI = acos(-1.0);
const int dx[] = { 0, 1, 0, -1 };
const int dy[] = { 1, 0, -1, 0 };



int main()
{
    int w, h;
    while (scanf("%d%d", &w, &h), w | h)
    {
	char tile[32][32];
	memset(tile, '#', sizeof(tile));
	for (int i = 1; i <= h; ++i)
	{
	    scanf("%s", tile[i] + 1);
	    tile[i][w + 1] = '#';
	}

	int sx, sy;
	for (int y = 1; y <= h; ++y)
	    for (int x = 1; x <= w; ++x)
		if (tile[y][x] == '@')
		    sx = x, sy = y;

	int res = 0;
	queue<pint> q;
	q.push(pint(sx, sy));
	while (!q.empty())
	{
	    pint t = q.front(); q.pop();
	    int x = t.first, y = t.second;

	    if (tile[y][x] == '#')
		continue;

	    ++res;
	    tile[y][x] = '#';

	    for (int i = 0; i < 4; ++i)
		q.push(pint(x + dx[i], y + dy[i]));
	}
	printf("%d\n", res);
    }
}