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<stdio.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<deque>
#include<stack>
#include<string>
#include<string.h>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#include<stdlib.h>
#include<cassert>
#include<time.h>
#include<bitset>

int main(){
  while(1){
    int h,w;
    cin>>w>>h;
    if(h==0&&w==0) break;
    char a[h][w];
    queue<pair<int,int>> q;
    bool used[h][w];
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
        cin>>a[i][j];
        used[i][j]=0;
        if(a[i][j]=='@') q.push(make_pair(i,j));
      }
    }
    int ans=0;
    while(!q.empty()){
      pair<int,int> p=q.front();
      q.pop();
      int x=p.first, y=p.second;
      if(used[x][y]) continue;
      used[x][y]=1;
      ans++;
      if(x>0){
        if(!used[x-1][y]&&a[x-1][y]=='.'){
          q.push(make_pair(x-1,y));
        }
      }
      if(x<h-1){
        if(!used[x+1][y]&&a[x+1][y]=='.'){
          q.push(make_pair(x+1,y));
        }
      }
      if(y>0){
        if(!used[x][y-1]&&a[x][y-1]=='.'){
          q.push(make_pair(x,y-1));
        }
      }
      if(y<w-1){
        if(!used[x][y+1]&&a[x][y+1]=='.'){
          q.push(make_pair(x,y+1));
        }
      }
    }
    cout<<ans<<endl;
  }
  return 0;
}

C++: /*
螺旋本 p371

circumcenter:
    https://ja.wikipedia.org/wiki/%E5%A4%96%E6%8E%A5%E5%86%86#%E5%A4%96%E5%BF%83%E3%81%AE%E4%BD%8D%E7%BD%AE
    https://mathtrain.jp/goshin
*/
#pragma GCC optimize("Ofast")
#include <cstdio>
#include <cstdlib>
#include <string>
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#include <bitset>
#include <cmath>
#include <limits>
#include <iostream>
#include <map>
#include <set>
#include <tuple>
using namespace std;
#define INF 1LL << 55
#define MAX 1LL << 30
#define MOD 1000000007
typedef long long ll;
typedef pair<int,int> P;
//typedef pair<pair<int,int>,int> p;
#define bit(n, k) ((n >> k) & 1) /*nのk bit目*/
#define rad_to_deg(rad) (((rad) / 2 / M_PI) * 360)
template<class T,class U>bool chmin(T&a,const U&b){if(a<=b)return false;a=b;return true;}
template<class T,class U>bool chmax(T&a,const U&b){if(a>=b)return false;a=b;return true;}
//__builtin_popcount(S);
//C.erase(unique(C.begin(),C.end()),C.end());
//#define int ll
int dx[]={1,-1,0,0},dy[]={0,0,-1,1};

signed main(){
    while(true){
        int W,H;
        cin>>W>>H;
        if(H==0 && W==0) return 0;
        char C[23][23];
        int sh,sw;
        for(int i=1;i<=H;i++){
            for(int j=1;j<=W;j++){
                cin>>C[i][j];
                if(C[i][j]=='@'){
                    sh=i;sw=j;
                }
            }
        }
        queue<P> que;
        int count=1;
        que.push(P(sh,sw));
        while(!que.empty()){
            P x=que.front(); que.pop();
            for(int i=0;i<4;i++){
                int nh=x.first+dx[i],nw=x.second+dy[i];
                if(nh>=1 && nh<=H && nw>=1 && nw<=W && C[nh][nw]=='.'){
                    count++;
                    C[nh][nw]='#';
                    que.push(P(nh,nw));
                }
            }
        }
        cout<<count<<endl;
    }
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cstdio>
#include <queue>
#include <map>

#define rep(i, n) for(int i = 0; i < n; i++)
#define FOR(i, a, b) for(int i = a; i < b; i++)
#define all(v) (v).begin(), (v).end()

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> P;

void p(vi v){
	rep(i, v.size()){
		cout << v[i] << ' ';
	}
	cout << endl;
}

void pp(vector<string> v){
	rep(i, v.size()){
		cout << v[i] << endl;
	}
}

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

const char wall = '#';

int seedfill(vector<string> &v, P start){
	queue<P> q;
	q.push(start);
	int count = 0;
	while(!q.empty()){
		P p = q.front();
		q.pop();
		int x = p.first;
		int y = p.second;

		if(v[y][x] != wall){
			v[y][x] = wall;
			count++;
			rep(i, 4){
				q.push(P(x+dx[i], y+dy[i]));
			}
		}
	}
	return count;
}

int solve(vector<string> &v){
	rep(i, v.size()){
		rep(j, v[i].size()){
			if(v[i][j] == '@'){
				return seedfill(v, P(j, i));
			}
		}
	}
	return -1;
}

int main(){

	int W, H;
	while(cin >> W >> H){
		if(!W && !H) break;

		vector<string> field(H+2);
		rep(i, W+2) field[0].push_back(wall);
		field[H+1] = field[0];

		FOR(i, 1, H+1){
			string tmp;
			cin >> tmp;
			field[i] = wall + tmp + wall;
		}

		cout << solve(field) << endl;
	}

	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;
using ll = long long ;
using P = pair<int,int> ;
using pll = pair<long long,long long>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e17;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979323846;
vector<int> dx = {0,0,1,-1};
vector<int> dy = {1,-1,0,0};
int h,w;
char maze[23][23];
bool seen[23][23];

int id(int i,int j){
    return i + j * h;
}

void dfs(int now){
    int i = now % h;
    int j = now / h;
    if(seen[i][j]) return;
    seen[i][j] = true;

    rep(k,4){
        int next_i = i + dx[k];
        int next_j = j + dy[k];
        if(0 <= next_i && next_i < h && 0 <= next_j && next_j < w){
            if(seen[next_i][next_j]) continue;
            if(maze[next_i][next_j] == '#') continue;
            dfs(id(next_i,next_j));
        }
    }

    return;
}

int main(){
    while(1){
        cin >> w >> h;
        if(w==0 && h==0) break;
        rep(i,h)rep(j,w) seen[i][j] = false;
        rep(i,h)rep(j,w) cin >> maze[i][j];
        int si, sj;
        rep(i,h)rep(j,w){
            if(maze[i][j]=='@') si = i,sj = j;
        }
        dfs(id(si,sj));
        int ans = 0;
        rep(i,h)rep(j,w){
            ans += seen[i][j];
        }
        cout << ans << endl;
    }
    return 0;
}