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>
using namespace std;
int W,H;
int x[4]={0,0,1,-1};
int y[4]={1,-1,0,0};
int i,j;
int count=0;
char room[100][100];

int dfs(int, int);

int main(){
  while(1){
    int sy,sx;
    scanf("%d %d",&W,&H);
    if(W==0&&H==0)break;
    for(i = 0 ; i < H ; i++){
      scanf("%c",&room[i][0]);
      for(j = 0 ; j < W ; j++){
	scanf("%c",&room[i][j]);
	if(room[i][j] == '@'){
	  sy = i;
	  sx = j; 
	}
      }
    }
    printf("%d\n", dfs(sx,sy));
  }
  return 0;
}

int dfs(int ax,int ay){
  if(ax<0||ay<0)return 0;
  if(ax>=W||ay>=H)return 0;
  if(room[ay][ax]=='#')return 0;
  room[ay][ax]='#';
  int cnt=1;
  int i;
  for(i=0;i<4;i++){
    int nx=ax+x[i];
    int ny=ay+y[i];
    cnt+=dfs(nx,ny);
  }
  return cnt;
}

C++: #include<iostream>
#include <cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<utility>
#include<queue>

#define REP(i,n) for(int i = 0;i < n ;i++)
#define pa pair<int,int>
#define pq queue< pair<int,int> >
	

using namespace std;

int w,h;
char mp[22][22];
bool dp[22][22];
int dx[]={1,0,-1,0},dy[] = {0,1,0,-1};
int sx,sy;
int cou;

int main()
{
	for(;;)
	{
		cin >> w >>h;
		if(w == 0 && h == 0) break;
		cou = 1;sx = 0;sy = 0;
		REP(i,22)REP(j,22)dp[i][j] = 0;
		
		REP(i,h)REP(j,w)
		{
			cin >> mp[i][j];
			if(mp[i][j] == '@'){sx = i;sy = j;}
		}
		
		pq p;
		p.push(make_pair(sx,sy));
		dp[sx][sy] = 1;
//		cout << sx << ' '<<sy<<endl;
		while(!p.empty())
		{
			pa t = p.front();p.pop();
			REP(i,4)
			{
				int cx = t.first+dx[i],cy = t.second+dy[i];
				if(cx >= 0 && cx < h && cy >= 0 && cy < w)
				if( dp[cx][cy] == 0&& mp[cx][cy] != '#')
				{
//					cout << cx << ' ' <<cy << endl;
					p.push(make_pair(cx,cy));
					dp[cx][cy] = 1;
					cou++;
				}
			}
		}
		
			
		
		cout << cou << endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,n) for(ll i=0;i<n;++i)
#define rrep(i,n) for(int i=n-1;i>=0;--i)
#define yesno(flg) if(flg){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}
#define MAX_N 1002
#define i197 1000000007
#define Vi vector<int>
#define Vl vector<long long>
#define pb(a) push_back(a)

using namespace std;

typedef long long ll;
typedef pair<ll,ll> P1;
typedef pair<int,int> Pi;
typedef pair<double,Pi> Pdi;
typedef pair<ll,int>Pli;
typedef pair<P1,ll> P2;
const ll INF=10000000000000000001;
int dy[]={0, 0, 1, -1};
int dx[]={1, -1, 0, 0};
struct Road{double cost;int a,b;};
struct edge{int to,cost;};
struct pos{
	int x,y;
};

int main() {

	while(1){
		int h,w;
		char c[21][21]={};
		int dp[21][21]={},ans=0;
		cin>>h>>w;
		if(h==0&&w==0)return 0;
		queue<pos> que;
		int si=0,sj=0;
		rep(i,w){
			rep(j,h){
				cin>>c[j][i];
				if(c[j][i]=='@'){
					dp[j][i]=1;
					sj=j;
					si=i;
				}
			}
		}
		pos pos1={sj,si};
		que.push(pos1);
		while(!que.empty()){
			pos pos2=que.front();que.pop();
			ans++;
			if(c[pos2.x+1][pos2.y]=='.'&&dp[pos2.x+1][pos2.y]==0){
				pos pos3={pos2.x+1,pos2.y};
				que.push(pos3);
				dp[pos2.x+1][pos2.y]=1;
			}
			if(c[pos2.x-1][pos2.y]=='.'&&dp[pos2.x-1][pos2.y]==0){
				pos pos4={pos2.x-1,pos2.y};
				que.push(pos4);
				dp[pos2.x-1][pos2.y]=1;
			}
			if(c[pos2.x][pos2.y+1]=='.'&&dp[pos2.x][pos2.y+1]==0){
				pos pos5={pos2.x,pos2.y+1};
				que.push(pos5);
				dp[pos2.x][pos2.y+1]=1;
			}
			if(c[pos2.x][pos2.y-1]=='.'&&dp[pos2.x][pos2.y-1]==0){
				pos pos6={pos2.x,pos2.y-1};
				que.push(pos6);
				dp[pos2.x][pos2.y-1]=1;
			}
		}
		cout<<ans<<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>
#include <string>
using namespace std;

string s[21];
int w, h, vx[4] = {1, 0, -1, 0}, vy[4] = {0, 1, 0, -1};
bool is_search[21][21];

int dfs(int x, int y) {
  //cout << x << " " << y << endl;
  is_search[x][y] = true;
  int ret = 0, cnt = 0;
  for(int i = 0; i < 4; ++i) {
    if(0 <= x+vx[i] && 0 <= y+vy[i] && x+vx[i] < h && y+vy[i] < w && s[x+vx[i]][y+vy[i]] != '#' && is_search[x+vx[i]][y+vy[i]] == false) {
      ret += dfs(x+vx[i], y+vy[i]);
      cnt++;
    }
  }
  //cout << ret+cnt << endl;
  return ret + cnt;
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int ans, sx, sy;
  bool flag;

  while(cin >> w >> h, w | h) {
    ans = 0; flag = false;
    for(int i = 0; i < h; ++i) fill(is_search[i], is_search[i] + w, false);
    for(int i = 0; i < h; ++i) cin >> s[i];

    for(int i = 0; i < h; ++i) {
      for(int j = 0; j < w; ++j) {
        if(s[i][j] == '@') {
          sx = i; sy = j;
          flag = true;
          break;
        }
      }
      if(flag) break;
    }

    ans = dfs(sx, sy) + 1;

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