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"
#define REP(i, n, N) for(ll i=(n); i < N; i++)
#define CK(N, A, B) (A <= N && N < B)
#define p(s) cout << (s) << endl
#define p2(a, b) cout << (a) << ' ' << (b) << endl
using namespace std;
typedef long long ll;

const int NODE_SIZE=10101;
int H;
int W;
int start_y, start_x;

string field_s[NODE_SIZE];

int d[NODE_SIZE][NODE_SIZE];

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

void bfs(){
    queue<pair<int,int>> q;
    bool visited[H][W];
    REP(i,0,H) REP(j,0,W) visited[i][j]=false;
    REP(i,0,H) REP(j,0,W) d[i][j]=-1;
    q.push({start_y, start_x});
    d[start_y][start_x]=0;
    while(!q.empty()){
        int cur_y = q.front().first;
        int cur_x = q.front().second;
        int curStep = d[cur_y][cur_x];
        q.pop();
        if(visited[cur_y][cur_x]) continue;
        visited[cur_y][cur_x] = true;

        REP(k,0,4){
            int next_y = cur_y + dy[k];
            int next_x = cur_x + dx[k];

            if(!CK(next_y,0,H) || !CK(next_x,0,W))continue;
            if(field_s[next_y][next_x]=='#')continue;
            if(field_s[next_y][next_x]=='.')field_s[next_y][next_x]='@';
            if(!visited[next_y][next_x]){
                q.push({next_y, next_x});
                d[next_y][next_x] = d[cur_y][cur_x] + 1;
            }
        }
    }
}

int main() {

    while(cin >> W >> H && W) {

        string s;
        REP(i,0,H){
            cin >> field_s[i];
        }
        REP(i,0,H) REP(j,0,W) if(field_s[i][j]=='@'){
            start_x = j;
            start_y = i;
        }
        bfs();
        int cnt=0;

        REP(i,0,H) REP(j,0,W) if(field_s[i][j]=='@')cnt++;
        p(cnt);
    }
    return 0;
}

C++: /* vim:set foldmethod=marker: */
#include <bits/stdc++.h>
using namespace std;

//{{{
#define all(x) (x).begin(),(x).end()
#define REP(i,a,b) for(int i=(int)(a); i<(int)(b); i++)
#define RREP(i,a,b) for(int i=(int)(a); i>=b; i--)
#define rep(i,n) REP(i,0,n)
#define rrep(i,n) RREP(i,n,0)
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pii> vp;

#define dump(x) (cerr << #x << "=" << x << endl)
template<class T> ostream& osContainer(ostream& os,T c) { os<<'[';for(decltype(c.begin()) it=c.begin();it!=c.end();it++)os<<*it<<',';os<<']';return os; }
template<class T> ostream& operator<<(ostream& os,const vector<T>& v) { return osContainer(os,v); }
template<class T> ostream& operator<<(ostream& os,const set<T>& s) { return osContainer(os,s); }
template<class T> ostream& operator<<(ostream& os,const multiset<T>& s) { return osContainer(os,s); }
template<class T,class S> ostream& operator<<(ostream& os,const map<T,S>& m) { return osContainer(os,m); }
template<class T,class S> ostream& operator<<(ostream& os,const multimap<T,S>& m) { return osContainer(os,m); }
template<class T1,class T2>
ostream& operator<<(ostream& os, const pair<T1,T2>& p){ os << '(' << p.first << ',' << p.second << ')'; return os; }

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

char check[20][20];
char field[21][21];

void solve()
{
    int w, h;
    while (cin >> w >> h, w || h) {
        memset(check, 0, sizeof(check));

        int sx, sy;

        rep(i, h) {
            rep(j, w){
                char c;
                cin >> c;
                field[i][j] = c;
                if (c == '@') {
                    sy = i, sx = j;
                    check[sy][sx] = 1;
                }
            }
            cin.ignore();
        }

        queue<pair<int, int>> q;
        q.push(mp(sy, sx));

        int s = 1;

        while(!q.empty()) {
            auto p = q.front();q.pop();
            int y = p.first, x = p.second;

            rep(i, 4) {
                int ddy = y + dy[i], ddx = x + dx[i];

                if (0 <= ddy && ddy < h && 0 <= ddx & ddx < w && field[ddy][ddx] == '.' && !check[ddy][ddx]) {
                    s++;
                    check[ddy][ddx] = 1;
                    q.push(mp(ddy, ddx));
                }
            }
        }

        cout << s << endl;
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout.setf(ios::fixed);
    cout.precision(12);
    solve();
}

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

int n, m;
char map[20][20];
int ans = 0;

int dfs(int x, int y){
	if(0 <= x && x < m && 0 <= y && y < n){
		if(map[x][y] != '.') return 0;
		map[x][y] = '@';
		dfs(x-1, y);
		dfs(x+1, y);
		dfs(x, y-1);
		dfs(x, y+1);
	}
	return 0;
}

int main(){
	while(1){
		for(int i=0; i<20; i++){
			for(int j=0; j<20; j++){
				map[i][j] = '#';
			}
		}
		scanf("%d %d", &n, &m);
		if(n == 0 && m == 0) break;
		for(int i=0; i<m; i++){
			scanf("%s", map[i]);
		}
		int x, y;
		for(int i=0; i<m; i++){
			for(int j=0; j<n; j++){
				if(map[i][j] == '@'){
					x = i;
					y = j;
				}
			}
		}
		dfs(x-1, y);
		dfs(x+1, y);
		dfs(x, y-1);
		dfs(x, y+1);
		for(int i=0; i<m; i++){
			for(int j=0; j<n; j++){
				if(map[i][j] == '@') ans++;
			}
		}
		printf("%d\n", ans);
		ans = 0;
	}
	return 0;
}