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
????????£????????????1h
??????????????????????????????????????????????????§?????????
#endif

#include <iostream>
#include <queue>
#include <string.h>
#include <utility>
using namespace std;

int w,h;
const int dx[]={1,0,-1,0}, dy[]={0,-1,0,1}; //??????????§??????¨
char P[30][30]; //??§?¨?????????????????????¨???

bool valid(int x,int y){ //??????????????????
	return (x>=0)&&(x<w)&&(y>=0)&&(y<h)&&(P[x][y]!='#');
}

void bfs(int x,int y){ //???????????¢?´¢
	int tot=1; //?????£????????????(?????????????????????)
	queue<pair<int,int> > q;
	pair<int,int> p;
	p = make_pair(x,y); //pair??????????????¨??????????????????
	q.push(p);
	while(!q.empty()){
		pair<int,int> cur=q.front(); //?????¨??°?????????????????????
		q.pop();
		for(int i=0;i<4;++i){
			if(valid(cur.first+dx[i],cur.second+dy[i])){
				q.push(make_pair(cur.first+dx[i],cur.second+dy[i]));
				tot++;
				P[cur.first+dx[i]][cur.second+dy[i]]='#'; //???????????£?????´???????°???????
			}
		}
		P[cur.first][cur.second]='#';
	}
	cout << tot <<endl;
}

int main(){
	int x1,y1;
	while(cin>>w>>h){
		if(w==0&&h==0)
			break;
		for(int j=0;j<h;++j){
			for(int i=0;i<w;++i){
				cin>>P[i][j];
				if(P[i][j]=='@')
					x1=i; y1=j; 
			}
		}
		bfs(x1,y1); //??????????????°????????£??\????????¢?´¢????§?
	}
}

C++: #include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>
using namespace std;
typedef pair<int, int> P;
int w, h;
string f[20];
int dx[4]={-1,1,0,0};
int dy[4]={0,0,-1,1};
bool isin(int x, int y) { return x>=0&&x<h&&y>=0&&y<w; }
int main() {
    while (scanf("%d%d",&w,&h)) {
        if (!w&&!h) break;
        int sx=-1,sy=-1;
        for (int i=0; i<h; i++) {
            cin>>f[i];
            for (int j=0; j<w; j++) if (f[i][j]=='@') {
                sx=i; sy=j;
                f[i][j]='#';
            }
        }
        queue<P> que;
        que.push(P(sx,sy));
        int res=1;
        while (!que.empty()) {
            int x=que.front().first, y=que.front().second;
            que.pop();
            for (int i=0; i<4; i++) {
                int nx=x+dx[i], ny=y+dy[i];
                if (isin(nx,ny)&&f[nx][ny]=='.') {
                    que.push(P(nx,ny));
                    f[nx][ny]='#';
                    res++;
                }
            }
        }
        printf("%d\n",res);
    }
}

C++: #include <iostream>
#include <queue>
using namespace std;
int dx[] = { -1, 0, 1, 0 };
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] = '#';
				}
			}
		}
		//cout << xy.front().first << ' ' << xy.front().second << endl;
		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];
				//cout << room[y][x];
				if( 0 <= x && x < W && 0 <= y && y < H && room[y][x] == '.' ){
					room[y][x] = '*';
					xy.push(pair<int, int>(y, x));
					sum++;
				}
			}
			//cout << endl;
			xy.pop();
		}
		/*for( int i = 0; i < H; i++ ){
			for( int j = 0; j < W; j++ ){
				cout << room[i][j];
			}
			cout << endl;
		}*/
		cout << sum << endl;
	}
}

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

using namespace std;

char map[22][22];
int h,w,cnt,dx[4]={0,0,1,-1},dy[4]={1,-1,0,0};

void search(int x,int y){
	cnt++;
	map[y][x] = '#';
	for(int i = 0; i < 4; i++){
		if(map[y+dy[i]][x+dx[i]] == '.')
			search(x+dx[i],y+dy[i]);
	}
}

int main()
{	
	while(scanf("%d%d",&w,&h) && h != 0 && w != 0){
		for(int i = 1; i <= h; i++){
			scanf("%s",&map[i][1]);
		}
		for(int i = 0; i <= h+1; i++){
			map[i][0] = '#';
			map[i][w+1] = '#';
		}
		for(int j = 0; j <= w+1; j++){
			map[0][j] = '#';
			map[h+1][j] = '#';
		}
		/*
		for(int i = 0; i <= h+1; i++){
			for(int j = 0; j <= w+1; j++){
				printf("%c",map[i][j]);
			}
			printf("\n");
		}
		*/
		cnt  = 0;
		for(int i = 1; i <= h; i++){
			for(int j = 1; j <= w; j++){
				if(map[i][j] == '@')
					search(j,i);
			}
		}
		printf("%d\n",cnt);
	}

	return 0;
}

C++: #include <stdio.h>
#include <iostream>
#include <math.h>
#include <string>
#include <vector>

using namespace std;
#define ll long long
#define pb push_back

// 前に&をつけるとvectorの参照渡しになる
int solve(int x, int y, vector<vector<char> > tile, int H, int W, int count, vector<vector<bool> >& flag) {
    // cout << count << ":" << y << ","<< x << endl;
    count++;
    if (!flag[y][x]) {
        return count;
    } else {
        flag[y][x] = false;
    }
    if (y + 1 < H) {
        if (tile[y + 1][x] == '.' && flag[y + 1][x]) {
            count = solve(x, y + 1, tile, H, W, count, flag);
        }
    }
    if (y - 1 >= 0) {
        if (tile[y - 1][x] == '.' && flag[y - 1][x]) {
            count = solve(x, y - 1, tile, H, W, count, flag);
        }
    }
    if (x + 1 < W) {
        if (tile[y][x + 1] == '.' && flag[y][x + 1]) {
            count = solve(x + 1, y, tile, H, W, count, flag);
        }
    }
    if (x - 1 >= 0) {
        if (tile[y][x - 1] == '.' && flag[y][x - 1]) {
            count = solve(x - 1, y, tile, H, W, count, flag);
        }
    }
    return count;
}

int main() {

    int H, W;
    while(true) {
        cin  >> W >> H;
        if (W==0 && H == 0) break;
        vector<vector<char> > tile(H, vector<char>(W));
        vector<vector<bool> > flag(H, vector<bool>(W, true));
        int x, y, count = 0;

        for (int i = 0; i < H; i++) {
            for (int j = 0; j < W; j++) {
                cin >> tile[i][j];
                if (tile[i][j] == '@') {
                    x = j;
                    y = i;
                }
            }
        }

        count = solve(x, y, tile, H, W, count, flag);
        cout << count << endl;
    }

}