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 REP(i, n) for (int i=0; i<(n); ++i)
#define RREP(i, n) for (int i=(int)(n)-1; i>=0; --i)
#define FOR(i, a, n) for (int i=(a); i<(n); ++i)
#define RFOR(i, a, n) for (int i=(int)(n)-1; i>=(a); --i)

#define SZ(x) ((int)(x).size())
#define all(x) begin(x),end(x)

#define dump(x) cerr<<#x<<" = "<<(x)<<endl
#define debug(x) cerr<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<<endl;

template<class T>
ostream &operator<<(ostream &os, const vector <T> &v) {
    os << "[";
    REP(i, SZ(v)) {
        if (i) os << ", ";
        os << v[i];
    }
    return os << "]";
}

template<class T, class U>
ostream &operator<<(ostream &os, const pair <T, U> &p) {
    return os << "(" << p.first << " " << p.second << ")";
}

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

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

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vvll = vector<vll>;

const ll MOD = 1e9 + 7;
const int INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;
const ld EPS = 1e-9;


int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);

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

    for (;;) {
        int w, h; cin >> w >> h;
        if (w == 0) break;

        vector<string> s(h);
        int si, sj;
        REP(i, h) {
            cin >> s[i];
            REP(j, w) {
                if (s[i][j] == '@') {
                    si = i, sj = j;
                    s[i][j] = '.';
                }
            }
        }

        int ans = 0;
        vvi vis(h, vi(w));
        function<void(int,int)> dfs = [&](int i, int j) {
            vis[i][j] = 1;
            ++ans;
            REP(k, 4) {
                int y = i + dy[k], x = j + dx[k];
                if (y < 0 or x < 0 or y >= h or x >= w) continue;
                if (s[y][x] == '#') continue;
                if (vis[y][x]) continue;
                dfs(y, x);
            }
        };

        dfs(si, sj);

        cout << ans << endl;
    }

    return 0;
}

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

int calc(std::vector<std::vector<char> >&v, int sx, int sy){

	v[sx][sy] = '#';

	int res = 1;
	if (sx - 1 >= 0 && v[sx - 1][sy] == '.')res += calc(v, sx - 1, sy);
	if (sx + 1 < v.size() && v[sx + 1][sy] == '.')res += calc(v, sx + 1, sy);
	if (sy - 1 >= 0 && v[sx][sy - 1] == '.')res += calc(v, sx, sy - 1);
	if (sy + 1 < v[sx].size() && v[sx][sy + 1] == '.')res += calc(v, sx, sy + 1);

	return res;

}

int main(){

	int w, h;
	while (std::cin >> w >> h){
		if (w == 0 && h == 0)break;

		std::vector<std::vector<char> >v(h, std::vector<char>(w));

		int sx, sy;
		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				std::cin >> v[i][j];
				if (v[i][j] == '@')sx = i, sy = j;
			}
		}

		std::cout << calc(v, sx, sy) << std::endl;
	}
	return 0;
}

C++: #include <algorithm>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <map>

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

using namespace std;
typedef pair<int, int> i_i;

int a,w,h;
bool t[22][22],s[22][22];

void dfs(i_i p) {
    s[p.first][p.second] = 1;
    a += 1;
    rep(j,2){
        if(t[p.first][p.second+(int)pow(-1,j)] && s[p.first][p.second+(int)pow(-1,j)] == 0) {
            dfs(make_pair(p.first,p.second+(int)pow(-1,j)));
        }
    }
    rep(i,2) {
        if(t[p.first+(int)pow(-1,i)][p.second] && s[p.first+(int)pow(-1,i)][p.second] == 0) {
            dfs(make_pair(p.first+(int)pow(-1,i),p.second));
        }
    }
}

int main() {
    while(1) {
        i_i m;
        scanf("%d %d",&w,&h);
        if(w == 0) break;
        FOR(i,1,h+1){
            char a[20];
            scanf("%s",a);
            FOR(j,1,w+1) {
                if(a[j-1] == '.') t[i][j] = 1; 
                if(a[j-1] == '@') m = make_pair(i,j);
            }
        }
        dfs(m);
        printf("%d\n",a);
        a = 0;
        rep(i,22) {
            rep(j,22) {
                t[i][j] = 0,s[i][j] =0;
            }
        }
    }
    return 0;
}

C++: // PKU 1979
#include<cstdio>
#define rep( i, a ) for( int i = 0; i != (a); ++i )

int W, H;
char fld[20][20];
int ans;

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

void dfs( int x, int y )
{
	++ans;
	fld[y][x] = ' ';

	rep( i, 4 )
	{
		int nx = x + dx[i], ny = y + dy[i];
		
		if( nx >= 0 && nx < W && ny >= 0 && ny < H && fld[ny][nx] == '.' )
			dfs( nx, ny );
	}

	return;
}

int main()
{
	while( scanf( "%d%d", &W, &H ), W | H )
	{
		ans = 0;
		int sx, sy;

		rep( i, H )
		{
			getchar();

			rep( j, W )
			{
				fld[i][j] = getchar();
			
				if( fld[i][j] == '@' )
					sx = j, sy = i;
			}
		}

		dfs( sx, sy );

		printf( "%d\n", ans );
	}

	return 0;
}

C++: #include<iostream>
#include<queue>
using namespace std;
int w,h;
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
main()
{
	while(cin>>w>>h,w)
	{
		queue<pair<int,int> >P;
		string s[22];
		int cnt=1;
		bool used[22][22]={};
		for(int i=0;i<h;i++)cin>>s[i];
		for(int i=0;i<h;i++)for(int j=0;j<w;j++)
		{
			if(s[i][j]=='@')
			{
				P.push(make_pair(i,j));
				used[i][j]=1;
			}
		}
		while(!P.empty())
		{
			int x=P.front().first,y=P.front().second;P.pop();
			for(int r=0;r<4;r++)
			{
				int tx=dx[r]+x,ty=y+dy[r];
				if(tx<0||ty<0||tx>=h||ty>=w||s[tx][ty]=='#'||used[tx][ty])continue;
				used[tx][ty]=1;
				P.push(make_pair(tx,ty));
				cnt++;
			}
		}
		cout<<cnt<<endl;
	}
}