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

#endif
#include <iostream>
#include <memory>
#include <vector>
#include <string>
#include <algorithm>
#include <assert.h>
#include <complex>
#include <queue>
#define rep(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
using namespace std;
using ll = long long;
ll ll_max = numeric_limits<ll>::max();
ll ll_min = numeric_limits<ll>::min();
int int_max = numeric_limits<int>::max();
int int_min = numeric_limits<int>::min();
int W, H;
int lab[20][20];
string tmp;
queue<pair<int, int> > now;
int dirs[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
bool check_point(pair<int, int> point) {
    bool ret = true;
    ret &= point.first > -1;
    ret &= point.second > -1;
    ret &= point.first < H;
    ret &= point.second < W;
    ret &= lab[point.first][point.second] == int_max;
    return ret;
}
int bfs() {
    int cnt = 1;
    while (!now.empty()) {
        pair<int, int> start = now.front();
        now.pop();
        for (auto dir : dirs) {
            auto next = make_pair(start.first + dir[0], start.second + dir[1]);
            if (check_point(next)) {
                now.push(next);
                lab[next.first][next.second] = lab[start.first][start.second] + 1;
                ++cnt;
            }
        }
    }
    return cnt;
}
int main(void){
    cin.tie(0);
    ios::sync_with_stdio(false);

    while (cin >> W >> H && W && H) {
        rep(h, H) {
            cin >> tmp;
            rep(w, W) {
                switch (tmp[w]) {
                    case '.':
                        lab[h][w] = int_max;
                        break;
                    case '#':
                        lab[h][w] = int_min;
                        break;
                    case '@':
                        lab[h][w] = 0;
                        now.push(make_pair(h, w));
                        break;
                }
            }
        }
        cout << bfs() << endl;
    }    

    return 0;
}

C++: #include<bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n ; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define whole(f, x, ...) ([&](decltype((x)) whole) { return (f)(begin(whole), end(whole), ## __VA_ARGS__); })(x) // decltypeで型取得、引数があればva_argsのところに入れる
using namespace std;
typedef long long ll; // long longをllでかけるようにした
const int INF = 1e9;
int n, m, ans = 0;
char W[100][100];

// 通れるようなマスがいくつあるか知りたい
void dfs(int y, int x){
    W[y][x] = '#';
    for(int dx = -1; dx <= 1; dx++){
        for(int dy = -1; dy <= 1; dy++){
            if(dx == dy and dx != 0 and dy != 0) continue;
            else if(dx == -dy) continue;
            int nx = dx + x;
            int ny = dy + y;
            if(W[ny][nx] == '.' and 0 <= nx and nx < n and 0 <= ny and ny < m){
                dfs(ny, nx);
                ans++;
            }
        }
    }
    return ;
}

int main(void){
    while(true){
        ans = 0; // 新しいデータセットの度に初期化
        cin >> n >> m;
        if(n == 0 and m == 0) return 0;
        REP(i, m){
            REP(j, n){
                cin >> W[i][j];
            }
        }

        REP(i, m){
            REP(j, n){
                if(W[i][j] == '@'){
                    ans++;
                    dfs(i, j);
                }
            }
        }
        cout << ans << endl;
    }
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(n); i++)
#define FOR(i,a,n) for(int i=(a); i<(n); i++)
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
const int INF = 1e9;
const int MOD = 1e9+7;

vector<string> mat;
vector<vector<bool>> seen;
int h, w;

const int dh[4] = {-1, 0, 1, 0};
const int dw[4] = {0, -1, 0, 1};

void dfs(int sh, int sw) {
    seen[sh][sw] = true;
    rep(i, 4) {
        int nh = sh+dh[i], nw = sw+dw[i];
        if (nh<0 || nh>h-1 || nw<0 || nw>w-1) continue;
        if (seen[nh][nw]) continue;
        if (mat[nh][nw] == '#') continue;
        dfs(nh, nw);
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    
    while(true) {
        cin >> w >> h;
        if (w+h == 0) break;
        mat.clear();
        rep(i, h) {
            string s; cin >> s;
            mat.push_back(s);
        }

        int sh, sw;
        rep(i, h) rep(j, w) if (mat[i][j]=='@') sh=i, sw=j;

        seen.assign(h, vector<bool>(w, false));
        dfs(sh, sw);

        int ans = 0;
        rep(i, h) rep(j, w) if(seen[i][j]) ans++;
        cout << ans << endl;
    }
}

C++: #include<iostream>
#include<vector>
#include<queue>
using namespace std;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
int dfs(int sx ,int sy, vector<vector<bool> > &map){
	int t=0;
if(map[sx][sy]){
	map[sx][sy]=false;
	t++;
for(int i=0;i<4;i++){
int x=sx+dx[i];
int y=sy+dy[i];
if(map[x][y])	t+=dfs(x,y,map);
}
}
return t;
}
int main(void){
while(1){
int h,w,sx,sy;
cin>>w>>h;
if(h==0)break;
vector<vector<bool> > map(h+2,vector<bool>(w+2,false));
for(int i=1;i<=h;i++){
	for(int j=1;j<=w;j++){
		char a;
		cin>>a;
		if(a=='.')map[i][j]=true;
		if(a=='@'){
			map[i][j]=true;
			sx=i;
			sy=j;
		}
	}
}
//cout<<sx<<' '<<sy<<endl;
/*
for(int i=1;i<=h;i++){
	for(int j=1;j<=w;j++)if(map[i][j])cout<<1;
	else cout<<0;
	cout<<endl;
}*/
cout<<dfs(sx,sy,map)<<endl;
}


return 0;
}