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 <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <stack>
#include <queue>
//ranker
using namespace std;

#define REPS(i, a, n) for (int i = (a); i < (n); ++i)
#define REP(i, n) REPS(i, 0, n)
#define RREP(i, n) REPS(i, 1, n + 1)
#define DEPS(i, a, n) for (int i = (a); i >= n; --i)
#define DEP(i, n) DEPS(i, n, 0)

using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<int, int>;
using pis = pair<int, string>;
using psi = pair<string, int>;
using D = double;

void merge (int *first1, int *last1, int *first2, int *last2){
    int size1 = last1 - first1;
    int size2 = last2 - first2;
    int *result = new int[size1+size2];
    int i = 0;
    int j = 0;
    int index = 0;
    while (true){
        if (i < size1 && (first1[i] <= first2[j] || j >= size2)){
            result[index] = first1[i];
            ++i; ++index;
        }
        if (j < size2 && (first1[i] > first2[j] || i >= size1)){
            result[index] = first2[j];
            ++j; ++index;
        }
        if (i==size1 && j==size2){
            for (i=0; i<size1; ++i) first1[i] = result[i];
            for (j=0; j<size2; ++j) first2[j] = result[j+size1];
            delete[] result;
            return;
        }
    }
}
void MergeSort (int *first, int *last){
    int size = last - first;
    if(size <= 1) return;
    MergeSort(first, first+size/2);
    MergeSort(first+size/2, last);
    merge(first, first+size/2, first+size/2, last);
}


int gcd(int a, int b)
{
    int c;

    if (a < b) {
        a+=b; b=a-b; a-=b;
    }

    while (b != 0) {
        c = a % b;
        a = b;
        b = c;
    }

    return a;
}

int dx[3]={-1, 0, 1};
int dy[3]={-1, 0, 1};
int W, H;
char masu[20][20];
int cnt;

int dfs(int a, int b){
  masu[a][b] = '#';
  cnt++;
  REP(i, 3)REP(j, 3) if(masu[a+dx[i]][b+dy[j]]=='.' && abs(dx[i]+dy[j])==1 && a+dx[i] >= 0 && a+dx[i] < W && b+dx[j] >= 0 && b+dx[j] < H) dfs(a+dx[i], b+dy[j]);
  return cnt;
}


int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  while(1){
    cin >> W >> H;
    if(W == 0 && H == 0) break;
    REP(j, H)REP(i, W) cin >> masu[i][j];

    int i0, j0;
    REP(j, H)REP(i, W) if(masu[i][j] == '@'){
      i0=i;
      j0=j;
      break;
    }

    cnt = 0;
    cnt = dfs(i0, j0);
    cout << cnt << endl;
  }
    return 0;
}

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

#define int				long long
#define FOR( i, m, n )  for( int (i) = (m); (i) < (n); (i)++ )
#define REP( i, n )     FOR( i, 0, n )
#define REPR( i, m )	for( int (i) = (m); (i) >= 0; (i)-- )
#define REPONE( i, n )	FOR( i, 1, n + 1 )
#define ALL( a )        (a).begin(), (a).end()
#define MP				make_pair
#define X				first.first
#define Y				first.second

typedef pair<int, int> P;

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

const int INF = 1e9;
const int MOD = 1e9 + 7;

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

char tile[21][21];
int W, H;
int ans;

void dfs( int x, int y ) {
	REP( i, 4 ) {
		int nx = x + dx[i];
		int ny = y + dy[i];

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

signed main() {
	while( cin >> W >> H, W, H ) {
		
		int sx, sy;
		REP( i, H ) {
			REP( j, W ) {
				cin >> tile[j][i];
				if( tile[j][i] == '@' ) {
					sx = j;
					sy = i;
					tile[j][i] = '.';
				}
			}
		}

		
		ans = 0;
		dfs( sx, sy );
		cout << ans << endl;
		
	}
}

C++: #include<cstdio>
#include<iostream>
#include<string>
using namespace std;
typedef pair<int,int> P;
int ans;
int w,h;
string map[50];
int dx[4] = {1,0,-1,0}, dy[4] = {0,-1,0,1};
int dfs(P s);

int main(){
  int i,j;
  P s;
  //cout <<x<<y<<endl;
  while(1){
    cin >> w >> h;
    cin.ignore();
    if(w == 0 && h == 0) break;
    for(i = 0;i < h;i++) getline(cin,map[i]);
    while(map[s.second][s.first] != '@'){
      //cout << map[y][x]<<endl;
      s.first++;
      if(s.first > w-1){
	s.second++;
	s.first = 0;
      }
    }
    dfs(s);
    cout << ans<<endl;
    s.first = s.second = ans = 0;
  }
  return 0;
}

int dfs(P s){
  int i;
  P tmp;
  map[s.second][s.first] = '#';
  ans++;
  //cout <<"x = " << s.first << " y = " << s.second << endl;
  for(i = 0; i<4;i++){
    if(s.first + dx[i] > -1 && s.first + dx[i] < w && s.second + dy[i] < h && s.second + dy[i] > -1 && map[s.second + dy[i]][s.first + dx[i]] == '.'){
      tmp.first = s.first + dx[i];
      tmp.second = s.second + dy[i];
      dfs(tmp);
    }
  }
    return 0;
}