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  INF 1100000000
#define	 MOD 1000000007
using namespace std;

int vis[22][22];
char mp[22][22];

int dfs(int sh, int sw, int ans){
	if(mp[sh+1][sw] == '.' && vis[sh+1][sw] == 0){
		vis[sh+1][sw] = 1;
		ans++;
		ans = dfs(sh+1, sw, ans);
	}
	if(mp[sh-1][sw] == '.' && vis[sh-1][sw] == 0){
		vis[sh-1][sw] = 1;
		ans++;
		ans = dfs(sh-1, sw, ans);
	}
	if(mp[sh][sw-1] == '.' && vis[sh][sw-1] == 0){
		vis[sh][sw-1] = 1;
		ans++;
		ans = dfs(sh, sw-1, ans);
	}
	if(mp[sh][sw+1] == '.' && vis[sh][sw+1] == 0){
		vis[sh][sw+1] = 1;
		ans++;
		ans = dfs(sh, sw+1, ans);
	}
	return ans;
}

int main(){
	int W, H, ans, sw, sh;
	
	while(1){
		ans = 0;
		cin >> W >> H;
		if(W == 0 && H == 0) break;
		for(int i=0;i<22;i++){
			for(int j=0;j<22;j++){
				vis[i][j] = 1;
			}
		}
			
		for(int i=1;i<=H;i++){
			for(int j=1;j<=W;j++){
				cin >> mp[i][j];
				if(mp[i][j] == '@'){
					sh = i;
					sw = j;
					vis[i][j] = 1;
				}else{
					vis[i][j] = 0;
				}
			}
		}
		
		ans = dfs(sh, sw, ans);
		ans++;
		cout << ans << endl; 
	}
	return 0;
}

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

using namespace std;

queue<pair<int,int> > Q;
pair<int,int> P;
int sum=0;
int W,H;
int ai,aj;
char x[25][25];
int y[25][25];
const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};

bool valid(int a,int b){
    if(0<=a && a<H && 0<=b && b<W && x[a][b]=='.'){
        return 1;
    }
    else{
        return 0;
    }
}

int solve( ){
    sum++;y[ai][aj]=1;
    while(!Q.empty()){
        pair<int,int> P2=Q.front();
        Q.pop();
        for(int i=0;i<4;i++){
            if(valid(P2.first+dx[i],P2.second+dy[i])==1&&y[P2.first+dx[i]][P2.second+dy[i]]==0){
                sum++;
                y[P2.first+dx[i]][P2.second+dy[i]]=1;
                P=make_pair(P2.first+dx[i],P2.second+dy[i]);
                Q.push(P);
            }
        }
    }
    return sum;
}
void inits(){
    sum=0;
    for(int i=0;i<25;i++ ){
        for(int j=0;j<25;j++){
            x[i][j]=0;
            y[i][j]=0;
        }
    }
}

int main(){
    while(cin>>W>>H&&W!=0){
        inits();
        for(int i=0;i<H;i++)
            for(int j=0;j<W;j++){
                cin>>x[i][j];
                if(x[i][j]=='@'){
                    ai=i;aj=j;
                }
            }
        P=make_pair(ai,aj);
        Q.push(P);
        cout<<solve()<<endl;
    }
    return 0;
}

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

using namespace std;
typedef pair<int, int> P;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, -1, 0, 1};
void solve()
{
	int W, H;
	while(cin >> W >> H, W || H)
	{
		vector< vector<char> > field(H);
		for(int i = 0; i < field.size(); ++i)
		{
			field[i].resize(W);
		}
		queue<P> Que;
		for(int i = 0; i < field.size(); ++i)
		{
			for(int j = 0; j < field[i].size(); ++j)
			{
				cin >> field[i][j];
				if(field[i][j] == '@')
				{
					P p;
					p.first = j;
					p.second = i;
					Que.push(p);
					field[i][j] = '#';
				}
			}
		}
		int count = 1;
		while(!Que.empty())
		{
			P p = Que.front();
			Que.pop();
			for(int i = 0; i < 4; ++i)
			{
				P np;
				np.first = p.first + dx[i];
				np.second = p.second + dy[i];
				if((0 <= np.first && np.first < W) && (0 <= np.second && np.second < H) && field[np.second][np.first] == '.')
				{
					Que.push(np);
					field[np.second][np.first] = '#';
					++count;
				}	
			}
		}
		cout << count << endl;
	}
}

int main()
{
	solve();
	return(0);
}

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

int n, m, cnt = 0;
const int MAX = 30;
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
bool seen[MAX][MAX];
char C[MAX][MAX];

void dfs(int x, int y){
    seen[x][y] = true;
    for(int i = 0; i < 4; i++){
        int nx, ny;
        nx = dx[i] + x;
        ny = dy[i] + y;
        if(nx < 0 or ny < 0 or nx >= n or ny >= m){
            continue;
        }
        if(seen[nx][ny] or C[nx][ny] == '#'){
            continue;
        }
        cnt++;
        dfs(nx, ny);
    }
}

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

    while(cin >> m >> n){
        if(n == 0 and m == 0) return 0;
        
        int sx, sy;
        for(int i = 0; i < n; i++){
            for(int j = 0; j < m; j++){
                seen[i][j] = false;
                cin >> C[i][j];
                if(C[i][j] == '@'){
                    sx = i;
                    sy = j;
                }
            }
        }

        cnt = 1;
        dfs(sx, sy);
        cout << cnt << endl;
    }

    return 0;
}

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

int moved = 0;
int w, h;
char myMap[20][20];

bool safe(int x, int y){
    return x < h && y < w;
}

void move(int x, int y){
    myMap[x][y] = '#';
    moved++;
    if (myMap[x+1][y] == '.' && safe(x+1, y)) move(x+1, y);
    if (myMap[x-1][y] == '.' && safe(x-1, y)) move(x-1, y);
    if (myMap[x][y+1] == '.' && safe(x, y+1)) move(x, y+1);
    if (myMap[x][y-1] == '.' && safe(x, y-1)) move(x, y-1);
    return;
}

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

	for (int i = 0; i < h; i++){
	    for (int j = 0; j < w; j++){
		cin >> myMap[i][j];
		if (myMap[i][j] == '@'){
		    initX = i; initY = j;
		}
	    }
	}

	move(initX, initY);

	cout << moved << endl;
    }
}

C++: #include<iostream>
#include<cstring>
typedef long long ll;
typedef long double ld;
using namespace std;
ll dx[] = {1, -1, 0, 0};
ll dy[] = { 0,0,1,-1 };
ll W, H;
ll cnt;
char field[100][100];
void dfs(ll y, ll x) {
	field[y][x] = '#';
	for (ll k = 0; k < 4; k++) {
		ll ny = y + dy[k];
		ll nx = x + dx[k];
		if (ny >= 0 && ny <H && nx >= 0 && nx < W) {
			if (field[ny][nx] == '.')
			{
				cnt++;
				dfs(ny, nx);
			}
		}
	}
	return;
}
int main() {
	while (cin >> W >> H) {
		if (W == 0 && H == 0) { break; }
		cnt = 1;
		for (ll i = 0; i < H; i++) {
			cin >> field[i];
		}
		for (ll i = 0; i < H; i++) {
			for (ll j = 0; j < W; j++) {
				if (field[i][j] == '@') {
					dfs(i, j);
				}
			}
		}
		cout << cnt << endl;
	}
}