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 <vector>
#include <string>
#include <utility>
#include <queue>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
#include <climits>
//#include "h.h"
using namespace std;

#define ll long long
#define pint pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define mp make_pair
#define pm(first, second) pb(mp(first, second))
#define SPACE " "
#define fpf first.first
#define fps first.second
#define spf second.first
#define sps second.second

#define all(X) (X).begin(), (X).end()

int main(){

	while(true){
		int W, H;
		cin >> W >> H;
		if(W == 0 && H == 0)break;
		vector<string> S(H);
		queue<pair<int, int>> Q;
		for(int i = 0; i < H; i ++){
			cin >> S[i];
			for(int j = 0; j < W; j ++){
				if(S[i][j] == '@')Q.push(mp(i, j));
			}
		}
		pair<int, int> P;
		while(!Q.empty()){
			P = Q.front();
			Q.pop();
			S[P.first][P.second] = '@';
			if(P.first > 0 && S[P.first - 1][P.second] == '.')Q.push(mp(P.first - 1, P.second));
			if(P.first < H - 1 && S[P.first + 1][P.second] == '.')Q.push(mp(P.first + 1, P.second));
			if(P.second > 0 && S[P.first][P.second - 1] == '.')Q.push(mp(P.first, P.second - 1));
			if(P.second < W - 1 && S[P.first][P.second + 1] == '.')Q.push(mp(P.first, P.second + 1));
		}
		int ans = 0;
		for(int i = 0; i < H; i ++){
			for(int j = 0; j < W; j ++){
				if(S[i][j] == '@')ans ++;
			}
		}
		cout << ans << endl;
	}

	return 0;
}

C++: #include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <vector>
#include <complex>
#include <utility>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <bitset>
#include <ctime>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <cassert>
#include <cstddef>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <sstream>
#include <fstream>

using namespace std;
#define REP(i, n) for (int (i) = 0; (i) < (n); (i)++)
#define FOR(i, a, b) for (int (i) = (a); (i) < (b); (i)++)
#define RREP(i, a) for(int (i) = (a) - 1; (i) >= 0; (i)--)
#define FORR(i, a, b) for(int (i) = (a) - 1; (i) >= (b); (i)--)
#define DEBUG(C) cerr << #C << " = " << C << endl;
using LL = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<LL>;
using VVL = vector<VL>;
using VD = vector<double>;
using VVD = vector<VD>;
using PII = pair<int, int>;
using PDD = pair<double, double>;
using PLL = pair<LL, LL>;
using VPII = vector<PII>;
#define ALL(a) begin((a)), end((a))
#define RALL(a) rbegin((a)), rend((a))
#define SORT(a) sort(ALL((a)))
#define RSORT(a) sort(RALL((a)))
#define REVERSE(a) reverse(ALL((a)))
#define MP make_pair
#define FORE(a, b) for (auto &&a : (b))
#define FIND(s, e) ((s).find(e) != (s).end())
#define EB emplace_back

const int INF = 1e9;
const int MOD = INF + 7;
const LL LLINF = 1e18;

vector<string> C;
vector<vector<bool>> used;
int MH, MW;
#define H first
#define W second
bool inside(int h, int w) {
    return 0 <= h && h < MH && 0 <= w && w < MW;
}
bool inside(PII p){return inside(p.H, p.W);}
int dh[] = {-1, 0, 0, 1};
int dw[] = {0, -1, 1, 0};
int dfs(PII now) {
    int res = 1;
    if (used[now.H][now.W]) return 0;
    used[now.H][now.W] = true;
    REP(i, 4) {
        auto buf = MP(now.H + dh[i], now.W + dw[i]);
        if (!inside(buf)) continue;
        if (C[buf.H][buf.W] == '.') res += dfs(buf);
    }
    return res;
}
void solve(int w, int h) {
    MH = h; MW = w;
    C.resize(h);
    used.resize(h, vector<bool>(w, false));
    PII start;
    REP(i, h) {
        cin >> C[i];
        REP(j, w) {
            if (C[i][j] == '@') start = MP(i, j);
        }
    }
    //cout << start.H << " " << start.W << endl;
    cout << dfs(start) << endl;
    used.clear();
    return;
}

int main(void) {
    int n, p, m, w, h;
    string s;
    int a, b, c;
    while (cin >> w >> h) {
        if (w == 0 && h == 0) return 0;
        solve(w, h);
        //return 0;
    }
}

C++: #include <iostream>
#include <string>
#define MAX 22
using namespace std;
char map[ MAX ][ MAX ];
int n;
void visit( int i, int j ){
  if( map[ i ][ j ] == '#' ){ return; }
  n++;
  map[ i ][ j ] = '#';
  visit( i - 1, j );
  visit( i, j - 1 );
  visit( i + 1, j );
  visit( i, j + 1 );
}
int main(){
  while( 1 ){
    int w, h;
    int pi, pj;
    n = 0;
    cin >> w >> h;
    if( !w || !h ){ return 0; }
    for( int i = 0; i <= h + 1; i++ ){
      if( i == 0 || i == h + 1 ){
        for( int j = 0; j <= w + 1; j++ ){
          map[ i ][ j ] = '#';
        }
        continue;
      }
      string line;
      cin >> line;
      for( int j = 0; j <= w + 1; j++ ){
        if( j == 0 || j == w + 1 ){
          map[ i ][ j ] = '#';
          continue;
        }
        map[ i ][ j ] = line[ j - 1 ];
        if( map[ i ][ j ] == '@' ){
          pi = i;
          pj = j;
        }
      }
    }
    visit( pi, pj );
    cout << n << endl;
  }
}