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<stdio.h>
#include<string.h>
int flag[20][20];//?????°?????????0
char map[20][21];//y:x
int count;
int H,W;
void search(int nx,int ny);
main(){
    scanf("%d%d",&W,&H);
    while(H){
        memset(flag,0,sizeof(flag));
        memset(map,0,sizeof(map));
        for(int i = 0;i < H;i++){
            scanf("%s",map[i]);
        }
        for(int i = 0;i < H;i++){
            for(int j = 0;j < W;j++){
                if(map[i][j] == '@'){
                    count = 0;
                    search(j,i);
                    printf("%d\n",count);
                }
            }
        }
        scanf("%d%d",&W,&H);
    }
}
void search(int nx,int ny){
    if(nx<0 || ny<0 || nx>W-1 || ny>H-1){
        return;
    }
    if(map[ny][nx] == '#'){
        return;
    }
    if(flag[ny][nx] == 0){
        flag[ny][nx]++;
        count++;
        search(nx+1,ny);
        search(nx-1,ny);
        search(nx,ny+1);
        search(nx,ny-1);
    }
}

C++: #include <algorithm>
#include <cmath>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <cassert>
#include <functional>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
//#define LOG(...)
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define RSORT(c) sort((c).rbegin(),(c).rend())
#define CLR(a) memset((a), 0 ,sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

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

struct UnionFind {
	vector<int> v;
	UnionFind(int n) : v(n)  { for (int i = 0; i < n; i++) v[i] = i; }
	int  find(int x)         { return v[x] == x ? x : v[x] = find(v[x]); }
	void unite(int x, int y) { v[find(x)] = find(y); }
};

int main() {
	int w, h;
	while (cin >> w >> h, w){
		vector<string> cell(h);
		int sx=-1, sy;
		REP(i, h){
			cin >> cell[i];
			if (sx==-1)
			REP(j, w){
				if (cell[i][j] == '@'){
					sx = j;
					sy = i;
					cell[i][j] = '#';
				}
			}
		}
		queue<pair<int, int>> Q;
		Q.push(pair<int, int>(sx, sy));
		int ans = 0;
		while (!Q.empty()){
			ans++;
			pair<int, int> p = Q.front(); Q.pop();
			REP(i, 4){
				if (0 <= p.first + dx[i] && p.first + dx[i] < w && 0 <= p.second + dy[i] && p.second + dy[i] < h&&cell[p.second + dy[i]][p.first + dx[i]] == '.'){
					cell[p.second + dy[i]][p.first + dx[i]] = '#';
					Q.push(pair<int, int>(p.first + dx[i], p.second + dy[i]));
				}
			}
		}
		cout << ans << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define P pair<ll,ll>
#define FOR(I,A,B) for(ll I = (A); I < (B); ++I)
#define FORR(I,A,B) for(ll I = (B-1); I >= (A); --I)
#define SORT(x) (sort(x.begin(),x.end())) // 0 2 2 3 4 5 8 9
#define REV(x) (reverse(x.begin(),x.end())) //reverse
ll gcd(ll a,ll b){if(a<b)swap(a,b);if(a%b==0)return b;else return gcd(b,a%b);}
ll lcm(ll a,ll b){ll c=gcd(a,b);return ((a/c)*(b/c)*c);}//saisyo kobaisu
#define NEXTP(x) next_permutation(x.begin(),x.end())
const ll INF=1e18+7;
const ll MOD=1e9+7;

bool solve(){
	ll H,W;
	cin >> W >> H;
	ll dx[] = {0,1,0,-1};
	ll dy[] = {1,0,-1,0};
 	if(H==0&&W==0)return false;
	string tile[H];
	bool did[H][W];
	FOR(i,0,H)FOR(j,0,W)did[i][j]=false;
	FOR(i,0,H)cin >> tile[i];
	queue<P> q;
	ll ans = 0;
	FOR(i,0,H)FOR(j,0,W){
		if(tile[i][j]=='@'){
			q.push({i,j});
			ans++;
			did[i][j]=true;
		}else if(tile[i][j]=='#'){
			did[i][j]=true;
		}
	}
	while(not q.empty()){
		ll x = q.front().first;
		ll y = q.front().second;
		q.pop();
		FOR(i,0,4){
			if(x+dx[i]<H && x+dx[i]>=0 && y+dy[i]<W && y+dy[i]>=0){
				if(not did[x+dx[i]][y+dy[i]]){
					did[x+dx[i]][y+dy[i]] = true;
					ans++;
					q.push({x+dx[i],y+dy[i]});
				}
			}
		}
	}
	cout << ans << endl;
	return true;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	while(1){
		if(not solve())break;
	}
}

C++: #include <iostream>
using namespace std;
int ch[23][23],cnt;
int dfs(int,int);
int main(){
  int w,h,i,j,k,l,result;
  string in[21];
  while(1){
    cnt=0;
    cin >> w >> h;
    if(w==0&&h==0) break;
    for(i=0;i<h;i++){
      cin >> in[i];
    }
    for(i=0;i<h+2;i++){
      for(j=0;j<w+2;j++){
	ch[i][j]=1;
      }
    }
    for(i=0,k=1;k<h+1;i++,k++){
      for(j=0,l=1;l<w+1;l++,j++){
	if(in[i][j]=='.') ch[k][l]=0;
	else if(in[i][j]=='#') ch[k][l]=1;
	else ch[k][l]=2;
      }
    }
    for(i=1;i<h;i++){
      for(j=1;j<w;j++){
	if(ch[i][j]==2) result=dfs(i,j);
      }
    }
    cout << cnt << endl;
  }
  return 0;
}
int dfs(int i,int j){
  if(ch[i][j]==1) return 0;
  ch[i][j]=1;
  cnt++;
  dfs(i+1,j);
  dfs(i-1,j);
  dfs(i,j-1);
  dfs(i,j+1);
  return cnt;
}

C++: #include<iostream>
#include<vector>

using namespace std;

int w;
int h;
string map[20];
bool come[20][20];

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

int dfs(int x, int y)
{
    if(x < 0 || y < 0 || w <= x || h <= y)return 0;
    if(come[y][x])return 0;
    if(map[y][x] == '#')return 0;

    come[y][x] = true;

    int ret = 1;
    for(int i = 0; i < 4;i++)
    {
        ret += dfs(x + dx[i], y + dy[i]);
    }
    return ret;
}

int main()
{
    while( cin >> w >> h, (w | h) )
    {
        
        string gomi;
        getline(cin, gomi);//??°?????????

        int px, py;

        //input
        for (int i = 0; i < h; i++)
        {
            getline(cin, map[i]);
            for(int j = 0; j < w; j++)
            {
                come[i][j] = false;
                if(map[i][j] == '@')
                {
                    px = j;
                    py = i;
                }
            }
        }
        cout << dfs(px, py) << endl;
    }
    return 0;
}