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 <bits/stdc++.h>
using namespace std;

#define int				long long
#define FOR( i, m, n )  for( int (i) = (m); (i) < (n); (i)++ )
#define REP( i, n )     FOR( i, 0, n )
#define REPR( i, m )	for( int (i) = (m); (i) >= 0; (i)-- )
#define REPONE( i, n )	FOR( i, 1, n + 1 )
#define ALL( a )        (a).begin(), (a).end()
#define MP				make_pair
#define X				first.first
#define Y				first.second

typedef pair<int, int> P;

template<class T>bool chmax( T& a, const T& b ) { if( a < b ) { a = b; return 1; } return 0; }
template<class T>bool chmin( T& a, const T& b ) { if( a > b ) { a = b; return 1; } return 0; }

const int INF = 1e9;
const int MOD = 1e9 + 7;

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

char tile[21][21];
int W, H;
int ans;

void dfs( int x, int y ) {
	REP( i, 4 ) {
		int nx = x + dx[i];
		int ny = y + dy[i];

		if( nx >= 0 && nx < W && ny >= 0 && ny < H && tile[nx][ny] == '.' ) {
			tile[nx][ny] = '#';
			ans++;
			dfs( nx, ny );
		}
	}
}

signed main() {
	while( cin >> W >> H, W, H ) {
		
		int sx, sy;
		REP( i, H ) {
			REP( j, W ) {
				cin >> tile[j][i];
				if( tile[j][i] == '@' ) {
					sx = j;
					sy = i;
					tile[j][i] = '#';
				}
			}
		}

		
		ans = 1;
		dfs( sx, sy );
		cout << ans << endl;
		
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const ll MOD=1000000007;
const ll INF=1000000000LL;
const int MAX=100001;
const double EPS=1e-10;
struct edge{int to,cost;};
typedef pair<int,int> P;
int w,h;
char c[21][21];
bool visited[21][21];
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
bool field(int x,int y){
    if(x>=0&&y>=0&&x<w&&y<h&&(!visited[y][x])&&(c[y][x]=='.')){
        return true;
    }
    return false;
}
void dfs(int x,int y){
    visited[y][x]=true;
    for(int i=0;i<4;i++){
        int nx=x+dx[i];
        int ny=y+dy[i];
        if(field(nx,ny)){
            dfs(nx,ny);
        }
    }
    return;
}
int main(){
    while(cin>>w>>h,w+h){
        int sx,sy;
        for(int i=0;i<h;i++){
            cin>>c[i];
            for(int j=0;j<w;j++){
                if(c[i][j]=='@'){
                    sx=j;
                    sy=i;
                }
            }
        }
        int ans=0;
        dfs(sx,sy);
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(visited[i][j]){
                    ans++;
                    visited[i][j]=false;
                }
            }
        }
        cout<<ans<<endl;
    }
}

C++: #include <cstdio>
#include <utility>
#include <queue>

using namespace std;

bool input();
int bfs();

int W, H;
char tile[20][20];		//添え字は[縦][横]
const int INF = 1000;
typedef pair<int, int> P;
int sw, sh;
int d[20][20];		//各点への最短距離の配列
int dw[4] = {1, 0, -1, 0};
int dh[4] = {0, 1, 0, -1};		//左から順に　→↓←↑　を表す
int count=0;		//発見した黒いタイルの総数を記憶


int main()
{
	for (;;)
	{
		if (!input()) return 0;
		count = 0;
		int ans = bfs();
		printf("%d\n", ans);
	}
}


