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
08-162015?????????????????°???????????????????????????
pair???
#endif

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

int w,h;
int tot=1; //?????£????????????(?????????????????????)
const int dx[]={1,0,-1,0}, dy[]={0,-1,0,1}; //??????????§??????¨
char P[100][100]; //??§?¨?????????????????????¨???

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

void bfs(int x,int y){ //???????????¢?´¢
	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])){
				tot+=1;
				q.push(make_pair(cur.first+dx[i],cur.second+dy[i]));
				P[cur.first+dx[i]][cur.second+dy[i]]='#'; //???????????£?????´???????°???????
			}
		}
		P[cur.first][cur.second]='#';
	}
}

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

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

int w,h;
string room[20];

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

inline bool isRegion(int x, int y){
    return x >= 0 && y >= 0 && x < w && y < h;
}

int dfs(int x, int y){
    int cnt = 1;
    for(int i = 0; i < 4; i++){
        int nx = x + dx[i], ny = y + dy[i];
        
        if(isRegion(nx,ny) && room[ny][nx] == '.'){
            room[ny][nx] = '#';
            cnt += dfs(nx, ny);
        }
    }
    return cnt;
}

int main(int argc, const char * argv[]) {
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    while(1){
        cin >> w >> h;
        if(!w && !h) break;
        
        int sx, sy;
        for(int i = 0; i < h; i++){
            cin >> room[i];
            if(room[i].find('@') != -1){
                sx = room[i].find('@');
                sy = i;
            }
        }
        
        cout << dfs(sx, sy) << endl;
        
    }
}

C++: #include "bits/stdc++.h"
using namespace std;
#define mod 1000000007
#define all(c) begin(c),end(c)
template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
using ll = long long;
using vi = vector<int>; using vs = vector<string>;
int dd[] = { 0, 1, 0, -1, 0 }; //→↓←↑

bool B[25][25]; int N, M;
void dfs(int ty, int tx, vs S)
{
	for (int r = 0; r < 4; r++)
	{
		int y = ty + dd[r];
		int x = tx + dd[r + 1];
		if (y < 0 || y >= N || x < 0 || x >= M)
		{
			continue;
		}
		if (S[y][x] == '.' && B[y][x] == false)
		{
			B[y][x] = true;
			dfs(y, x, S);
		}
	}
}

void solve()
{
	while (true)
	{
		cin >> M >> N;
		if (N == 0 && M == 0)
		{
			break;
		}

		vs S(N);
		for (int i = 0; i < N; i++)
		{
			cin >> S[i];
		}
		int sy = -1, sx = -1;
		for (int i = 0; i < N; i++)
		{
			for (int j = 0; j < M; j++)
			{
				if (S[i][j] == '@')
				{
					sy = i;
					sx = j;
					break;
				}
				B[i][j] = false;
			}
		}

		dfs(sy, sx, S);
		int ans = 0;
		for (int i = 0; i < N; i++)
		{
			for (int j = 0; j < M; j++)
			{
				if (B[i][j])
				{
					ans++;
				}
			}
		}
		cout << (ans+1) << endl;
	}

}

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

C++: #include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>
#include <numeric>
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

const long long MAX = 5100000;
const long long INF = 1LL << 60;
const long long mod = 1000000007LL;
//const long long mod = 998244353LL;

using namespace std;
typedef unsigned long long ull;
typedef long long ll;

int dh[4] = { 1,0,-1,0 };
int dw[4] = { 0,1,0,-1 };
int main()
{
	/*
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	*/
	while (1) {
		int H, W; scanf("%d %d", &W, &H);
		if (H == 0 && W == 0) break;
		vector<string> vs(H); for (ll i = 0; i < H; i++) cin >> vs[i];
		int sh, sw;
		for (int i = 0; i < H; i++) {
			for (int j = 0; j < W; j++) {
				if (vs[i][j] == '@') {
					sh = i;
					sw = j;
				}
			}
		}
		queue<pair<int, int>> q;
		q.emplace(sh, sw);
		vs[sh][sw] = '#';
		int res = 1;
		while (!q.empty()) {
			int h, w; tie(h, w) = q.front();
			q.pop();

			for (int i = 0; i < 4; i++) {
				int nh = h + dh[i];
				int nw = w + dw[i];
				if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue;
				if (vs[nh][nw] == '#') continue;
				res++;
				vs[nh][nw] = '#';
				q.emplace(nh, nw);
			}
		}
		printf("%d\n", res);
	}
	return 0;
}

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

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

int a,b,ans;
int q[20+5][20+5];
char p[20+5][20+5];

using namespace std;
int main() {
	queue<pair<int,int> > M;
	pair<int,int> X;
	while(cin >> a >> b){
		for (int i=1; i<=21; i++){
			for (int j=1; j<=21; j++){
				q[i][j] = 0;
				p[i][j] = ' ';
			}
		}
		if (a==0) return 0;
		ans = 0;
		for (int i=1; i<=b; i++){
			for (int j=1; j<=a; j++){
				cin >> p[i][j];
				if (p[i][j] == '@'){
					M.push(make_pair(i,j));
					ans = 1;
					q[i][j] = 1;
				}
			}
		}
	

		while(!M.empty()){
			a = M.front().first;
			b = M.front().second;
			M.pop();
			for (int i=0; i<=3; i++){		
				if (p[a+dx[i]][b+dy[i]]=='.' && q[a+dx[i]][b+dy[i]]==0){
					M.push(make_pair(a+dx[i],b+dy[i]));
					q[a+dx[i]][b+dy[i]] = 1;
					ans+=1;
				}
			}
		}
		cout << ans << endl;
	}
}