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>
using namespace std;
#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) a.begin(), a.end()
#define MS(m,v) memset(m,v,sizeof(m))
#define D10 fixed<<setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template<class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template<class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
int dx[] = { -1, 0, 0, 1 }; int dy[] = { 0, -1, 1, 0 };
//[int dx[] = { -1, -1, -1, 0, 0, 1, 1, 1 }; int dy[] = { 0, -1, 1, -1, 1, 0, -1, 1 };
bool valid(int x, int y, int h, int w) { return (x >= 0 && y >= 0 && x < h&&y < w); }
int place(int x, int y, int w) { return w*x + y; }

/*--------------------template--------------------*/

int main()
{
	int w, h;
	while (cin >> w >> h, w)
	{
		vs fld(h);
		REP(i, h) cin >> fld[i];
		int x, y;
		REP(i, h)REP(j, w)
		{
			if (fld[i][j] == '@')
			{
				x = i, y = j;
				fld[i][j] = '.';
			}
		}
		queue<pii> que;
		que.push(pii(x, y));
		int cnt = 0;
		while (que.size())
		{
			int tx = que.front().first, ty = que.front().second;
			que.pop();
			if (fld[tx][ty] == '.')
			{
				fld[tx][ty] = '$';
				cnt++;
			}
			REP(i, 4)
			{
				int nx = tx + dx[i], ny = ty + dy[i];
				if (valid(nx, ny, h, w) && fld[nx][ny] == '.')
				{
					que.push(pii(nx, ny));
				}
			}
		}
		cout << cnt << endl;
	}

	return 0;
}

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

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define ALL(v) (v).begin(), (v).end()
#define p(s) cout<<(s)<<endl
#define p2(s, t) cout << (s) << " " << (t) << endl
#define br() p("")
#define pn(s) cout << (#s) << " " << (s) << endl
#define p_yes() p("Yes")
#define p_no() p("No")

const ll mod = 1e9 + 7;
const ll inf = 1e18;

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

ll W, H;
ll cnt;

void dfs(vector<string> &A, ll x, ll y){
    cnt++;
    A[y][x]='#';

    FOR(i, 0, 4){
        ll tx = x + dx[i];
        ll ty = y + dy[i];

        if(0<=tx && tx<W && 0<=ty && ty<H && A[ty][tx]=='.'){
            dfs(A, tx, ty);
        }
    }
}

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

    for(;;){
        cin >> W >> H;

        if(W==0 && H==0){
            return 0;
        }

        vector<string> A(H);
        FOR(i, 0, H){
            cin >> A[i];
        }

        ll start_x, start_y;
        FOR(y, 0, H){
            FOR(x, 0, W){
                if(A[y][x]=='@'){
                    start_x = x;
                    start_y = y;
                }
            }
        }

        cnt = 0;
        dfs(A, start_x, start_y);
        p(cnt);
    }
    
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using lint     = long long;
const lint inf = 1LL << 60;
const lint mod = 1000000007;

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    while (true) {
        int h, w;
        cin >> w >> h;
        if (w == 0 && h == 0)
            return 0;
        vector<string> s(h);
        for (int i = 0; i < h; ++i) {
            cin >> s[i];
        }
        int sy, sx;
        [&] {
            for (int i = 0; i < h; ++i) {
                for (int j = 0; j < w; ++j) {
                    if (s[i][j] == '@') {
                        sy = i;
                        sx = j;
                        return;
                    }
                }
            }
        }();
        queue<pair<int, int>> q;
        q.emplace(sy, sx);
        const int dx[4] = {-1, 1, 0, 0};
        const int dy[4] = {0, 0, -1, 1};
        auto inside     = [&](int y, int x) { return 0 <= y && y < h && 0 <= x && x < w; };
        int ret         = 1;
        while (q.size() != 0) {
            auto p = q.front();
            q.pop();
            for (int i = 0; i < 4; ++i) {
                int nx = p.second + dx[i];
                int ny = p.first + dy[i];
                if (inside(ny, nx) && s[ny][nx] == '.') {
                    ret++;
                    s[ny][nx] = '#';
                    q.emplace(ny, nx);
                }
            }
        }
        cout << ret << "\n";
    }
    return 0;
}