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 <vector>
#include <string>
using namespace std;

typedef  vector< vector<int> > vector2d;

int main() {
  
    int H,W,count=0,kaisuu=0,kosuu,sum,judge;
    char map[20][20];
    vector2d nextx,nexty; //nextx[回数][個数]
    nextx.reserve(10000);
    nexty.reserve(10000);

  while(1){
    cin >>W>>H;
    if (W==0&&H==0)break;
    
    //マップの入力
    for(int cH=0;cH<H;cH++){
	for(int cW=0;cW<W;cW++){
	  cin >> map[cW][cH];
	  if (map[cW][cH]=='@'){
	    nextx[kaisuu].push_back(cW);
	    nexty[kaisuu].push_back(cH);
	  }
	}
    }
    sum = 1;
    while(1){
      judge = 0;
      for(kosuu=0;kosuu<nextx[kaisuu].size();kosuu++){
	if (nextx[kaisuu][kosuu]-1!=-1){
	  if(map[ nextx[kaisuu][kosuu]-1 ][ nexty[kaisuu][kosuu] ]=='.'){  //@の左側をチェック
	    //もし'@'だったらその座標を保存しそれを@に！
	    nextx[kaisuu+1].push_back(nextx[kaisuu][kosuu]-1);
	    nexty[kaisuu+1].push_back(nexty[kaisuu][kosuu]);
	    map[ nextx[kaisuu][kosuu]-1 ][ nexty[kaisuu][kosuu] ]='@';
	    sum++;
	    judge++;
	  }
	}
	if (nexty[kaisuu][kosuu]-1!=-1){
	  if(map[ nextx[kaisuu][kosuu] ][ nexty[kaisuu][kosuu]-1 ]=='.'){  //@の上側をチェック
	    //もし'@'だったらその座標を保存しそれを@に！
	    nextx[kaisuu+1].push_back(nextx[kaisuu][kosuu]);
	    nexty[kaisuu+1].push_back(nexty[kaisuu][kosuu]-1);
	    map[ nextx[kaisuu][kosuu] ][ nexty[kaisuu][kosuu]-1 ]='@';
	    sum++;
	    judge++;
	  }
	}
	if (nextx[kaisuu][kosuu]+1!=W){
	  if(map[ nextx[kaisuu][kosuu]+1 ][ nexty[kaisuu][kosuu] ]=='.'){  //@の右側をチェック
	    //もし'@'だったらその座標を保存しそれを@に！
	    nextx[kaisuu+1].push_back(nextx[kaisuu][kosuu]+1);
	    nexty[kaisuu+1].push_back(nexty[kaisuu][kosuu]);
	    map[ nextx[kaisuu][kosuu]+1 ][ nexty[kaisuu][kosuu] ]='@';
	    sum++;
	    judge++;
	  }
	}
	if (nexty[kaisuu][kosuu]+1!=H){
	  if(map[ nextx[kaisuu][kosuu] ][ nexty[kaisuu][kosuu]+1 ]=='.'){  //@の下側をチェック
	    //もし'@'だったらその座標を保存しそれを@に！
	    nextx[kaisuu+1].push_back(nextx[kaisuu][kosuu]);
	    nexty[kaisuu+1].push_back(nexty[kaisuu][kosuu]+1);
	    map[ nextx[kaisuu][kosuu] ][ nexty[kaisuu][kosuu]+1 ]='@';
	    sum++;
	    judge++;
	  }
	}
      }
      if (judge==0)break;
      kaisuu++;
    }
    kaisuu++;
    cout << sum <<endl;
  }
  
  return 0;
  
}

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

#define rep(i,N) for(int i=0;i<(int)(N);i++)
#define REP(i,N,k) for(int i=k;i<(int)(N);i++)

#define vi vector<int>
#define pb push_back

