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 <vector>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>

#define rep(i, n) for(int i = 0; i < n; i ++)
#define ALL(T) T.begin(), T.end()
#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define MEMSET(v, h) memset((v), h, sizeof(v))

using namespace std;

typedef pair<int, int> pii;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<string> vs;

const int INF = 1 << 24;
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};

int cnt, w, h;
char m[100][100];

void dfs(pii);

int main(void){
    while(cin >> w >> h, w){
        MEMSET(m, '#');

        cnt = 0;
        pii p;

        rep(y, h){
            cin >> m[y];
            rep(x, w) if(m[y][x] == '@') m[y][x] = '.', p = mp(x, y);
        }

        dfs(p);
        cout << cnt << endl;
    }

    return 0;
}

void dfs(pii p){
    if((p.X < 0 || w <= p.X) ||
       (p.Y < 0 || h <= p.Y)) return;

    if(m[p.Y][p.X] == '.') cnt ++;
    else return;
    m[p.Y][p.X] = '#';

    rep(i, 4){
        pii next = mp(p.X + dx[i], p.Y + dy[i]);
        dfs(next);
    }
}

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

using namespace std;

struct TILE {
	char tile;
	bool hantei;
};

typedef pair<int, int> P;

TILE yuka[20][20];
int w, h;

int main() {
	while (cin >> w >> h) {
		if (w == 0 && h == 0) {
			break;
		}
		int si, sj;
		int imove[4] = {1, 0, -1, 0};
		int jmove[4] = {0, 1, 0, -1};
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				cin >> (yuka[i][j]).tile;
				(yuka[i][j]).hantei = false;
				if ((yuka[i][j]).tile == '@') {
					si = i;
					sj = j;
				}
			}
		}
		queue<P> q;
		q.push(P(si, sj));
		while (!q.empty()) {
			P p = q.front();
			q.pop();
			(yuka[p.first][p.second]).hantei = true;
			for (int k = 0; k < 4; k++) {
				if (0 <= p.first+imove[k] && p.first+imove[k] < h 
					&& 0 <= p.second+jmove[k] && p.second+jmove[k] < w 
					&& (yuka[p.first+imove[k]][p.second+jmove[k]]).tile == '.' 
					&& !(yuka[p.first+imove[k]][p.second+jmove[k]]).hantei) {
						q.push(P(p.first+imove[k], p.second+jmove[k]));
				}
			}
		}
		int counter = 0;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if ((yuka[i][j]).hantei) {
					counter++;
				}
			}
		}
		cout << counter << endl;
	}
	return 0;
}

C++: #include <iostream> // C++の入出力
#include <stdio.h> // C の入出力
#include <algorithm> // sort などのアルゴリズム
#include <string.h> // C の文字列用
#include <ctype.h> // C の文字種用
#include <string> // C++の文字列
#include <math.h>
#include <sstream>
#include <cstdio>
#include <queue>
using namespace std;
char fel[20][20];
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
void dfs(int i,int x,int y);
int h,w,sx,sy,cnt=1;
int main(void){
    while(true){
        cin>>w>>h;
        if(h+w==0)break;
        for(int s=0;s<h;s++){
            string in;
            cin>>in;
            for(int i=0;i<w;i++){
                fel[s][i]=in[i];
                if(fel[s][i]=='@'){
                    sx=i;
                    sy=s;
                }
            }
        }
        dfs(0,sx,sy);
        cout<<cnt<<endl;
        cnt=1;
    }
    return 0;
}

void dfs(int i,int x,int y){
    fel[y][x]='#';
    for(int s=0;s<4;s++){
        int nx=x+dx[s];
        int ny=y+dy[s];
        if(nx>=0&&nx<w&&ny>=0&&ny<h&&fel[ny][nx]=='.'){
            cnt++;
            dfs(i+1,nx,ny);
        }  
    }
    return;
}

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

#define pb         push_back

const int INF = 100000000;

int dx[4] = {-1, 0 , 1, 0}, dy[4] = {0 , -1, 0, 1};
int w, h;
vector<string> ma;

void rec(int y, int x);

int main(void) {
	while(1){
		cin >> w >> h;
		if(w == 0 && h == 0) break;

		ma = vector<string>();

		for(int i=0; i<h; i++){
			string t;
			cin >> t;
			ma.pb(t);
		}

		int sx, sy;
		for(int i=0; i<h; i++){
			//cout << ma[i] << endl;
			for(int j=0; j<w; j++){
				if(ma[i][j] == '@'){
					ma[i][j] = '.';
					sx = j;
					sy = i;
				}
			}
		}

		rec(sy, sx);

		int ans = 0;
		for(int i=0; i<h; i++){
			for(int j=0; j<w; j++){
				if(ma[i][j] == 'o'){
					ans++;
				}
			}
		}

		cout << ans << endl;

	}
	
	return 0;
}

void rec(int y, int x){
	//cout << y << ' ' << x << endl;
	if(ma[y][x] == '.') ma[y][x] = 'o';
	else return;
	int nx, ny;
	for(int i=0; i<4; i++){
		nx = x + dx[i];
		ny = y + dy[i];

		if(nx >= 0 && nx < w && ny >= 0 && ny < h){
			rec(ny, nx);
		}
	}
}

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

//#define int long long
#define DBG 0
#define dump(o) if(DBG){cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) if(DBG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define each(it,c) for(auto it=(c).begin();it!=(c).end();it++)
#define all(c) c.begin(),c.end()
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);

char A[22][22] = {};
int f[22][22] = {};
int W, H;

bool isinrange(int i, int j) {
	return 0 <= i&&i < H && 0 <= j&&j < W;
}

int di[] = { 1,0,-1,0 };
int dj[] = { 0,1,0,-1 };
void dfs(int i, int j) {
	if (isinrange(i, j) == false)return;
	if (A[i][j] == '#')return;
	if (f[i][j])return;
	f[i][j] = true;
	rep(k, 0, 4) {
		int ii = i + di[k];
		int jj = j + dj[k];
		dfs(ii, jj);
	}
}

signed main() {
	for (; cin >> W >> H&&W&&H;) {
		memset(f, 0, sizeof(f));
		memset(A, 0, sizeof(A));
		int ai, aj;
		rep(i, 0, H) rep(j, 0, W) {
			cin >> A[i][j];
			if (A[i][j] == '@') {
				ai = i, aj = j;
			}
		}
		dfs(ai, aj);
		int ans = 0;
		rep(i, 0, H)rep(j, 0, W) {
			if (f[i][j]) {
				ans++;
				dump(i);
				dump(j);
			}
		}
		cout << ans << endl;
		rep(i, 0, H)dumpc(f[i]);
	}
	return 0;
}