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 FOR(i, s, n) for(int i = s; i < (int)n; i++)
#define per(i, n) for(int i = n; i >= 0; i--)
#define ROF(i, s, n) for(int i = s; i >= (int)n; i--)
#define FORIT(i, A) for (auto i : A)
#define PRINT(x) cout << (x) << "\n"
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define MP make_pair
#define EACH(i, n) for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i)
#define SZ(a) int((a).size())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define CLR(a) memset((a), 0 ,sizeof(a))
#define dump(x) cout << #x << " = " << (x) << "\n";
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << "\n";
#define sq(n) (n) * (n)
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef priority_queue<int> maxpq;
typedef priority_queue< int, vector<int>, greater<int> > minpq;
static const double EPS = 1e-10;
static const double PI = acos( -1.0 );
static const int mod = 10007;
static const int INF = 1 << 25;
static const LL LL_INF = 1152921504606846976;
static const int dx[] = { 1, -1, 0, 0 };
static const int dy[] = { 0, 0, 1, -1 };

int W, H, a, b, ans;
VS mp;
string str;

int gcd( int m, int n ) {
	if ( n == 0 ) return m;
	return gcd( n, m % n );
}


int main() {
	while ( ~scanf("%d %d", &W, &H) && W && H ) {
		for ( int i = 0; i < W + 2; i++ ) {
			str += "#";
		}
		ans = 0;
		mp.resize( H + 2 );
		mp[ 0 ] = str;
		mp[ H + 1 ] = str;
		for ( int i = 1; i < H + 1; i++ ) {
			cin >> str;
			str = "#" + str + "#";
			mp[ i ] = str;
			for ( int j = 1; j < W + 2; j++ ) {
				if ( mp[ i ][ j ] == '@' ) {
					a = i;
					b = j;
				}
			}
		}
		queue<PII> q;
		q.push( PII( a, b ) );
		while ( q.size() ) {
			int y = q.front().first;
			int x = q.front().second;
			if ( mp[ y ][ x ] == '#' ) {
				q.pop();
				continue;
			}
			mp[ y ][ x ] = '#';
			ans++;
			q.pop();
			for ( int i = 0; i < 4; i++ ) {
				if ( mp[ y + dy[ i ] ][ x + dx[ i ] ] != '#' ) {
					q.push( PII( y + dy[ i ], x + dx[ i ] ) );
				}
			}
		}
		printf( "%d\n", ans );
		str.clear();
		mp.clear();
	}
	return 0;
}

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

const int NUM = 7;

void grooping(int y, int x, int w, int h, char** room, unsigned long*** group, unsigned long* next)
{
	if (room[y][x] != '#') {
		for (int i = y - 1; i <= y + 1; i++) {
			for (int j = x - 1; j <= x + 1; j++) {
				if (i < 0 || j < 0 || i >= h || j >= w) {
					continue;
				}
				if ((i == y && j != x) || (i != y && j == x)) {
					for (int k = 0; k < NUM; k++) {
						group[y][x][k] |= group[i][j][k];
					}
				}
			}
		}
		for (int i = 0; i < NUM; i++) {
			if (group[y][x][i] == 0UL && next[i] != 0UL) {
				group[y][x][i] |= next[i];
				next[i] <<= 1;
				break;
			}
		}
	}
}

int main()
{
	int w, h;
	while (true) {
		cin >> w >> h;
		if (w == 0 && h == 0) {
			break;
		}

		unsigned long next[NUM] = { 1UL, 1UL, 1UL, 1UL, 1UL, 1UL, 1UL };
		char** room = new char*[h];
		unsigned long*** group = new unsigned long**[h];
		int x, y;
		for (int i = 0; i < h; i++) {
			room[i] = new char[w];
			group[i] = new unsigned long*[w];
			for (int j = 0; j < w; j++) {
				cin >> room[i][j];
				if (room[i][j] == '@') {
					x = j;
					y = i;
				}
				group[i][j] = new unsigned long[NUM];
				for (int k = 0; k < NUM; k++) {
					group[i][j][k] = 0UL;
				}
			}
		}

		for (int k = 0; k < 210; k++) {
			for (int i = 0; i < h; i++) {
				for (int j = 0; j < w; j++) {
					grooping(i, j, w, h, room, group, next);
				}
			}
			for (int i = h - 1; i >= 0; i--) {
				for (int j = w - 1; j >= 0; j--) {
					grooping(i, j, w, h, room, group, next);
				}
			}
			for (int j = 0; j < w; j++) {
				for (int i = 0; i < h; i++) {
					grooping(i, j, w, h, room, group, next);
				}
			}
			for (int j = w - 1; j >= 0; j--) {
				for (int i = h - 1; i >= 0; i--) {
					grooping(i, j, w, h, room, group, next);
				}
			}
		}

		int count = 0;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				for (int k = 0; k < NUM; k++) {
					if ((group[i][j][k] & group[y][x][k]) != 0UL) {
						count++;
						break;
					}
				}
			}
		}

		cout << count << '\n';

		for (int i = 0; i < h; i++) {
			delete[] room[i];
			for (int j = 0; j < w; j++) {
				delete[] group[i][j];
			}
			delete[] group[i];
		}
		delete[] room;
		delete[] group;
	}

	return 0;
}

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

const int MAX_SIZE = 20;
typedef pair<int, int> P;

int calc(char f[][MAX_SIZE], queue<P> q, int W, int H) {
  int n = 0;
  while (q.size()) {
    n++;
    P p = q.front();
    q.pop();
    int di[] = {-1, 1, 0, 0};
    int dj[] = {0, 0, -1, 1};
    for (int k = 0; k < 4; ++k) {
      int i = p.first  + di[k];
      int j = p.second + dj[k];
      if (i >= 0 && j >= 0 && i < H && j < W && f[i][j] == '.') {
        f[i][j] = '#';
        q.push(P(i, j));
      }
    }
  }
  return n;
}
int main() {
  int W, H;
  char f[MAX_SIZE][MAX_SIZE];
  while (true) {
    queue<P> q;
    cin >> W >> H;
    if (W == 0 && H == 0) break;
    for (int i = 0; i < H; ++i) {
      cin >> f[i];
      for (int j = 0; j < W; ++j) {
        if (f[i][j] == '@') {
          q.push(P(i, j));
        }
      }
    }
    cout << calc(f, q, W, H) << endl;
  }
}