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;
typedef long long ll;
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define FORR(i, m, n) for(int i = m; i >= n; i--)
#define INF (ll)2e9
#define MOD ((ll)1e9+7)
#define ALL(v) v.begin(), v.end()
#define SZ(x) ((int)(x).size())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
#define y0 y3487465
#define y1 y8687969
#define j0 j1347829
#define j1 j234892
#define next asdnext
#define prev asdprev
#define bit(n) (1LL<<(n))
#define cauto const auto&
#define println(v) cout << v << "\n";

inline vector<vector<vector<ll>>> makeVector(ll i, ll j, ll k) {
    vector<vector<vector<ll>>> v(i, vector<vector<ll>>(j, vector<ll>(k, 0)));
    return v;
}
inline vector<vector<ll>> makeVector(ll i, ll j) {
    vector<vector<ll>> v(i, vector<ll>(j, 0));
    return v;
}

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 (b<a) { a=b; return 1; } return 0; }
template <class InputIterator>
InputIterator adv(InputIterator x, typename std::iterator_traits<InputIterator>::difference_type n) {
    advance(x, n);
    return x;
}

class mod {
    static ll fact[];
public:

    template<class... A>
    static ll mul(A... args) {
        ll res = 1;
        for (ll i : std::initializer_list<ll>{args...}) {
            res = (res * i) % MOD;
        }
        return res;
    }

    static ll power(ll base, ll exp) {
        if (exp == 0) return 1;
        if (exp & 1) {
            return mul(base, power(base, exp - 1));
        } else {
            ll p = power(base, exp / 2);
            return mul(p, p);
        }
    }

    static ll factorial(int n) {
        if (fact[n] != 0) return fact[n];
        if (n == 0) return 1;
        return fact[n] = mul(n, factorial(n - 1));
    }

    static ll inverse(int n) {
        return power(n, MOD - 2);
    }

    static ll comb(int n, int r) {
        if (r < 0 || r > n) return 0;
        return mul(factorial(n), inverse(factorial(n - r)), inverse(factorial(r)));
    }

    template<class... A>
    static ll div(ll dividend, A... args) {
        ll res = dividend;
        for (ll i : std::initializer_list<ll>{args...}) {
            res = mul(res, inverse(i));
        }
        return res;
    }

    template<class... A>
    static ll add(A... args) {
        ll res = 0;
        for (ll i : std::initializer_list<ll>{args...}) {
            res = (res + i) % MOD;
        }
        return res;
    }

    template<class... A>
    static ll sub(ll l, A... args) {
        ll res = l;
        for (ll i : std::initializer_list<ll>{args...}) {
            res = (res - i + MOD) % MOD;
        }
        return res;
    }
};
ll mod::fact[(int) 1e6 + 1];

int ni() {
    int i;
    cin >> i;
    return i;
}
ll nll() {
    ll l;
    cin >> l;
    return l;
}

int main()
{
    int dy[] = {-1, 0, 1, 0};
    int dx[] = {0, 1, 0, -1};
    while (true) {
        int W = ni(), H = ni();
        if (W == 0) break;
        auto board = makeVector(H, W);
        int sy, sx;
        REP (y, H)  {
            string line;
            cin >> line;
            REP (x, W) {
                board[y][x] = (line[x] == '#');
                if (line[x] == '@') {
                    sy = y;
                    sx = x;
                }
            }
        }

        auto visited = makeVector(H, W);

        deque<pair<int, int>> Q;
        Q.push_back(make_pair(sy, sx));
        visited[sy][sx] = true;

        ll ans = 0;
        while (!Q.empty()) {
            ans++;
            auto p = Q.back();
            Q.pop_back();
            REP(i,4) {
                int ny = p.first + dy[i];
                int nx = p.second + dx[i];
                if (ny < 0 || H <= ny) continue;
                if (nx < 0 || W <= nx) continue;
                if (board[ny][nx]) continue;
                if (visited[ny][nx]) continue;
                Q.push_back(make_pair(ny, nx));
                visited[ny][nx] = true;
            }
        }

        cout << ans <<  endl;

    }

    return 0;
}

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

using namespace std;

int w, h;
int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};
int dfs(vector<string>& mp, int x, int y){
  int cnt = 0;  
  for(int i=0;i<4;++i){
	int tx = x + dx[i];
	int ty = y + dy[i];
	if(tx < 0 || w <= tx || ty < 0 || h <= ty) continue;
	if(mp[ty][tx] == '#') continue;
	mp[ty][tx] = '#';
	cnt += dfs(mp, tx, ty) + 1;
  }
  return cnt;
}

int main(){
  while(true){
	if(!(cin >> w >> h)) return 0;
	if(w == 0 && h == 0) break;
	vector<string> vs(h);
	for(int i=0;i<h;++i)
	  cin >> vs[i];
	int x, y;
	for(int i=0;i<h;++i)
	  for(int j=0;j<w;++j)
		if(vs[i][j] == '@') x=j, y=i;
	vs[y][x] = '#';
	cout << 1+dfs(vs,x,y) << endl;
  }
  return 0;
}

C++: #include<iostream>
using namespace std;
typedef pair<int, int> pii;
pii dxdy[]={{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
char fld[20][20];
bool used[20][20];
int W, H;

void rec(int i, int j){
   used[i][j]=1;
   for(pii dd: dxdy){
      int ni=i+dd.first;
      int nj=j+dd.second;
      if((0<=ni&&ni<H&&0<=nj&&nj<W)&&fld[ni][nj]=='#'&&used[ni][nj]==0){
         rec(ni, nj);
      }
   }
}

int main(){

   while(1){
      cin>> W>> H;
      if(!(H||W)) break;
      int sx, sy;
      for(int i=0; i<H; i++){
         for(int j=0; j<W; j++){
            cin>> fld[i][j];
            if(fld[i][j]=='@'){
               sy=i;
               sx=j;
            }
         }
      }

      for(int i=0; i<20; i++)for(int j=0; j<20; j++) used[i][j]=0;
      used[sy][sx]=1;
      rec(sy, sx);
      int ans=0;
      for(int i=0; i<H; i++)for(int j=0; j<W; j++){
         ans+=used[i][j];
      }
      cout<< ans<< endl;

   }

   return 0;
}