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++: //#define _GRIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;

// ?????¬???????????¬??????
#pragma region MACRO
#define P(x) cout << (x) << endl
#define p(x) cout << (x)
#define PED cout << "\n"
#define rep(i,n) for(int i=0; i<(int)n; ++i)
#define REP(i,x,n) for(int i=x; i<(int)n; ++i)
#define repi(i,n) for(int i=0; i<=(int)n; ++i)
#define REPI(i,x,n) for(int i=x; i<=(int)n; ++i)
#define ILP while(true)
#define FOR(i,c) for(__typeof((c).begin())!=(c).begin(); i!=(c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
#define mp make_pair
#pragma endregion

// ???
#pragma region TYPE_DEF
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<string, string> pss;
typedef pair<string, int> psi;
typedef pair<int, string> pis;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long double> vld;
typedef vector<long> vl;
typedef vector<long long> vll;
typedef vector<string> vs;
#pragma endregion

// Effective std
#pragma region ESTD
template<typename C, typename T> int count(C& c, T t) { return count(ALL(c), t); }
template<typename C, typename F> int count_if(C& c, F f) { return count_if(ALL(c), f); }
template<typename C, typename T> void erase(C& c, T t) { remove(ALL(c), t), c.end(); }
template<typename C> void remove(vector<C>& c, unsigned int index) { c.erase(c.begin()+index); }
template<typename C, typename T, typename U> void replace(C& c, T t, U u) { replace(ALL(c), t, u); }
template<typename C, typename F, typename U> void replace_if(C& c, F f, U u) { (ALL(c), f, u); }
template<typename C> void reverse(C& c) { reverse(ALL(c)); }
template<typename C> void sort(C& c) { sort(ALL(c)); }
template<typename C, typename Pred> void sort(C& c, Pred p) { sort(ALL(c), p); }
#pragma endregion

// ?????°
#pragma region CONST_VAL
constexpr int PI = (2*acos(0.0));
constexpr int EPS = (1e-9);
constexpr int MOD = (int)(1e9+7);
constexpr int INF = 100000000;
#pragma endregion

int w, h;
char a[21][21];
int dx[]{1,-1,0,0};
int dy[]{0,0,1,-1};
int cnt;

void dfs(int x, int y) {
    a[x][y] = 0;
    rep(i,4) {
        int nx = x + dx[i], ny = y + dy[i];
        if(0<=nx&&nx<h&&0<=ny&&ny<w&&a[nx][ny]=='.') {
            cnt++;
            dfs(nx, ny);
        }
    }
}

int main() {
    while(cin>>w>>h, w) {
        cnt = 1;
        rep(i,h) cin >> a[i];
        rep(i, h) rep(j,w) {
            if(a[i][j]=='@') {
                dfs(i,j);
                break;
            }
        }
        cout << cnt << endl;
    }
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<math.h>
#include<string>
#include<limits.h>
#define ll long long
#define rep(i,n) for(int i=0;i<n;i++)
#define nrep(i,a,b) for(int i=a;i<b;i++)
#define mrep(i,a) for(int i=a;i<=0;i++)
#define INF LLONG_MAX
using namespace std;

// .:true #:false
vector<pair<ll,ll> > idou(4);
ll w, h, ans;

void dfs(vector<vector<bool> > &map,ll tate, ll yoko) {
	ans++;
	map[tate][yoko] = false;
	ll t, y;
	rep(i, 4) {
		t = tate+idou[i].first;
		y = yoko+idou[i].second;
		if (0 <= t && t < h && 0 <= y && y < w && map[t][y]) {
			dfs(map,t, y);
		}
	}
}

int main() {

	//???
	idou[0].first = -1;
	idou[0].second = 0;
	//???
	idou[1].first = 0;
	idou[1].second = 1;
	//???
	idou[2].first = 1;
	idou[2].second = 0;
	//???
	idou[3].first = 0;
	idou[3].second = -1;

	while (1) {
		ans = 0;
		cin >> w >> h;
		if (!w && !h) break;
		vector<vector<bool> > map(h, vector<bool>(w));
		ll s_t, s_y;
		rep(i, h) {
			string s; cin >> s;
			rep(j, w) {
				if (s[j] == '@') {
					s_t = i;
					s_y = j;
					map[i][j] = true;
				}
				else map[i][j] = (s[j] == '.');
			}
		}dfs(map,s_t, s_y);
		cout << ans << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;
int dx[] = { -1, 0, 1, 0 };
int dy[] = { 0, -1, 0, 1 };

int main(){
	int W, H;
	while( cin >> W >> H, W, H ){
		char room[20][20];
		queue< pair<int, int> > xy;
		for( int i = 0; i < H; i++ ){
			for( int j = 0; j < W; j++ ){
				cin >> room[i][j];
				if( room[i][j] == '@' ){ xy.push(pair<int, int>(i, j)); }
			}
		}
		//cout << xy.front().first << ' ' << xy.front().second << endl;
		int sum = 1;
		while( !xy.empty() ){
			for( int i = 0; i < 4; i++ ){
				int x = xy.front().second + dx[i];
				int y = xy.front().first + dy[i];
				//cout << room[y][x];
				if( room[y][x] == '.' ){
					room[y][x] = '#';
					xy.push(pair<int, int>(y, x));
					sum++;
				}
			}
			xy.pop();
		}
		cout << sum << 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};
int serch(int n,int m,int y,int x)
{
	int ans=0;
	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(n,m,ay,ax)+1;
	return ans;
}
int main() {
	int n,m;
	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(n,m,i,j)+1<<endl;
	}
}

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

class P {
	public:
		int x;
		int y;
		P(int x, int y) :x(x),y(y) {}
};

char map[20][20];

int bfs(int W, int H) {
	queue<P> q;
	bool ch[H][W];
	for (int i=0; i<H; i++)
	for (int j=0; j<W; j++)
		ch[i][j] = false;
	for (int i=0; i<H; i++)
	for (int j=0; j<W; j++)
		if (map[i][j] == '@') {
			q.push(P(j,i));
			ch[i][j] = true;
			break;
		}
	while(!q.empty()) {
		P c = q.front();
		q.pop();
		if (c.x+1<W && (!ch[c.y][c.x+1]) && map[c.y][c.x+1]=='.')
		{ q.push(P(c.x+1, c.y)); ch[c.y][c.x+1]=true; }
		if (c.y+1<H && (!ch[c.y+1][c.x]) && map[c.y+1][c.x]=='.')
		{ q.push(P(c.x, c.y+1)); ch[c.y+1][c.x]=true; }
		if (c.x-1>=0 && (!ch[c.y][c.x-1]) && map[c.y][c.x-1]=='.')
		{ q.push(P(c.x-1, c.y)); ch[c.y][c.x-1]=true; }
		if (c.y-1>=0 && (!ch[c.y-1][c.x]) && map[c.y-1][c.x]=='.')
		{ q.push(P(c.x, c.y-1)); ch[c.y-1][c.x]=true; }
	}
	int cnt = 0;
	for (int i=0; i<H; i++)
	for (int j=0; j<W; j++)
		if (ch[i][j]) cnt++;
	return cnt;
}

int main() {
	int n, m;
	while (cin >> n >> m, (n||m)) {
		for (int i=0; i<m; i++)
		for (int j=0; j<n; j++)
			cin >> map[i][j];
		int res = bfs(n, m);
		cout << res << endl;
	}
	return 0;
}