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<algorithm>
#include<string>
#include<cstring>
using namespace std;
char mas[30][30];
int W,H,sum;
int dx[4] = {0,-1,0,1};
int dy[4] = {1,0,-1,0};
void dfs(int x, int y);
int main(){
  int sx,sy;
  while(1){
    sum = 0;
    cin >> W >> H;
    if(W == 0 && H == 0)break;
    for(int i = 0; i < H; i++){
      for(int j = 0; j < W; j++){
	cin >> mas[i][j];
	if(mas[i][j] == '@'){
	  sx = j;
	  sy = i;
	}
      }
    }
    dfs(sx,sy);
    cout << sum << endl;
  }
}
void dfs(int x, int y){
  sum++;
  mas[y][x] = '#';
  for(int i = 0; i < 4; i++){
    int nx = x + dx[i];
    int ny = y + dy[i];
    if(nx >= 0 && nx < W && ny >= 0 && ny < H && mas[ny][nx] != '#'){
      dfs(nx,ny);
    }
  }
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define EPS 1e-10

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

int w, h;
char field[21][21];
bool used[21][21];

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

void solve(int y,int x){

	REP(i, 4){
		int ny = y + dy[i], nx = x + dx[i];
		if (ny < h&&ny >= 0 && nx < w&&nx >= 0){
			if (field[ny][nx] == '.'&&used[ny][nx] == 0){
				used[ny][nx] = 1;
				solve(ny, nx);
			}
		}
	}

	return;
}

int main(){
	while (cin >> w >> h&&w + h){
		REP(i, 21)REP(j, 21)used[i][j] = 0;
		REP(i, h){
			REP(j, w){
				cin >> field[i][j];
				if (field[i][j] == '@'){
					sy = i, sx = j;
				}
			}
		}

		solve(sy, sx);
		used[sy][sx] = 1;

		int ans = 0;
		REP(i, h){
			REP(j, w){
				if (used[i][j])ans++;
			}
		}
		cout << ans << endl;
	}
}

C++: #include <bits/stdc++.h>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<P,int> pip;
typedef vector<pip> vip;
const int inf=1<<30;
const ll INF=1ll<<60;
const double pi=acos(-1);
const double eps=1e-8;
const ll mod=1e9+7;
const int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};

int h,w;
vvi a;

int dfs(int x,int y){
	int t=1;
	a[x][y]++;
	for(int i=0;i<4;i++){
		int X=x+dx[i],Y=y+dy[i];
		if(!a[X][Y]) t+=dfs(X,Y);
	}
	return t;
}

int main(){
	while(1){
		cin>>w>>h;
		if(!h) break;
		int s,t;
		a=vvi(h+2,vi(w+2,1));
		for(int i=1;i<=h;i++) for(int j=1;j<=w;j++){
			char c;
			cin>>c;
			if(c!='#') a[i][j]--;
			if(c=='@') s=i,t=j;
		}
		cout<<dfs(s,t)<<endl;
	}
}

C++: #include <stdio.h>
#include <string.h>

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

#define ISIN(x, y) ((x) >= 0 && (x) < (w) && (y) >= 0 && (y) < (h))

int main(void)
{
	char s[100][100];
	char f[100][100];
	int w, h;
	int px, py;
	int count, ago;
	
	while (1){
		scanf("%d%d", &w, &h);
		if (w == 0){
			break;
		}
		for (int y = 0; y < h; y++){
			scanf("%s", s[y]);
			for (int x = 0; x < w; x++){
				if (s[y][x] == '@'){
					px = x;
					py = y;
				}
			}
		}
		
		ago = count = 1;
		memset(f, 0, sizeof(f));
		f[py][px] = 1;
		do {
			ago = count;
			for (int y = 0; y < h; y++){
				for (int x = 0; x < w; x++){
					if (f[y][x] == 1){
						for (int i = 0; i < 4; i++){
							if (s[y + dy[i]][x + dx[i]] != '#' && f[y + dy[i]][x + dx[i]] == 0 && ISIN(x + dx[i], y + dy[i])){
								f[y + dy[i]][x + dx[i]] = 1;
								count++;
							}
						}
					}
				}
			}
		} while (ago != count);
		
		printf("%d\n", count);
	}
	
	return (0);
}

C++: #include <iostream>
#include <queue>
#include <utility>
#include <array>

using namespace std;

typedef pair<int, int> pii;

int height, width;
int sx, sy;


void bfs(vector<vector<char>>& field)
{
    int ans = 1;
    array<int, 4> dx = {0, 0, 1, -1};
    array<int, 4> dy = {1, -1, 0, 0};

    queue<pii> p;
    p.push({sx, sy});
    
    while (!p.empty()) {
        pii q = p.front(); p.pop();
        
        //??????
        int cx = q.first;
        int cy = q.second;

        for (int i = 0; i < 4; ++i) {
            int nx = cx + dx.at(i);
            int ny = cy + dy.at(i);

            if (nx >= 0 && nx < width && ny >= 0 && ny < height 
                    && field.at(ny).at(nx) == '.') {

                ans++;
                field.at(ny).at(nx) = '#';
                p.push({nx, ny});
            }
        }
    }

    std::cout << ans << std::endl;
}


int main()
{
    while (cin >> width >> height) {
        if (height == 0 && width == 0) break;

        vector<vector<char>> field(height, vector<char>(width));

        for (int y = 0; y < height; ++y) {
            for (int x = 0; x < width; ++x) {
                cin >> field.at(y).at(x);

                if (field.at(y).at(x) == '@') {
                    sx = x; sy = y;
                }
            }
        }

        bfs(field);
    }

    return 0;
}