#define pii pair<int,int>
#define fi first
#define se second
int main(){


    ifstream in("b_in.txt");
    cin.rdbuf(in.rdbuf());

    int w,h;
    while(cin >> w >> h,w){
        int sx,sy;
        string s[30];
        rep(i,h) cin >> s[i];
        rep(y,h)rep(x,w) if(s[y][x]=='@') s[y][x]='.',sx=x,sy=y;

        queue<pii> que;
        que.push(pii(sx,sy));
        bool u[30][30] = {};
        u[sy][sx] = true;
        int cnt = 1;
        while(!que.empty()){
            int x,y;
            tie(x,y) = que.front();
            que.pop();
            rep(i,4){
                const int dx[] = {0,1,0,-1,0};
                int nx = x + dx[i];
                int ny = y + dx[i+1];
                if(nx < 0 || w <= nx || ny < 0 || h <= ny) continue;
                if(s[ny][nx] == '#') continue;
                if(u[ny][nx]) continue;
                u[ny][nx] = true;
                cnt += 1;
                que.push(pii(nx,ny));
            }
        }
        cout << cnt << endl;
    }

    return 0;
}

C++: #include <iostream>
#define RED '#'
#define BLACK '.'
using namespace std;

int w,h;
char tiles[20][20];

int rc(int x,int y){
  if( x<0 || x>=h )
    return 0;
  if( y<0 || y>=w )
    return 0;
  if( tiles[x][y]==RED )
    return 0;
  tiles[x][y]=RED;

  int rtn=1;
  rtn += rc( x+1,y  );
  rtn += rc( x-1,y  );
  rtn += rc( x,y+1  );
  rtn += rc( x,y-1  );

  return rtn;
}
void pr(){
  for( int i=0;i<h;i++){
    for(int j=0;j<w;j++ )
      cout << tiles[i][j];
    cout << endl;
  }
}

int main(){
  int x,y;

  while( true ){
    cin >> w >> h;
    if( !w&&!h ) break;

    for( int i=0;i<h;i++ )
      for( int j=0;j<w;j++ ){
	cin >> tiles[i][j];
	if( tiles[i][j]=='@' ){
	  tiles[i][j]=BLACK;
	  x=i; y=j;
	}
      }
    //   pr();
    cout << rc( x,y ) << endl;
    //pr();
    //break;
  }

  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define fi first
#define se second
#define pb push_back
#define rep(i, s, n) for (int i = s; i < n; i++)
#define rrep(i, s, n) for (int i = (n)-1; i >= (s); i--)
#define all(a) a.begin(),a.end()
typedef pair<int,int>pint;
typedef vector<int>vint;
typedef vector<pint>vpint;
const long long MOD = 1000000007, INF = 1e17;
 
#define endl '\n'
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0)
 
template<class T>inline bool chmax(T& a,T b){if(a<b){a=b;return true;}return false;}
template<class T>inline bool chmin(T& a,T b){if(a>b){a=b;return true;}return false;}


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

signed main()
{
	IOS();
	int H,W;
	vector<int>ans;
	while(1){
		cin>>W>>H;
		if(H==0&&W==0)break;
		string s[H];
		rep(i,0,H)cin>>s[i];
		queue<pair<int,int>>que;
		rep(i,0,H){
			rep(j,0,W){
				if(s[i][j]=='@')que.push({i,j});
			}
		}
		vector<vector<int>>visit(H,vector<int>(W,0));
		while(!que.empty()){
			int i = que.front().fi;
			int j = que.front().se;
			que.pop();
			if(visit[i][j])continue;
			visit[i][j] = true;
			rep(k,0,4){
				int ni = i + dx[k];
				int nj = j + dy[k];
				if(ni<0||nj<0||H<=ni||W<=nj)continue;
				if(s[ni][nj]=='#')continue;
				if(visit[ni][nj])continue;
				que.push({ni,nj});
			}
		}
		int res = 0;
		rep(i,0,H)rep(j,0,W)if(visit[i][j])res++;
		ans.push_back(res);
	}
	for(auto e:ans){
		cout<<e<<endl;
	}
}