int bfs()
{
	// printf("bfs()\n");
	queue<P> que;
	for (int i=0; i<20; i++)
		for (int i2=0; i2<20; i2++)
			d[i][i2] = INF;
	que.push(P(sh, sw));
	d[sh][sw] = 0;
	count++;
	
	while (que.size())
	{
		P p = que.front();
		que.pop();
		
		for (int i=0; i<4; i++)
		{
			int nw = p.second + dw[i];
			int nh = p.first + dh[i];
			
			// printf("nw=%d nh=%d\n", nw, nh);
			// printf("tile[%d][%d]=%c\n", nh, nw, tile[nh][nw]);
			// printf("d[%d][%d]=%d\n", nh, nw, d[nh][nw]);
			
			if (0<=nw && nw < W && 0<=nh && nh<H && tile[nh][nw]!='#' && d[nh][nw]==INF)
			{
				que.push(P(nh, nw));
				d[nh][nw] = d[p.first][p.second] + 1;
				count++;
			}
		}
	}
	return count;
}




//１つのデータセットに対して１回呼ばれる
bool input()
{
	scanf("%d %d", &W, &H);
	// printf("W=%d H=%d\n", W, H);
	if (W==0) return false;

	for (int i=0; i<H; i++)
	{
		scanf("%s", tile[i]);
		for (int i2=0; i2<W; i2++)
		{
			if (tile[i][i2]=='@')
			{
				sw = i2;
				sh = i;
			}
		}
		// printf("tile[%d]=%s\n", i, tile[i]);
		// printf("%c\n",tile[i][5]);
	}
	
	return true;
}

C++: #include "bits/stdc++.h"

#define REP(i,n) for(int i=0;i<n;++i)
#define RREP(i,n) for(int i=n-1;i>=0;--i)
#define FOR(i,m,n) for(int i=m;i<n;++i)
#define RFOR(i,m,n) for(int i=m-1;i>=n;--i)
#define ALL(v) v.begin(),v.end()
#define PB(a) push_back(a)
#define INF 1000000001
#define MOD 1000000007

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

using namespace std;

void dfs(const vector<string> &v, vector<vector<bool>> &b, int x, int y, int& cnt) {
	b[y][x] = true;
	cnt++;
	REP(i, 4) {
		int nx = x + dx[i], ny = y + dy[i];
		if (nx<v[y].size() && nx >= 0 && ny >= 0 && ny<v.size() &&v[ny][nx] == '.' && !b[ny][nx])
			dfs(v, b, nx, ny,cnt);
	}
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int h, w;
	while (cin >> w >> h&&h) {
		vector<string> v(h);
		REP(i, h) {
			cin >> v[i];
		}
		vector<bool> b(w, false);
		vector<vector<bool> > visited(h, b);
		int y=0, x=0;
		REP(i, h) {
			if (v[i].find('@')!=size_t(-1)) {
				y = i;
				x = v[i].find('@');
			}
		}
		int cnt = 0;
		dfs(v, visited, x, y, cnt);
		cout << cnt << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<list>
using namespace std;
int sum = 0;
char a[20][20];
void solve(int h, int w,  int i,int  j) {
	if (i + 1 < h&&a[i + 1][j] == '.') {
		sum++;
		a[i + 1][j] = '#';
		solve(h, w,  i+1, j);
	}
	if (i - 1 >= 0&&a[i - 1][j] == '.') {
		sum++;
		a[i - 1][j] = '#';
		solve(h, w,  i-1, j);
	}
	if (j + 1 < w&&a[i][j+1] == '.') {
		sum++;
		a[i][j+1] = '#';
		solve(h, w,  i, j+1);
	}
	if (j - 1 >= 0&&a[i][j-1] == '.') {
		sum++;
		a[i][j-1] = '#';
		solve(h, w,  i, j-1);
	}
}
int main() {
	int h, w;
	while (cin >> w >> h&&w != 0 && h != 0) {
		string b;
		int h2, w2;
		for (int i = 0; i < h; i++) {
			cin >> b;
			for (int j = 0; j < w; j++) {
				a[i][j] = b[j];
				if (a[i][j] == '@') {
					h2 = i;
					w2 = j;
				}
			}
		}
		sum = 0;
		solve(h, w,  h2, w2);
		cout << ++sum << endl;
	}
}