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

using namespace std;

int solve( int w, int h, vector< vector<char> > &v );
void dfs( int W, int H, int w_index, int h_index, vector< vector<char> > &V, int &ans );

int main(int argc, char const *argv[]) {
    vector< vector<char> > vec;

    int w, h;
    vector<int> sums;

    while (1) {
        cin >> w >> h;

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

        vec.resize(w);
        for (int i = 0; i < w; i++) {
            vec[i].resize(h);
        }

        for (int i = 0; i < w; i++) {
            for (int j = 0; j < h; j++) {
                cin >> vec[i][j];
            }
        }

        sums.push_back( solve( w, h, vec ) );
    }

    for( auto it = sums.begin(); it != sums.end(); it ++ ){
        cout << *it << endl;
    }

    return 0;
}

int solve( int w, int h, vector< vector<char> > &v ){
    int w_index, h_index, total;
    int ans = 0;

    for (auto it = v.begin(); it != v.end(); it++ ) {
        auto iter = find(it->begin(), it->end(), '@');

        if(iter!=it->end()){
            w_index = distance( v.begin(), it );
            h_index = distance( it->begin(), iter );
        }
    }

    dfs( w, h, w_index, h_index, v, ans );

    return ans;
}

void dfs( int W, int H, int w_index, int h_index, vector< vector<char> > &V , int &ans ){
    int dx[4] = { 1, 0, -1, 0 };
    int dy[4] = { 0, 1, 0, -1 };
    V[w_index][h_index] = '#';

    for( int i = 0; i < 4; i++ ){
        int nx = w_index + dx[i];
        int ny = h_index + dy[i];

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

    return;
}

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

using namespace std;

#define H 20

string field[H];
int w, h; 

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

int no_black = 0;
void count(int x, int y) {
    if (x < 0 || w <= x || y < 0 || h <= y || field[y][x] == '#') return;
    field[y][x] = '#'; no_black++;
    for (int i = 0; i < 4; i++) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        count(nx, ny);
    }
}

void init() {
    no_black = 0;
}

int main() {
    int x, y;
    while (cin >> w >> h) {
        if (w == 0 && h == 0) break;
        init();
        for (int i = 0; i < h; i++) {
            cin >> field[i];
            for (int j = 0; j < w; j++) {
                if (field[i][j] == '@') {
                    x = j;
                    y = i;
                }
            }
        }
        count(x, y);
        cout << no_black << endl;
    }
    return 0;
}

C++: #include <iostream>

using namespace std;

#define MAX_H 22
#define MAX_W 22

int W, H, X, Y, A;

char tile[MAX_H][MAX_W];

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

void dfs(int x, int y)
{
	tile[y][x] = '@';

	for (int i = 0; i < 4; i++)
	{
		if (0 <= x + dx[i] <= W - 1 && 0 <= y + dy[i] <= H - 1)
		{
			if (tile[y + dy[i]][x + dx[i]] == '#')
			{
				dfs(x + dx[i], y + dy[i]);
			}
		}
	}
}

int main()
{
	while (true)
	{
		cin >> W >> H;

		if (W == 0 && H == 0) { break; }

		for (int i = 0; i < H; i++)
		{
			for (int j = 0; j < W; j++)
			{
				cin >> tile[i][j];

				if (tile[i][j] == '@')
				{
					X = j;
					Y = i;
				}
			}
		}

		dfs(X, Y);

		A = 0;

		for (int i = 0; i < H; i++)
		{
			for (int j = 0; j < W; j++)
			{
				if (tile[i][j] == '@')
				{
					A++;
				}
			}
		}

		cout << A << endl;
	}

	return 0;
}

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

#define for_(i,a,b) for(int i=(a);i<(b);++i)

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

int W, H, ans;
string grid[22];

int rec(int x, int y) {
	int res = 1;
	
	grid[y][x] = '#';
	
	for_(d,0,4) {
		int nx = x + dx[d], ny = y + dy[d];
		if (nx < 0 || W <= nx || ny < 0 || H <= ny) continue;
		if (grid[ny][nx] == '#') continue;
		res += rec(nx, ny);
	}
	
	return res;
}

int main() {
	while (cin >> W >> H, W) {
		for_(i,0,H) cin >> grid[i];
		
		for_(i,0,H) for_(j,0,W) if (grid[i][j] == '@') cout << rec(j, i) << endl;
	}
}

C++: #include <bits/stdc++.h>
#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--)

#define PB push_back
#define MP make_pair
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define CLR(a) memset(a,0,sizeof(a))
#define SET(a,c) memset(a,c,sizeof(a))

#define DEBUG(x) cout<<"#x"<<": "<<x<<endl

using namespace std;

typedef long long int ll;
typedef vector<int> vi;
typedef vector<ll> vl;

const ll INF = INT_MAX/3;
const ll MOD = 1000000007;
const double EPS = 1e-14;
const int dx[] = {1,0,-1,0} , dy[] = {0,1,0,-1};

int main(){

    while(true){
	int h,w;
	cin >> w >> h;
	if(w == 0 && h == 0) break;
	vector< vector<char> > board(h+2,vector<char>(w+2,'#'));
	int sx,sy;
	int ans = 0;
	REP(i,h)REP(j,w){
	    cin >> board[i+1][j+1];
	    if(board[i+1][j+1] == '@'){
		sx = j+1;
		sy = i+1;
		board[sy][sx] = '#';
		ans++;
	    }
	}
	queue<int> qx;
	queue<int> qy;
	qx.push(sx);
	qy.push(sy);
	while(!qx.empty()){
	    int x = qx.front();
	    int y = qy.front();
	    qx.pop();
	    qy.pop();
	    REP(i,4){
		int nx = x + dx[i];
		int ny = y + dy[i];
		if(board[ny][nx] == '.'){
		    board[ny][nx] = '#';
		    qx.push(nx);
		    qy.push(ny);
		    ans++;
		}
	    }
	}
	cout << ans << endl;
    }

    return 0;
}