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 <stdio.h>
#include <string.h>

int main(){
        bool check=true;
        int w, h, at_x, at_y;

        while(1){
                int total=1;
                char tile[22][22] = { { 0 } };
                scanf("%d %d", &w, &h);
                if( w == 0 && h == 0 ) break;
                for( int i=0; i<h; i++ ){
                        scanf("%s", &tile[i+1][1]);
                }
                check = true;
                while( check ){
                        check = false;
                        for( int y=1; y<=h; y++){
                                for( int x=1; x<=w; x++ ){
                                        if( tile[y][x] == '@' ){
                                                at_x = x;
                                                at_y = y;
                                                if( tile[y-1][x] == '.' ){
                                                        tile[y-1][x] = '@';
                                                        check = true;
                                                        total++;
                                                }
                                                if( tile[y+1][x] == '.' ){
                                                        tile[y+1][x] = '@';
                                                        check = true;
                                                        total++;
                                                }
                                                if( tile[y][x-1] == '.' ){
                                                        tile[y][x-1] = '@';
                                                        check = true;
                                                        total++;
                                                }
                                                if( tile[y][x+1] == '.' ){
                                                        tile[y][x+1] = '@';
                                                        check = true;
                                                        total++;
                                                }
                                        }
                                }
                        }
                }
                printf("%d\n", total);
        }
        return 0;
}

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

int dfs(int x,int y);
int h,w;
char ta[22][22];
int cheak[22][22]={0};
int dx[4]={-1,0,1,0};
int dy[4]={0,-1,0,1};
int cou=0;

int main(){
  int a,b;
  while(1){
    cin >> w >> h;
    if(w==0 && h==0) break;
    cou=0;
    
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	cin >> ta[i][j];
	cheak[i][j]=0;
	if(ta[i][j] == '@'){
	  a=i,b=j;
	}
      }
    }
    //cout << a << " " << b << endl;
    cout << dfs(a,b) << endl;
    /*for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
      cout << cheak[i][j];
      }
      cout << endl;
      }*/
  }
}

int dfs(int x,int y){
  cheak[x][y]=1;
  cou++;
  for(int i=0;i<4;i++){
    int nx=x+dx[i];
    int ny=y+dy[i];
    if(nx>=0 && nx<h && ny>=0 && ny<w && ta[nx][ny]=='.' && cheak[nx][ny]==0){
      dfs(nx,ny);
    }
  }
  return cou;
}

C++: #include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define RITR(x,c) for(__typeof(c.rbegin()) x=c.rbegin();x!=c.rend();x++)
#define setp(n) fixed << setprecision(n)

#define lf double
#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll,ll>
#define pi pair<int,int>

#define all(a) (a.begin()),(a.end())
#define rall(a) (a.rbegin()),(a.rend())
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ins insert

using namespace std;

ll w, h;
char field[100][100];
bool use[100][100];
ll ans=0;

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

void dfs(int x, int y)
{
	use[y][x] = true;
	ans++;
	rep(i, 4){
		ll tx = x+dx[i];
		ll ty = y+dy[i];
		if (!(0<=tx && tx<=w-1 && 0<=ty && ty<=h-1)) continue;
		if (use[ty][tx] || field[ty][tx]=='#') continue;
		dfs(tx, ty);
	}
	return;
}

int main(void)
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	
	while(cin>>w>>h, w!=0){
		ll sx, sy;
		ans = 0;
		memset(use, 0, sizeof(use));
		rep(i, h){
			rep(j, w){
				cin >> field[i][j];
				if (field[i][j]=='@'){
					sx = j; sy = i;
				}
			}
		}
		dfs(sx, sy);
		cout << ans << "\n";
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int (i)=(0);(i)<(int)(n);++(i))
using ll = long long;
using P = pair<int, int>;
using namespace std;

template<class T> void vin(vector<T>& v, int n) {
    v.resize(n);
    for (int i = 0; i < n; ++i) {
        cin >> v[i];
    }
}

int w, h;
char ch[50][50];
int dx[] = { 0, 1, 0, -1 };
int dy[] = { 1, 0, -1, 0 };

int bfs(int y, int x) {
    int res = 0;
    rep(i, 4) {
        int ny = y + dy[i], nx = x + dx[i];
        if (0 > ny or 0 > nx or ny >= h or nx >= w) continue;
        if (ch[ny][nx] == '.') {
            ch[ny][nx] = 'a';
            res += bfs(ny, nx) + 1;
        }
    }
    return res;
}

int main() {
    while (cin >> w >> h and (w or h)) {
        int py = 0, px = 0;
        rep(i, h) rep(j, w) {
            cin >> ch[i][j];
            if (ch[i][j] == '@') {
                py = i, px = j;
                ch[i][j] = '.';
            }
        }
        cout << bfs(py, px) << endl;
    }
}