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 <algorithm>

using namespace std;

struct DataSet{
    int x, y;
    int ax, ay;
    string str;
};

int search(int xSize, int ySize, int x, int y, string& s){
    //cout << '[' << x << ',' << y << ']' << endl;
    int ret = 0;
    int nowPos = x + y * xSize;
    s[nowPos] = '#';
    ++ret;
    
    if(s[nowPos - 1] == '.' && x != 0){
        ret += search(xSize, ySize, x-1, y, s);
    }
    if(s[nowPos + 1] == '.' && x + 1 != xSize){
        ret += search(xSize, ySize, x+1, y, s);
    }
    if(s[nowPos - xSize] == '.' && y != 0){
        ret += search(xSize, ySize,x, y-1, s);
    }
    if(s[nowPos + xSize] == '.' && y + 1 != ySize){
        ret += search(xSize, ySize, x, y+1, s);
    }
    return ret;
}

int main()
{
    vector<DataSet> dataSets;
    char word;
    
    while(cin){
        DataSet dataSet;
        cin >> dataSet.x >> dataSet.y;
        for(int i = 0; i < dataSet.y * dataSet.x; i++){
            cin >> word;
            if(word == '@'){
                dataSet.ax = i % dataSet.x;
                dataSet.ay = i / dataSet.x;
            }
            //cout << line << endl;
            dataSet.str += word;
        }
        dataSets.push_back(dataSet);
    }
    dataSets.pop_back();
    
    //cout << dataSets.back().x << ' ' << dataSets.back().y << endl;
    //cout << dataSets.back().ax << ' ' << dataSets.back().ay << endl;
    //cout << dataSets.back().str;
    
    for(auto dataSet : dataSets){
        if(dataSet.x == 0) continue;
        cout << search(dataSet.x, dataSet.y, dataSet.ax, dataSet.ay, dataSet.str) << endl;
    }
    
    return 0;
}

C++: #include <iostream>
using namespace std;
int ch[22][22],cnt;
int dfs(int,int);
int main(){
  int w,h,i,j,k,l,result;
  string in[20];
  while(1){
    cnt=0;
    cin >> w >> h;
    if(w==0&&h==0) break;
    for(i=0;i<h;i++){
      cin >> in[i];
    }
    for(i=0;i<h+2;i++){
      for(j=0;j<w+2;j++){
	ch[i][j]=1;
      }
    }
    for(i=0,k=1;k<h+1;i++,k++){
      for(j=0,l=1;l<w+1;l++,j++){
	if(in[i][j]=='.') ch[k][l]=0;
	else if(in[i][j]=='#') ch[k][l]=1;
	else ch[k][l]=2;
      }
    }
    for(i=1;i<h;i++){
      for(j=1;j<w;j++){
	if(ch[i][j]==2) result=dfs(i,j);
      }
    }
    cout << cnt << endl;
  }
  return 0;
}
int dfs(int i,int j){
  if(ch[i][j]==1) return 0;
  ch[i][j]=1;
  cnt++;
  dfs(i+1,j);
  dfs(i-1,j);
  dfs(i,j-1);
  dfs(i,j+1);
  return cnt;
}

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

#define FOR( i, m, n )  for( int (i) = (m); (i) < (n); (i)++ )
#define REP( i, n )     FOR( i, 0, n )
#define ALL( a )        (a).begin(), (a).end()             

#define F				first
#define S				second

int w, h, cnt = 0;

void solve( vector<vector<char>>& f, pair<int, int> mp ) {

	if( mp.F - 1 >= 0 && f[mp.S][mp.F - 1] == '.' ) {
		cnt++;
		f[mp.S][mp.F - 1] = '#';
		solve( f, make_pair( mp.F - 1, mp.S ) );
	}
	if( mp.F + 1 < w && f[mp.S][mp.F + 1] == '.' ) {
		f[mp.S][mp.F + 1] = '#';
		cnt++;
		solve( f, make_pair( mp.F + 1, mp.S ) );
	}
	if( mp.S - 1 >= 0 && f[mp.S - 1][mp.F] == '.' ) {
		f[mp.S - 1][mp.F] = '#';
		cnt++;
		solve( f, make_pair( mp.F, mp.S - 1 ) );
	}
	if( mp.S + 1 < h && f[mp.S + 1][mp.F] == '.' ) {
		f[mp.S + 1][mp.F] = '#';
		cnt++;
		solve( f, make_pair( mp.F, mp.S + 1 ) );
	}
}

int main() {

	while( 1 ) {
		cin >> w >> h; if( w == 0 && h == 0 ) break;
		
		pair<int, int> mp;
		vector<vector<char>> f;
		vector<char> tmp( w );
		REP( i, h ) {
			REP( j, w ) {
				cin >> tmp[j];
				if( tmp[j] == '@' ) { 
					mp.first = j;
					mp.second = i; 
					tmp[j] = '#';
					cnt++;
				}
			}
			f.push_back( tmp );
		}
		solve( f, mp );
		cout << cnt << endl;
		cnt = 0;
	}
}

C++: #include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctype.h>
#include <ctime>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <numeric>
#include <complex>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <iostream>
#include <iterator>
#include <algorithm>

using namespace std;

#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define lengthof(x) (sizeof(x) / sizeof(*(x)))
#define FILL(ptr, value) FILL_((ptr), sizeof(ptr)/sizeof(value), (value))
template <typename T>
void FILL_(void * ptr, size_t size, T value){
  std::fill((T*)ptr, (T*)ptr+size, value);
}
//4方向ベクトル→↑←↓
int dx[] ={1,0,-1,0};
int dy[] ={0,-1,0,1};
int w,h;
char board[25][25];
int sx,sy;
int nx,ny;
int ans;
void dfs(int x,int y){
  board[y][x] ='#';
  for(int j = 0;j <4;j++){
    nx =x +dx[j];
    ny =y +dy[j];
    if(nx <1 ||nx >w||ny <1 ||ny >h)
      continue;
    if(board[ny][nx] =='.'){
      ans++;
      dfs(nx,ny);
    }
  }
}

int main()
{
  while(cin>>w>>h,w||h){
    ans = 1;
    for(int i = 1;i <=h;i++){
      for(int j = 1;j <=w;j++){
	cin>>board[i][j];
      }
    }
    for(int i = 1;i <=h;i++){
      for(int j = 1;j <=w;j++){
	if(board[i][j] =='@'){
	  sx = j;
	  sy = i;
	  dfs(j,i);
	}
      }
    }
    cout <<ans<<endl;
  }
  return 0;
}