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>
using namespace std;

int main(){
	int W=0,H=0;
	int posW=0,posH=0;
	int ans=0;
	int fposW=0,fposH=0;
	int n=0;

	while(1){
	cin >> W >> H;

	if(H==0&&W==0) return 0;

	char tile[20][20];

	for(int i=0;i<H;i++){
		for(int j=0;j<W;j++){
			cin >> tile[i][j];
		}
	}

	for(int i=0;i<H;i++){
		for(int j=0;j<W;j++){
			if(tile[i][j] == '@'){
				fposH = i;
				fposW = j;
			}
		}
	}
	posH = fposH;
	posW = fposW;

	for(int n=0;n<1000*W*H;n++){
		if(tile[posH][posW-1]=='.'){
			ans++;
			tile[posH][posW-1]='@';
			posW--;
		}
		else if(tile[posH-1][posW]=='.'){
			ans++;
			tile[posH-1][posW]='@';
			posH--;
		}
		else if(tile[posH][posW+1]=='.'){
			ans++;
			tile[posH][posW+1]='@';
			posW++;
		}
		else if(tile[posH+1][posW]=='.'){
			ans++;
			tile[posH+1][posW]='@';
			posH++;
		}
		else if(tile[posH+1][posW]=='@'&&n%4==0){
			posH++;
		}
		else if(tile[posH][posW+1]=='@'&&n%4==1){
			posW++;
		}
		else if(tile[posH-1][posW]=='@'&&n%4==2){
			posH--;
		}
		else if(tile[posH][posW-1]=='@'&&n%4==3){
			posW--;
		}
		if(n==(1000*H*W)/4){
			posH=fposH;
			posW=fposW;
		}
		if(n==(1000*H*W)/2){
			posH=fposH;
			posW=fposW;
		}if(n==(1000*H*W*3)/4){
			posH=fposH;
			posW=fposW;
		}
		
		}

	cout << ans+1 << endl;
	ans = 0;
	for(int i=0;i<H;i++){
		for(int j=0;j<W;j++){
			tile[i][j]='#';
		}
	}
	posH=0;
	posW=0;
	fposH=0;
	fposW=0;
	
	}

	return 0;
}

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

using namespace std;

int tile[22][22];
int w, h;
int cnt;

void fill(int x, int y) {
	tile[x][y] = 0;
	cnt++;
	if (tile[x - 1][y] == 1) fill(x - 1, y);
	if (tile[x][y - 1] == 1) fill(x, y - 1);
	if (tile[x][y + 1] == 1) fill(x, y + 1);
	if (tile[x + 1][y] == 1) fill(x + 1, y);
}

int main() {
	int x = 0, y = 0;
	while (cin >> w >> h && (w || h)) {
		cnt = 0;
		for (int i = 0; i < 22; ++i) for (int j = 0; j < 22; ++j) tile[i][j] = 0;
		for (int i = 1; i <= h; ++i){
			for (int j = 1; j <= w; ++j) {
				char tmp;
				cin >> tmp;
				if (tmp == '.') tile[i][j] = 1;
				else if (tmp == '#') tile[i][j] = 0;
				else x = i, y = j, tile[i][j] = 1;
			}
		}
		fill(x, y);
		cout << cnt << endl;
	}

	return 0;
}

C++: #include<iostream>
#include<cstdio>
using namespace std;
int tile(int i,int j);
char memo[22][22];
int w,h;
int ii,jj;

int main(){
  while(1){
    cin >> w >> h;
    if(w==0&&h==0)break;
    for(int i=0;i<h;i++){
      cin >> memo[i];
      for(int j=0;j<w;j++){
	if(memo[i][j]=='@'){
	  ii=i;
	  jj=j;
	}
      }
    }
    int c=tile(ii,jj);
    cout << c << endl;
  }
}

int tile(int i,int j){
  if(i>=h||j>=w||i<0||j<0)return 0;
  if(memo[i][j]=='#') return 0;
  memo[i][j]='#';
  int ret=0;
  ret+=tile(i,j-1);
  ret+=tile(i,j+1);
  ret+=tile(i-1,j);
  ret+=tile(i+1,j);
  return ret+1;
}

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

#define iota(i,n,b,s) for(int i=int(b);i!=int((b)+(s)*(n));i+=(s))
#define range(i,n,m) iota(i,(((n)>(m))?((n)-(m)+1):((m)-(n)+1)),(n),((n)>(m)?-1:1))
#define rep(i,n) iota(i,(n),0,1)

#define INF (1e9)
#define EPS (1e-9)
#define cons(a,b) (make_pair(a,b))
#define car(a) (a.first)
#define cdr(a) (a.second)
#define cadr(a) (car(cdr(a)))
#define cddr(a) (cdr(cdr(a)))
#define all(a) a.begin(), a.end()
#define trace(var) cerr<<">>> "<<#var<<" = "<<var<<endl;

typedef long long INTEGER;
typedef double FLOAT;

template<class S, class T>
ostream& operator<<(ostream& os, pair<S,T> p) {
  os << '(' << car(p) << ", " << cdr(p) << ')';
  return os;
}

template<class T>
ostream& operator<<(ostream& os, vector<T> v) {
  os << v[0];
  for (int i=1, len=v.size(); i<len; ++i) os << ' ' << v[i];
  return os;
}

int main()
{
entry:;

  int w, h; cin >> w >> h;
  if (!w && !h) return 0;

  int f[h][w];
  int ci, cj;
  rep (i, h) {
    string s; cin >> s;
    rep (j, s.size()) {
      if (s[j] == '.') {
        f[i][j] = 1;
      } else if (s[j] == '@') {
        f[i][j] = 1;
        ci = i;
        cj = j;
      } else {
        f[i][j] = 0;
      }
    }
  }

  stack<pair<int,int>> s;
  s.push(cons(ci, cj));

  int ans = 0;
  while (!s.empty()) {
    pair<int, int> x = s.top(); s.pop();
    if (f[car(x)][cdr(x)] == 0) continue;
    f[car(x)][cdr(x)] = 0;
    ++ans;

    if (car(x) > 0) s.push(cons(car(x) - 1, cdr(x)));
    if (cdr(x) > 0) s.push(cons(car(x), cdr(x) - 1));
    if (car(x) < h - 1) s.push(cons(car(x) + 1, cdr(x)));
    if (cdr(x) < w - 1) s.push(cons(car(x), cdr(x) + 1));

  }

  cout << ans << endl;

  goto entry;

  return 0;
}