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 <stdio.h>

#define W 21
#define H 20

int tileCount(char room[H][W], int row, int col, int h, int w);

int main()
{
  char room[H][W];
  int h;    // 部屋の行方向のタイルの枚数
  int w;    // 部屋の列方向のタイルの枚数
  int row;  // 最初に人が立っている行位置
  int col;  // 最初に人が立っている列位置
  int i, j;

  // 部屋の情報を、配列room[]に文字列として読み込む
  scanf("%d%d", &w, &h);
  while (h != 0 && w != 0) {
    for (i = 0; i < h; i++) {
      scanf("%s", room[i]);
      for (j = 0; j < w; j++) {
        if (room[i][j] == '@') {
          row = i;  // 最初に人が立っている行位置
          col = j;  // 最初に人が立っている列位置
        }
      }
    }
    printf("%d\n", tileCount(room, row, col, h, w));

    scanf("%d%d", &w, &h);
  }
  
  return(0);
}

int tileCount(char room[H][W], int row, int col, int h, int w){
  int dx[4]={0,1,0,-1};
  int dy[4]={1,0,-1,0};
  int nx,ny;
  int i,j;
  int cnt=1;
  if(row<0 || col<0 || col>=w || row>=h || room[row][col]=='#')return 0;
  room[row][col]='#';
  for(i=0;i<4;i++){
    nx=col+dx[i];
    ny=row+dy[i];
    cnt+=tileCount(room,ny,nx,h,w);
  }
  return cnt;
}

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

using namespace std;

char tyle[30][30]={'0'};

int find_tyle(int i,int j){
  if(tyle[i+1][j]=='.'){
    tyle[i+1][j]='a';
    find_tyle(i+1,j);
  }
  if(tyle[i][j+1]=='.'){
    tyle[i][j+1]='a';
    find_tyle(i,j+1);
  }
  if(tyle[i-1][j]=='.'){
    tyle[i-1][j]='a';
    find_tyle(i-1,j);
  }
  if(tyle[i][j-1]=='.'){
    tyle[i][j-1]='a';
    find_tyle(i,j-1);
  }
}

int main(){
  
  int w,h;
  
  while(cin>>w>>h,w){
    int ans=0;
    for(int i=1;i<=h;++i){
      for(int j=1;j<=w;++j){
      cin>>tyle[i][j];
      }
    }
    for(int i=1;i<=h;++i){
      for(int j=1;j<=w;++j){
	if(tyle[i][j]=='@'){
	  find_tyle(i,j);
	  tyle[i][j]='a';
	}
      }
    }
    for(int i=1;i<=h;++i){
      for(int j=1;j<=w;++j){
	if(tyle[i][j]=='a'){
	  ans++;
      }
    }
  }
    cout<<ans<<endl;

   for(int i=0;i<30;++i){
      for(int j=0;j<=30;++j){
      tyle[i][j]='0';
      }
    }
    
  }
}

C++: #include <bits/stdc++.h>
using namespace std;
/*{{{*/  //template
#define rep(i,n) for(int i=0;i<n;i++)
constexpr int INF = numeric_limits<int>::max()/2;
constexpr long long LINF = numeric_limits<long long>::max()/3;
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
#define sz(x) (int)(x).size()
#define debug(x) cerr<<#x<<":"<<x<<endl
#define debug2(x,y) cerr<<#x<<","<<#y":"<<x<<","<<y<<endl
//struct fin{ fin(){ cin.tie(0); ios::sync_with_stdio(false); } } fin_;
struct Double{ double d; explicit Double(double x) : d(x){} };
ostream& operator<<(ostream& os,const Double x){ os << fixed << setprecision(20) << x.d; return os; }
template<typename T> ostream& operator<<(ostream& os,const vector<T>& vec){ os << "["; for(const auto& v : vec){ os << v << ","; } os << "]"; return os; }
template<typename T,typename U> ostream& operator<<(ostream& os,const pair<T,U>& p){ os << "(" << p.first << ","<< p.second <<")"; return os; }
template<typename T> ostream& operator<<(ostream& os,const set<T>& st){ os<<"{"; for(T v:st) os<<v<<","; os <<"}"; return os; }
template<typename T,typename U> inline void chmax(T &x,U y){ if(y>x) x = y; }
template<typename T,typename U> inline void chmin(T &x,U y){ if(y<x) x = y; }
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
ll gcd(ll a,ll b){ if(b==0) return a; else return gcd(b,a%b); }
//constexpr double eps = 1e-14; 
constexpr double eps = 1e-10; 
constexpr ll mod = 1e9+7;
const int dx[]={1,0,-1,0} ,dy[] = {0,1,0,-1};
/*}}}*/

int w,h;
int sx,sy;

void solve(){
    vector<string> t(h);
    rep(i,h) cin >> t[i];

    rep(i,h) rep(j,w) if(t[i][j]=='@'){
        sx = i;
        sy = j;
        break;
    }

    bool visited[32][32];
    rep(i,32) rep(j,32) visited[i][j]=false;
    visited[sx][sy]=true;
    queue<pii> que;
    que.push(pii(sx,sy));
    while(!que.empty()){
        auto p = que.front();
        que.pop();

        int x = p.fi;
        int y = p.se;

        rep(i,4){
            int nx = x + dx[i];
            int ny = y + dy[i];
            if(nx<0 or ny<0 or nx>=h or ny>=w) continue;
            if(t[nx][ny]=='#') continue;
            if(visited[nx][ny]) continue;

            visited[nx][ny] = true;
            que.push(pii(nx,ny));
        }
    }

    int ans = 0;
    rep(i,h) rep(j,w) if(visited[i][j]) ans++;
    cout << ans << endl;
}

int main(){
    while(1){
        cin >> w >> h;
        if(w==0 and h==0) break;
        solve();
    }
}

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

#define INF 1.1e9
#define LINF 1.1e18
#define FOR(i,a,b) for (int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define BIT(x,n) bitset<n>(x)
#define PI 3.14159265358979323846

typedef long long ll;
typedef pair<int,int> P;
typedef pair<ll,P> PP;

//-----------------------------------------------------------------------------

int sx,sy,h,w;
string fld[20];
int dx[]={1,-1,0,0},dy[]={0,0,1,-1};

int bfs() {
	bool used[h][w];
	REP(i,h) REP(j,w) used[i][j]=false;
	int cnt=1;
	queue<P> q;
	used[sy][sx]=true;
	q.push(P(sx,sy));
	while(!q.empty()) {
		P p=q.front();q.pop();
		REP(i,4) {
			int nx=p.fi+dx[i],ny=p.se+dy[i];
			if(nx<0||nx>=w||ny<0||ny>=h||used[ny][nx]||fld[ny][nx]=='#') continue;
			used[ny][nx]=true;
			cnt++;
			q.push(P(nx,ny));
		}
	}
	return cnt;
}

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

	while(true) {
		cin>>w>>h;
		if(w==0) break;
		REP(i,h) {
			cin>>fld[i];
			REP(j,w) if(fld[i][j]=='@') sx=j,sy=i;
		}
		cout<<bfs()<<endl;
	}

	return 0;
}