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<string>
#include<vector>
#include<map>
#include<queue>
using namespace std;

int dx[4] = { -1, 1, 0, 0 };
int dy[4] = { 0, 0, -1, 1 };

int search(vector<string> v) {

  pair<int,int> init;
  for(int i=0; i<v.size(); i++) {
    for(int j=0; j<v[i].length(); j++) {
      if( v[i][j] == '@' )
	init.second = i,init.first = j;
    }
  }

  int res = 0;

  queue< pair<int,int> > que;
  que.push(init);
  while(!que.empty()) {
    pair<int,int> q = que.front();
    que.pop();

    for(int i=0; i<4; i++) {
      int x=q.first+dx[i];
      int y=q.second+dy[i];
      if( x >= 0 && x < v[q.second].length() ) {
	if( y >= 0 && y < v.size() ) {
	  if( v[y][x] == '.') {
	    v[y][x] = '@';
	    pair<int,int> p;
	    p.first = x;
	    p.second = y;
	    que.push(p);
	  }
	}
      }
    }

  }

  for(int i=0; i<v.size(); i++) {
    for(int j=0; j<v[i].length(); j++) {
      //cout<<v[i][j];
      if(v[i][j] == '@') res++;
    }
    //cout<<endl;
  }
  return res;

}

int main() {

  int w,h;
  while(true) {

    cin>>w>>h;
    if(!w&&!h) break;
    vector<string> v;
    string s;

    for(int i=0; i<h; i++) {
      cin>>s;
      v.push_back(s);
    }

    cout<<search(v)<<endl;
  }
}

C++: #include<stdio.h>

char room[21][21];
int count,W,H;

void root(int W_n,int H_n){
	if((H_n+1)<H&&room[H_n+1][W_n]=='.'){
		room[H_n+1][W_n]='#';
		count++;
		root(W_n,H_n+1);
	}
	if(W_n>0&&room[H_n][W_n-1]=='.'){
		room[H_n][W_n-1]='#';
		count++;
		root(W_n-1,H_n);
	}
	if(H_n>0&&room[H_n-1][W_n]=='.'){
		room[H_n-1][W_n]='#';
		count++;
		root(W_n,H_n-1);
	}
	if((W_n+1)<W&&room[H_n][W_n+1]=='.'){
		room[H_n][W_n+1]='#';
		count++;
		root(W_n+1,H_n);
	}

}


int main(void){
	
	scanf("%d%d",&W,&H);
	while(W!=0&&H!=0){
		count=1;
		for(int i=0;i<H;i++){
			scanf("%s",room[i]);
		}
		for(int i=0;i<H;i++){
			for(int j=0;j<W;j++){
				if(room[i][j]=='@'){
					root(j,i);
				}
			}
		}
		printf("%d\n",count);
		scanf("%d%d",&W,&H);
	}
	return 0;
}

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

using namespace std;
vector<int> ans;

int main(){
	while(1){
		int w,h;
		cin >> w >> h;
		if(w + h == 0) break;
		int cf[20][20];
		for(int i = 0;i < h;i++){
			for(int j = 0;j < w;j++){
				cf[i][j] = -1;
			}
		}
		bool field[20][20];
		for(int i = 0;i < h;i++){
			for(int j = 0;j < w;j++){
				field[i][j] = false;
			}
		}
		int px,py;
		map<int,int> ch;
		for(int i = 0;i < h;i++){
			for(int j = 0;j < w;j++){
				char t;
				cin >> t;
				if(t == '.') field[i][j] = true;
				else if(t == '@'){ px = j; py = i;field[i][j] = true;}
			}
		}
		
		int counta = 0;
		for(int i = 0;i < h;i++){
			for(int j = 0;j < w;j++){
				if(field[i][j]){
					bool ue = (i != 0 && cf[i-1][j] != -1);
					bool yoko = (j != 0 && cf[i][j-1] != -1);
					if(ue)cf[i][j] = cf[i-1][j];
					else if(yoko) cf[i][j] = cf[i][j-1];
					else{cf[i][j] = counta;counta++;}
					if(ue && yoko){
						if(cf[i-1][j] != cf[i][j-1]){
							if(cf[i-1][j] < cf[i][j-1]) ch[cf[i][j-1]] = cf[i-1][j];
							else ch[cf[i-1][j]] = cf[i][j-1];
						}
					}
				}
			}
		}
		for(int i = 0;i < h;i++){
			for(int j = 0;j < w;j++){
				if(cf[i][j] != -1){
					map<int,int>::iterator itr;
					while((itr = ch.find(cf[i][j])) != ch.end()){
						cf[i][j] = ch[cf[i][j]];
					}
				}
			}
		}
		
		int pn = cf[py][px];
		int count = 0;
		for(int i = 0;i < h;i++){
			for(int j = 0;j < w;j++){
				if(cf[i][j] == pn) count++;
			}
		}
		ans.push_back(count);
	}
	for(int i = 0;i < ans.size();i++) cout << ans[i] << endl;
	return 0;
}

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

//repetition
#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define rep(i, n) for(ll i = 0; i < (ll)(n); i++)

//container util
#define all(x) (x).begin(),(x).end()

//typedef
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VLL;
typedef vector<VLL> VVLL;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;

//const value
//const ll MOD = 1e9 + 7;
const int dx[] = {0,1,0,-1};//{0,0,1,1,1,-1,-1,-1};
const int dy[] = {1,0,-1,0};//{1,-1,0,1,-1,0,1,-1};

//conversion
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
inline ll toLL(string s) {ll v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

void dfs(int ci, int cj, vector<string>& board, VVI& visited, int h,int w){
  visited[ci][cj] = 1;
  rep(dir,4){
    if((ci + dy[dir]) < h && (cj + dx[dir]) < w &&
       (ci + dy[dir]) >= 0 && (cj + dx[dir]) >= 0  &&
       visited[(ci + dy[dir])][(cj + dx[dir])] == 0 &&
       board[(ci + dy[dir])][(cj + dx[dir])] == '.'){
      dfs((ci + dy[dir]),(cj + dx[dir]),board,visited,h,w);
    }
  }
}

void solve(int h, int w){
  vector<string> board(h);
  rep(i,h) cin >> board[i];
  VVI visited(h,VI(w,0));
  int sy = 0;
  int sx = 0;

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

  int cnt = 0;
  rep(i,h)rep(j,w) if(visited[i][j]) cnt++;
  cout << cnt << endl;
}
int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);
  int h,w;
  while(true){
    cin >> w >> h;
    if(h == 0 &&  w == 0){
      break;
    }
    solve( h,  w);
  }
  return 0;
}