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 <string>
#include <iterator>
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>

using namespace std;

string GetStr()
{
	string str;
	getline(cin,str);
	return str;
}

vector<int> GetIntVector()
{
	string str;
	getline(cin,str);
	istringstream line( str );

	istream_iterator<int> iit(line);
	istream_iterator<int> iit_eof;

	vector<int> ret;

	for(;iit != iit_eof; iit++){
		ret.push_back(*iit);
	}

	return ret;
}

int counter;

void GoNext(vector<string>& lines,int hh,int hw)
{
	counter++;
	lines[hh][hw] = '#';

	if(hw > 0 && lines[hh][hw-1] != '#')
		GoNext(lines,hh,hw-1);

	if(hw < (int)lines[hh].size() - 1 && lines[hh][hw+1] != '#')
		GoNext(lines,hh,hw+1);

	if(hh > 0 && lines[hh-1][hw] != '#')
		GoNext(lines,hh-1,hw);

	if(hh < (int)lines.size() - 1 && lines[hh+1][hw] != '#')
		GoNext(lines,hh+1,hw);
}

void solve(int w,int h)
{
	vector<string> lines;
	for(int i = 0; i < h; i++){
		lines.push_back(GetStr());
	}

	int humanw,humanh = -1;
	for(int i = 0; i < h; i++){
		for(int j = 0; j < w; j++){
			if(lines[i][j] == '@'){
				humanh = i; humanw = j;
				break;
			}
		}
		if(humanh != -1) break;
	}

	counter = 0;

	GoNext(lines,humanh,humanw);

	cout << counter <<endl;
}

void solve1130()
{
	vector<int> v = GetIntVector();
	while(v[0] != 0 && v[1] != 0){
		solve(v[0],v[1]);
		 v = GetIntVector();
	}
}


int main()
{
	solve1130();

	return 0;
}

C++: #include<iostream>
#define MAX (256)
using namespace std;

int N, M;
char field[MAX][MAX];
int sx, sy;
int ans = 0;

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

void dfs (int x, int y){
	if(0 <= x && x < N && 0 <= y && y < M && field[x][y] == '.'){
		field[x][y] = '#';
		ans++;
		
		for(int i =0; i < 4; i++){
			int nx = x + dx[i], ny = y + dy[i];
			dfs(nx, ny);
		
		}
	}
}

int main(){

	while(1){	
	cin >> M >> N;
	
	if(M == 0 && N == 0) break;
	
	for(int i =0; i < N; i++){
		for(int j =0; j < M; j++){
			cin >> field[i][j];
		}
	}
	
	for(int i =0; i < N; i++){
		for(int j =0; j < M; j++){
			if(field[i][j] == '@'){
				sx = i;
				sy = j;
				field[sx][sy] = '.';
			}
		}
	}
		
	dfs(sx, sy);
	
	cout << ans << endl;
	}
	return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS // #pragma warning(disable:4996)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <functional>
#include <sstream>
#include <cmath>
#include <set>
#include <map>
using namespace std; 

#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define all(a) a.begin(),a.end()
#define len(x) ((int)(x).size())
#define tmax(a,b,c) max((a),max((b),(c)))
#define tmin(a,b,c) min((a),min((b),(c)))
#define debug(x) cerr << #x << " is " << x << endl;

typedef pair<int, int> Pii;
typedef vector<int> Vi;
typedef vector<vector<int> > VVi;
typedef long long ll;
const int inf = 2e9;
const ll ll_inf = 1e17;
const int mod = 1e9 + 7;
const long double eps = 1e-10;

int dy[4] = {1,0,-1,0}; // n e s w
int dx[4] = {0,1,0,-1};
char m[21][21];
int cnt;

void bfs(int sx, int sy, int w, int h)
{
	rep(i,0,4) {
		int nx = sx + dx[i];
		int ny = sy + dy[i];
		if (ny < 0 || nx < 0 || ny >= h || nx >= w) continue;
		if (m[ny][nx] == '.') {
			m[ny][nx] = '@';
			cnt++;
			bfs(nx, ny, w, h);
		}
	}
}

int main()
{
	while(1) {
		int w, h;
		cin >> w >> h;
		if (!w && !h) break;
		int sx, sy;
		cnt = 0;
		rep(i,0,h) rep(j,0,w) {
			cin >> m[i][j];
			if (m[i][j] == '@') {
				cnt++;
				sx = j;
				sy = i;
			}
		}
		//rep(i,0,h) { rep(j,0,w) cout << m[i][j]; cout << endl; }
		bfs(sx, sy, w, h);
		cout << cnt << endl;
	}	

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)   FOR(i,0,n)
#define pb emplace_back
typedef long long ll;
typedef pair<int,int> pint;

string s[21];
bool used[21][21];
const int dx[]={-1,0,1,0},dy[]={0,-1,0,1};
int main(){
    int w,h;
    while(cin>>w>>h,w){
        int sx,sy,cnt=0;
        rep(i,h){
            cin>>s[i];
            rep(j,w)if(s[i][j]=='@') sx=j,sy=i;
        }
        memset(used,0,sizeof(used));
        queue<pint> q;
        q.push({sx,sy});
        used[sy][sx]=true;
        while(!q.empty()){
            pint pi=q.front();q.pop();
            ++cnt;
            rep(i,4){
                int tx=pi.first+dx[i],ty=pi.second+dy[i];
                if(tx>=0&&ty>=0&&tx<w&&ty<h&&s[ty][tx]=='.'&&!used[ty][tx]){
                    q.push({tx,ty});
                    used[ty][tx]=true;
                }
            }
        }
        cout<<cnt<<endl;
    }
    return 0;
}

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

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

char grid[25][25];

void init()
{
	for (int i = 0; i < 25; i++){
		for (int j = 0; j < 25; j++){
			grid[i][j] = '#';
		}
	}
}

int search(int ny, int nx)
{
	grid[ny][nx] = '#';
	
	int sum = 1;
	for (int i = 0; i < 4; i++){
		int ty = ny + dy[i];
		int tx = nx + dx[i];
		if (grid[ty][tx] == '.'){
			if (grid[ty][tx] == '.'){
				sum += search(ty, tx);
			}
		}
	}
	
	return (sum);
}

int main()
{
	int h, w;
	while (scanf("%d %d", &w, &h), w + h){
		init();
		
		int sy, sx;
		for (int i = 1; i <= h; i++){
			for (int j = 1; j <= w; j++){
				scanf(" %c", &grid[i][j]);
				
				if (grid[i][j] == '@'){
					sy = i;
					sx = j;
				}
			}
		}
		
		printf("%d\n", search(sy, sx));
	}
	
	return 0;
}