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

using namespace std;

#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define RREP(i,n) for(int (i)=(int)(n)-1;(i)>=0;(i)--)
#define REMOVE(Itr,n) (Itr).erase(remove((Itr).begin(),(Itr).end(),n),(Itr).end())
#define PB_VEC(Itr1,Itr2) (Itr1).insert((Itr1).end(),(Itr2).begin(),(Itr2).end())
#define UNIQUE(Itr) sort((Itr).begin(),(Itr).end()); (Itr).erase(unique((Itr).begin(),(Itr).end()),(Itr).end())

typedef long long ll;

vector<string> M;

int main(){

    
    while(true){
    int w,h;
    cin>>w>>h;
        
        if(w==0&&h==0)break;
        
    M.clear();
        
    REP(i,h){
        string s; cin>>s;
        M.push_back(s);
    }
    
    int x,y = 0;
    for(x=0;x<w;x++){
        for(y=0;y<h;y++){
            if(M[y][x]=='@')goto nexts;
        }
    }
    
nexts:;
    
    int ans=0;
    stack<pair<int,int>> sta;
    sta.push(make_pair(x,y));
    while(sta.size()!=0){
        int xt=sta.top().first,yt=sta.top().second;
        sta.pop();
        
        if(M[yt][xt]=='.'){
            M[yt][xt]= '#';
            ans++;
        }
        
        if(xt<w-1)if(M[yt][xt+1]=='.')sta.push(make_pair(xt+1,yt));
        if(xt>0)if(M[yt][xt-1]=='.')sta.push(make_pair(xt-1,yt));
        if(yt<h-1)if(M[yt+1][xt]=='.')sta.push(make_pair(xt,yt+1));
        if(yt>0)if(M[yt-1][xt]=='.')sta.push(make_pair(xt,yt-1));
        
    }
    
    cout<<ans+1<<endl;
    }
    
    return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <functional>
#include <utility>
using namespace std;
#define MOD 1000000007
#define ADD(X,Y) ((X) = ((X) + (Y)%MOD) % MOD)
typedef long long i64; typedef vector<int> ivec; typedef vector<string> svec; typedef pair<int, int> pi;
int dd[] = { 0, 1, 0, -1 };


int main()
{
	while (true)
	{
		int W, H;
		cin >> W >> H;
		if (W == 0 && H == 0) break;

		char C[30][30];
		int B[30][30];

		int sx = -1; int sy = -1;
		for (int i = 0; i < H; i++)
		{
			for (int j = 0; j < W; j++)
			{
				cin >> C[i][j];
				B[i][j] = 0;
				if (C[i][j] == '@')
				{
					sy = i;
					sx = j;
				}
			}
		}

		queue<pi> Q;
		Q.push(make_pair(sy, sx));
		B[sy][sx] = 1;
		int ans = 1;
		while (!Q.empty())
		{
			int ty = Q.front().first; int tx = Q.front().second;
			Q.pop();

			for (int r = 0; r < 4; r++)
			{
				int y = ty + dd[r];
				int x = tx + dd[r + 1];
				if (y < 0 || y >= H || x < 0 || x >= W)continue;
				if (C[y][x] == '#')continue;
				if (B[y][x])continue;
				ans++;
				B[y][x] = 1;
				Q.push(make_pair(y, x));
			}
		}

		cout << ans << endl;
	}
	return 0;
}

C++: #include <iostream>

using namespace std;

char table[21][21];
bool reachable;
int cnt;
int w, h;

void dfs(int x, int y) {
    if (table[y][x] == '@')
        reachable = true;
    table[y][x] = '#';
    for (int dy = -1; dy <= 1; dy++) {
        for (int dx = -1; dx <= 1; dx++) {
            if (dx*dy != 0 || x+dx < 0 || x+dx >= w || y+dy < 0 || y+dy >= h)
                continue;
            if (table[y+dy][x+dx] != '#') {
                cnt++;
                dfs(x+dx, y+dy);
            }
        }
    }
}

int main()
{
    start:
    while (cin >> w >> h, w|h) {
        // input
        for (int i = 0; i < h; i++) {
            cin >> table[i];
        }
        // dfs
        for (int j = 0; j < h; j++) {
            for (int i = 0; i < w; i++) {
                if (table[j][i] != '#') {
                    reachable = false;
                    cnt = 1;
                    dfs(i, j);
                    if (reachable) {
                        cout << cnt << endl;
                        goto start;
                    }
                }
            }
        }
    }

    return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;
const int dx[] = { -1, 0, 1, 0 };
const int dy[] = { 0, -1, 0, 1 };

int main(){
	int W, H;
	while( cin >> W >> H, W, H ){
		char room[20][20];
		queue< pair<int, int> > xy;
		for( int i = 0; i < H; i++ ){
			for( int j = 0; j < W; j++ ){
				cin >> room[i][j];
				if( room[i][j] == '@' ){
					xy.push(pair<int, int>(i, j));
					room[i][j] = '#';
				}
			}
		}
		int sum = 1;
		while( !xy.empty() ){
			for( int i = 0; i < 4; i++ ){
				int x = xy.front().second + dx[i];
				int y = xy.front().first + dy[i];
				if( 0 <= x && x < W && 0 <= y && y < H && room[y][x] == '.' ){
					room[y][x] = '*';
					xy.push(pair<int, int>(y, x));
					sum++;
				}
			}
			xy.pop();
		}
		cout << sum << endl;
	}
}

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

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

typedef pair<int,int> P;

int bfs(int, int, int, int, char **);

int main()
{
	int w, h, sx, sy;
	char **map;

	while( cin >> w >> h && w > 0)
	{
		fflush(stdin);

		map = new char *[h];
		for(int f1 = 0; f1 < h; f1++)
		{
			map[f1] = new char [w+1];
			scanf("%s",map[f1]);
			for(int f2 = 0; f2 < w; f2++)
			{
				if(map[f1][f2] == '@')
				{
					sx = f2;
					sy = f1;
				}
			}
		}
		cout << bfs(w, h, sx, sy, map) << endl;
	}

	return 0;
}

int bfs(int w, int h, int sx, int sy, char **map)
{
	int px, py, cnt = 1;

	queue<P> que;
	que.push(P(sy,sx));
	while(!que.empty())
	{
		P p = que.front(); que.pop();
		for(int f1 = 0; f1 < 4; f1++)
		{
			py = p.first + dy[f1];
			px = p.second + dx[f1];
			if(px >= 0 && px < w && py >= 0 && py < h && map[py][px] == '.')
			{
				que.push(P(py,px));
				map[py][px] = '#';
				cnt++;
			}
		}
	}
	return cnt;
}