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<queue>

using namespace std;
vector<int> ans;
typedef pair <int, int> P;

int  main(){
  int W,H;
  cin >> W >> H;
  int tate[4] = {0,1,0,-1};
  int yoko[4] = {1,0,-1,0};
  while(W){
    int count = 0;
    queue < P > zahyou;
    vector< vector<int> > room(H, vector<int>(W));
    for(int i = 0 ; i < H ; i++){
      for(int j = 0 ; j < W ; j++){
        char a;
        cin >> a;
        if(a == '.')room[i][j] = 1;
        else if(a == '@'){
          zahyou.push( P (i,j) );
          room[i][j] = 1;
        }
        else room[i][j] = 0;
      }
    }
    while(zahyou.size()){
      pair<int, int> now = zahyou.front();
      zahyou.pop();
      if(room[now.first][now.second] == 1){
        count++;
        room[now.first][now.second] = 0;
        for(int i = 0; i < 4 ; i++){
          zahyou.push( P (now.first+tate[i], now.second+yoko[i]));
        }
      }
    }
    ans.push_back(count);
    cin >> W >> H;
  }
  for(int i = 0 ; i < ans.size(); i++)
    cout << ans[i] << endl;
}

C++: #include <bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef vector<int>    VI;
typedef vector<bool>   VB;
typedef vector<string> VS;

#define MP make_pair
#define PB push_back
#define ALL(x)  (x).begin(),(x).end()
#define rep(i, N)  for (int i = 0; i < (int)(N); i++)
#define REP(i, init, N)  for (int i = (init); i < (int)(N); i++)
#define DUMP( x ) cerr << #x << " = " << ( x ) << endl
#define RANGE(x, min, max) min <= x && x <= max

int w, h;
VS tbl;

const int DX[] = {-1, 0, 1, 0}, DY[] = {0, -1, 0, 1};

int dfs(int y, int x) {
	int res = 1;
	tbl[y][x] = '#';
	rep (i, 4) {
		int ny = y + DY[i], nx = x + DX[i];
		if (RANGE(ny, 0, h - 1) && RANGE(nx, 0, w - 1) && tbl[ny][nx] == '.') {
			res += dfs(ny, nx);
		}
	}
	return res;
}

int main(void) {
	while (1) {
		tbl.clear();
		cin >> w >> h;
		if (w == 0 && h == 0) break;
		rep (i, h) {
			string s;
			cin >> s;
			tbl.PB(s);
		}
		int sy, sx;
		rep (i, h) rep (j, w) {
			if (tbl[i][j] == '@') {
				sy = i;
				sx = j;
				break;
			}
		}
		cout << dfs(sy, sx) << endl;	
	}
    return 0;
}

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

bool used[25][25];
int s;
int n,m;

void check(int x,int y){
	if(x<0 || n<=x || y<0 || m<=y)return;
	if(used[x][y])return;
	used[x][y]=true,s++;
	check(x+1,y);
	check(x-1,y);
	check(x,y+1);
	check(x,y-1);
}

int main(){
	int i,j,k;
	int a,b,c;
	int x,y;
	char ca[26];
	while(cin>>m>>n){
		if(!n)break;
		memset(used,false,sizeof(used));
		for(i=0;i<n;i++){
			cin>>ca;
			for(j=0;j<m;j++){
				if(ca[j]=='#')used[i][j]=true;
				if(ca[j]=='@')x=i,y=j;
			}
		}
		s=0;
		check(x,y);
		cout<<s<<endl;
	}
}

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

struct UnionFind {
  vector< int > data;
  UnionFind(int sz) {
    data.assign(sz, -1);
  }
  int Size(int x) {
    return(-data[Find(x)]);
  }
  int Find(int k) {
    return(data[k] < 0 ? k : data[k] = Find(data[k]));
  }
  void Unite(int a, int b) {
    a = Find(a), b = Find(b);
    if(a == b) return;
    data[a] += data[b];
    data[b] = a;
  }
};

int main() {
  int W, H;
  while(cin >> W >> H, W) {
    string mas[50];
    for(int i = 0; i < H; i++) cin >> mas[i];
    UnionFind uf(W * H);
    for(int i = 0; i < H; i++) {
      for(int j = 1; j < W; j++) {
        if(mas[i][j - 1] != '#' && mas[i][j] != '#') uf.Unite(i * W + j - 1, i * W + j);
      }
    }
    for(int i = 1; i < H; i++) {
      for(int j = 0; j < W; j++) {
        if(mas[i - 1][j] != '#' && mas[i][j] != '#') uf.Unite(i * W - W + j, i * W + j);
      }
    }
    for(int i = 0; i < H; i++) {
      for(int j = 0; j < W; j++) {
        if(mas[i][j] == '@') cout << uf.Size(i * W + j) << endl;
      }
    }
  }
}

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

int w, h;
char tile[20][20];
int ans = 0;
int dx[4] = { 0, 0, 1, -1 };
int dy[4] = { 1, -1, 0, 0 };

void solve(int x, int y)
{
	ans++;
	tile[y][x] = '#';
	for (int i = 0; i < 4; i++)
	{
		int X = x + dx[i];
		int Y = y + dy[i];
		if (tile[Y][X] == '.' && 0 <= X && X < w && 0 <= Y && Y < h)
		{
			solve(X, Y);
		}
	}
}

int main()
{

	while (cin >> w >> h, w + h != 0){
		
		ans = 0;

		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				cin >> tile[i][j];
			}
		}
		
		for (int i = 0; i < h; i++)
		{
			for (int j = 0; j < w; j++)
			{
				if (tile[i][j] == '@') solve(j, i);
			}
		}

		cout << ans << endl;
	}

	return 0;
}

C++: #include<iostream>
#include<climits>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<functional>
#include<stack>
#include<list>
#include<string>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) for(int i=0;i<(n);i++)
#define INF INT_MAX/3
#define ALL(a) (a).begin(),(a).end()
#define PII pair<int,int>
#define PCC pair<char,char>
#define PCI pair<char,int>
#define PIC pair<int,char>
#define MP make_pair
#define DEBUG(x) cout<<#x<<": "<<x<<endl;
char MAP[20][20];
int ans;
int dx[4] = { 1, 0, -1, 0 };
int dy[4] = { 0, 1, 0, -1 };
void dfs(int x, int y,int w,int h) 
{
	if (x < 0 || x>=w || y < 0 || y>=h)return;
	else if (MAP[y][x] == '#')return;
	else
	{
		MAP[y][x] = '#';
		::ans++;
		for (int r = 0; r < 4; r++)
		{
			int nx = x + dx[r];
			int ny = y + dy[r];
			dfs(nx, ny,w,h);
		}
	}
}
int main()
{
	while (1)
	{
		int w, h;
		::ans = 0;
		cin >> w >> h;
		if (w == 0)break;
		PII self;
		REP(y, h)
		{
			REP(x, w)
			{
				cin >> MAP[y][x];
				if (MAP[y][x] == '@')self = MP(y, x);
			}
		}
		dfs(self.second, self.first,w,h);
		cout << ::ans << endl;
	}
	return 0;
}