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<vector>
#include<list>
#include<algorithm>
#include<iostream>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
int f(int a[][22],int b[][22],int x,int y){
  int sm=0;
  if(b[x][y]==0&&a[x][y]!=-1){
    b[x][y]=1;
    sm++;
    sm+=f(a,b,x-1,y);
    sm+=f(a,b,x,y-1);
    sm+=f(a,b,x+1,y);
    sm+=f(a,b,x,y+1);
  }
  return sm;
}
int main(){
  int i,j;
  int a[22][22],b[22][22],x,y,w,h;
  char c;
  while(cin>>w>>h&&w+h){
    memset(a,-1,sizeof(a));
    for(i=0;i<h;i++){
      for(j=0;j<w;j++){
	cin>>c;
	if(c=='#')
	  a[j+1][i+1]=-1;
	else{
	  a[j+1][i+1]=0;
	  if(c=='@'){
	    x=j+1;
	    y=i+1;
	  }
	}
      }
    }
    memset(b,0,sizeof(b));
    cout<<f(a,b,x,y)<<endl;
  }
  return 0;
}

C++: #include<iostream>
#include<stack>
#include<map>

using namespace std;

int main(){
  int w, h;

  while(cin >> w >> h, !(w == 0 && h == 0)){
    char tiles[h+2][w+2];
    pair<int, int> start;

    for(int i = 0; i < h+2; i++){
      for(int j = 0; j < w+2; j++){
        if(i == 0 || j == 0 || i == h+1 || j == w+1){
          tiles[i][j] = '#';
        }else{
          cin >> tiles[i][j];
          if(tiles[i][j] == '@') start = make_pair(i, j);
        }
      }
    }

    /*for(int i = 0; i < h+2; i++){
      for(int j = 0; j < w+2; j++){

        cout << tiles[i][j];
      }
      cout << endl;
    }
    cout << "start is (" << start.first << start.second << ")" << endl;
*/
    int dh[4] = {-1,  0, 1, 0};
    int dw[4] = { 0, -1, 0, 1};

    int cnt = 0;
    stack< pair<int, int> > s;
    s.push(start);
    pair<int, int> cur;

    while(!s.empty()){
      cur = s.top();
      s.pop();
      cnt++;

      for(int i = 0; i < 4; i++){
        if(tiles[cur.first + dh[i]][cur.second + dw[i]] == '.'){
          tiles[cur.first + dh[i]][cur.second + dw[i]] = '#';
          pair<int, int> next = make_pair(cur.first + dh[i], cur.second + dw[i]);
          s.push(next);
        }
      }
    }
    cout << cnt << endl;
  }
}

C++: #include <bits/stdc++.h>
using namespace std;
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define REP(i,n) for(int i=0;i<n;i++)
#define FOR(i,n1,n2) for(int i=n1;i<n2;i++)
#define speed_up    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

typedef long long int ll;
typedef pair<ll,ll> Pi;
const int INF=(ll)(1LL<<31)-1;
const ll INFl=(ll)9223372036854775807;
const int MAX=10000;
const ll MOD=(ll)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a, b)*b;}
//int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};

template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}

int room[23][23];
int main(){
	
	while(1){
		int w,h;
		cin>>w>>h;
		if(w==0&&h==0)break;
		int sh,sw;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				char a;
				cin>>a;
				if(a=='.')room[i][j]=0;
				if(a=='#')room[i][j]=-1;
				if(a=='@'){
					room[i][j]=1;
					sh=i;sw=j;
				}
			}
		}
		int ans=1;
		queue<pair<int,int> > q;
		q.push(pair<int,int>(sh,sw) );
		while(q.size()){
			pair<int,int> pp=q.front();q.pop();
			if(pp.first>0&&room[pp.first-1][pp.second]==0){
				q.push(pair<int,int>(pp.first-1,pp.second));
				room[pp.first-1][pp.second]=1;
				ans++;
			}
			if(pp.first<h-1&&room[pp.first+1][pp.second]==0){
				q.push(pair<int,int>(pp.first+1,pp.second));
				ans++;
				room[pp.first+1][pp.second]=1;
			}
			if(pp.second>0&&room[pp.first][pp.second-1]==0){
				q.push(pair<int,int>(pp.first,pp.second-1));
				ans++;
				room[pp.first][pp.second-1]=1;
			}
			if(pp.second<w-1&&room[pp.first][pp.second+1]==0){
				q.push(pair<int,int>(pp.first,pp.second+1));
				ans++;
				room[pp.first][pp.second+1]=1;
			}
		}
		cout<<ans<<endl;
		for(int i=0;i<23;i++){
			for(int j=0;j<23;j++){
				room[i][j]=0;
			}
		}
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int ans;
vector<string> Tiles;
void counts(int NW,int NH,int W,int H){
	if(NW!=0){
		if(Tiles[NH][NW-1]=='.'){
			ans++;
			Tiles[NH][NW-1] = 'D';
			counts(NW-1,NH,W,H);
		}
	}
	if(NH!=0){
		if(Tiles[NH-1][NW]=='.'){
			ans++;
			Tiles[NH-1][NW] = 'D';
			counts(NW,NH-1,W,H);
		}
	}
	if(NW<W-1){
		if(Tiles[NH][NW+1]=='.'){
			ans++;
			Tiles[NH][NW+1] = 'D';
			counts(NW+1,NH,W,H);
		}
	}
	if(NH<H-1){
		if(Tiles[NH+1][NW]=='.'){
			ans++;
			Tiles[NH+1][NW] = 'D';
			counts(NW,NH+1,W,H);
		}
	}
	return;
}

int main(){
	int W,H;
	int FW,FH;
	cin >> W >> H;
	int loop,loop2;
	while(W){
		vector<string> (H).swap(Tiles);
		for(loop=0;loop<H;loop++){
			cin >> Tiles[loop];
		}
		for(loop=0;loop<H;loop++){
			for(loop2=0;loop2<W;loop2++){
				if(Tiles[loop][loop2]=='@'){
					FW = loop2;
					FH = loop;
					loop = H;
					break;
				}
			}
		}
		ans = 1;
		counts(FW,FH,W,H);
		cout << ans << endl;
		cin >> W >> H;
	}
	return 0;
}

C++: #include <iostream>
#define REP(i, a, n) for(int i = (a); i < (n); i++)
using namespace std;

int W, H, sx, sy;
char T[20][20];
bool visited[20][20];

bool isin(int y, int x) {
  return 0 <= y && y < H && 0 <= x && x < W;
}

int dfs(int sy, int sx) {
  int dd[5] = { 0, 1, 0, -1, 0 };
  int ret = 1;

  visited[sy][sx] = true;

  REP(i, 0, 4) {
    int ny = sy + dd[i + 0];
    int nx = sx + dd[i + 1];
    if(isin(ny, nx) && !visited[ny][nx] && T[ny][nx] != '#') ret += dfs(ny, nx);
  }

  return ret;
}

int main(void) {
  while(cin >> W >> H, W) {
    REP(i, 0, H) {
      cin >> T[i];
      REP(j, 0, W) {
        if(T[i][j] == '@') {
          sy = i;
          sx = j;
        }
      }
    }
    REP(i, 0, H) REP(j, 0, W) visited[i][j] = false;

    cout << dfs(sy, sx) << endl;
  }
}