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<cstdio>
#include<cstdlib>
#include<cmath>
#include<climits>
#include<iostream>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<list>
#include<map>
#include<set>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
#define pb push_back
int dy[]={0, 0, 1, -1, 0};
int dx[]={1, -1, 0, 0, 0};
 
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
char room[100][100];
bool fin[100][100];

int dfs(int i,int j){
    int sum = 1;

    fin[i][j] = false;

    for(int k = 0;k < 4;k++){
        int ii = i + dy[k];int jj = j + dx[k];
        if(fin[ii][jj] && room[ii][jj] == '.')
            sum += dfs(ii,jj);
    }
    return sum;
}

int main(){
    int w,h;
    while(1){
        scanf("%d%d",&w,&h);

        if(w == 0 && h == 0)
            break;

        for(int i = 0;i < h;i++)
            scanf("%s",&room[i][0]);

        for(int i = 0;i < h;i++)
            for(int j = 0;j < w;j++)
                fin[i][j] = true;

        for(int i = 0;i < h;i++){
            for(int j = 0;j < w;j++){
                if(room[i][j] == '@')
                    printf("%d\n",dfs(i,j));
            }
        }
    }
    return 0;
}

C++: #include <iostream>
#include <string.h>

using namespace std;
char S[21][21];
int count = 0;

void a(int i, int j){
	if(S[i][j] == '.'){
		count++;
		S[i][j] = '#';
		a(i - 1, j);
		a(i + 1, j);
		a(i, j + 1);
		a(i, j - 1);
	}
}


int main(){
	while(true){
		memset(S,'#',sizeof(S));
		count = 0;
		int x, y;
		cin >> x >> y;
		if(x == 0 && y == 0){
			break;
		}
		for(int i = 0; i < y; i++){
			for(int j = 0; j < x; j++){
				cin >> S[i][j];
			}
		}

		int startx, starty;
		for(int i = 0; i < y; i++){
			for(int j = 0; j < x; j++){
				if(S[i][j] == '@'){
					startx = j;
					starty = i;
					S[i][j] = '.';
					break;
				}
			}
		}
		a(starty, startx);
		cout << count << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
string place[20];
int dx[4]={0,1,0,-1},dy[4]={-1,0,1,0},n,m;
int serch(int y,int x)
{
	int ans=1;
	place[y][x]='#';
	for(int i=0,ax,ay;i<4;i++)if((ay=dy[i]+y)<m&&(ax=dx[i]+x)<n&&ay>=0&&ax>=0&&place[ay][ax]=='.')ans+=serch(ay,ax);
	return ans;
}
int main() {
	while(cin>>n>>m,n,m){
		for(int i=0;i<m;i++)cin>>place[i];
		for(int i=0;i<m;i++)for(int j=0;j<n;j++)if(place[i][j]=='@')cout<<serch(i,j)<<endl;
	}
}

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

#define rep(i,n) for(int i=0; i<n; i++)

int W, H;
vector<string> mp;
int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};

void solve(int x, int y){
  if(x<0 || H<=x || y<0 || W<=y || mp[x][y]!='.')return;
  mp[x][y]='A';
  rep(i,4)solve(x+dir[i][0], y+dir[i][1]);
}

int main(){
  while(true){
    cin>>W>>H;
    if(W==0&&H==0)break;
    mp.clear();
    rep(i,H){
      string s;
      cin>>s;
      mp.push_back(s);
    }
    int sx, sy;
    rep(i,H)rep(j,W)if(mp[i][j]=='@'){sx=i; sy=j; break;}
    mp[sx][sy]='.';
    solve(sx, sy);

    int cnt=0;
    rep(i,H)rep(j,W)if(mp[i][j]=='A')cnt++;
    cout<<cnt<<endl;
  }
  return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <bitset>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,n) for (int i=(n)-1;i>=0;i--)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define REPS(i,f,n) for (int i=(f)-1;i>=(n);i--)
int dx[4] = {1, 0, 0, -1};
int dy[4] = {0, 1, -1, 0};

char board[21][21];
int w, h;
int x, y;// ?????¨?????????

void slove(void){
	int cnt = 0;
	queue<pint> q;
	q.push(make_pair(y, x));//?????¨????????????queue?????\??????

	while(!q.empty()){
		pint now = q.front(); q.pop();
		rep(i, 4){
			int nowy = now.fi + dy[i];
			int nowx = now.se + dx[i];
			if (0 <= nowy <= h - 1 && 0 <= nowx && nowx <= w - 1){
				if (board[nowy][nowx] == '.'){
					cnt++;
					board[nowy][nowx] = 0;
					q.push(make_pair(nowy, nowx));
				}
			}
		}
	}
	printf("%d\n", cnt + 1);//?§?????????´????????\??????
	return;
}

void scan(int w, int h){
	rep(i, h){
		rep(j, w){
			cin >> board[i][j];
			if (board[i][j] == '@'){
				y = i; x = j;
			}
		}
		getchar();
	}
	/*
	rep(i, h){
		rep(j, w){
			printf("%c", board[i][j]);
		}
		printf("\n");
	}
	*/
	slove();
	return;
}

int main(void){
	rep(i, 1000){
		cin >> w >> h;

		if (w == 0) break;
		scan(w, h);
	}
	return 0;
}

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

int rec(string field[], int x, int y, int w, int h){
    int count=1;
    int f=0;
    field[y][x] = ',';
    if(x+1<w && field[y][x+1] == '.'){
        count += rec(field, x+1, y, w, h);
    }
    if(y+1<h && field[y+1][x] == '.'){
        count += rec(field, x, y+1, w, h);
    }
    if(x-1>=0 && field[y][x-1] == '.'){
        count += rec(field, x-1, y, w, h);
    }
    if(y-1>=0 && field[y-1][x] == '.'){
        count += rec(field, x, y-1, w, h);
    }
    return count;
}

int main(){
    int w, h;
    string field[20];
    int x, y;
    cin >> w >> h;
    while(w || h){
        x = string::npos;
        for(int i=0;i<h;++i){
            cin >> field[i];
            if(x==string::npos){
                x = field[i].find('@');
                y = i;
            }
        }
        cout << rec(field, x, y, w, h) << endl;
        cin >> w >> h;
    }
}