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 <cstdio>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <algorithm>
#include <functional>

using namespace std;

#define rep(i,n) for((i)=0;(i)<(int)(n);(i)++)
#define foreach(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)

int W,H,res;
string board[30];

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

void dfs(int y, int x){
  int i;

  res++;
  board[y][x] = '#';
  rep(i,4){
    int my = y + dy[i];
    int mx = x + dx[i];
    if(0 <= my && my < H && 0 <= mx && mx < W && board[my][mx] == '.') dfs(my,mx);
  }
}

int main(){
  int i,j;

  while(1){
    scanf("%d %d",&W,&H);
    if(W == 0 && H == 0) break;

    rep(i,H) cin >> board[i];

    res = 0;
    rep(i,H) rep(j,W) if(board[i][j] == '@') dfs(i,j);
    printf("%d\n",res);
  }

  return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;
int cnt = 0;
void depth_first_search(int,int,vector< vector<char> > &);
int main(){

  while(true){
    int w,h;
    cin >> w;
    cin >> h;
    
    int fx;
    int fy;
    if(w == 0 && h ==0 )return 0;
    vector< vector<char> > map(w, vector<char>(h));
    for(int i = 0;i < h;i++){
      for(int j = 0;j < w;j++){
	cin >> map[j][i];
	
	if(map[j][i] == '@'){
	  fx = j;
	  fy = i;
	}
      }
    }
    cnt = 1;
    depth_first_search(fx,fy,map);
    cout << cnt << endl;
  }
  return 0;
}
void depth_first_search(int x,int y,vector< vector<char> > &map){
  int dx[4] = {0,1,-1,0};
  int dy[4] = {1,0,0,-1};

  for(int i = 0;i < 4;i++){
    if(!(x+dx[i] == -1 
	|| x+dx[i] == map.size() 
	|| y+dy[i] == -1 
	|| y+dy[i] == map[0].size()
	)
	&& map[x+dx[i]][y+dy[i]] == '.' 
       ){
      map[x+dx[i]][y+dy[i]] = '1';
      cnt++;
      
      depth_first_search(x+dx[i],y+dy[i],map);
      
    }
  }
}

C++: #include <iostream>
#include <vector>
using namespace std;
int solve(bool visited[20][20],vector<string> v,int x,int y,int w,int h){
	if(x<0||y<0||w<=x||h<=y) return 0;
	if(visited[y][x]==true||v[y][x]=='#') return 0;
	int sum=1;
	visited[y][x]=true;
	sum+=solve(visited,v,x+1,y,w,h);
	sum+=solve(visited,v,x,y+1,w,h);
	sum+=solve(visited,v,x-1,y,w,h);
	sum+=solve(visited,v,x,y-1,w,h);
	return sum;
}
int main(void){
	int w,h;
	while(cin>>w>>h,w||h){
		vector<string> v;
		bool visited[20][20];
		for(int i=0;i<20;++i) for(int j=0;j<20;++j)visited[i][j]=false;
		int x,y;
		for(int i=0;i<h;++i){
			string s;cin>>s;
			if(s.find("@")!=-1){x=s.find("@");y=i;}
			v.push_back(s);
		}
		cout<<solve(visited,v,x,y,w,h)<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <utility>
#ifndef LIB_DEBUG_HPP
#define LIB_DEBUG_HPP
#ifdef DEBUG
#include <cassert>
#define debug(a) a
#else
#define debug(a)
#endif
#endif
#ifndef repeat
#define repeat(i,n) for (int i = 0; i < (n); ++i)
#endif
#ifndef repeat_from
#define repeat_from(i,m,n) for (int i = (m); i < (n); ++i)
#endif
#ifndef repeat_one
#define repeat_one(i,n) for (int i = 1; i <= (n); ++i)
#endif
#ifndef repeat_rev
#define repeat_rev(i,n) for (int i = (n)-1; 0 <= i; --i)
#endif
#ifndef foreach
#define foreach(it, cont) for (typeof(cont.begin()) it = cont.begin(); it != cont.end(); ++it)
#endif
#ifndef LIB_TYPEDEF_LL_HPP
#define LIB_TYPEDEF_LL_HPP
typedef long long ll;
typedef unsigned long long ull;
#endif
#ifndef LIB_DEBUG_HPP
#define LIB_DEBUG_HPP
#ifdef DEBUG
#include <cassert>
#define debug(a) a
#else
#define debug(a)
#endif
#endif
using namespace std;
const int D4[4][2] = { {0,-1}, {0,1}, {-1,0}, {1,0} };
int H, W;
vector<vector<bool> > tile;
vector<vector<bool> > used;
int dfs(int h, int w) {
    if (used[h][w]) return 0;
    used[h][w] = true;
    int acc = 1;
    repeat (d,4) {
        int h0 = h + D4[d][0];
        int w0 = w + D4[d][1];
        if (0 <= h0 and h0 < H and 0 <= w0 and w0 < W and not used[h0][w0]) {
            if (tile[h0][w0] == false) {
                acc += dfs(h0, w0);
            }
        }
    }
    return acc;
}
int main() {
    ios_base::sync_with_stdio(false);
    while (true) {
        cin >> W >> H; cin.ignore();
        if (W == 0 and H == 0) break;
        tile = vector<vector<bool> >(H, vector<bool>(W));
        pair<int,int> p;
        repeat (h,H) {
            repeat (w,W) {
                char c; cin >> c;
                if (c == '.') {
                    tile[h][w] = false;
                } else if (c == '#') {
                    tile[h][w] = true;
                } else if (c == '@') {
                    tile[h][w] = false;
                    p = make_pair(h,w);
                } else {
                    debug (assert (false));
                }
            }
            cin.ignore();
        }
        used = vector<vector<bool> >(H, vector<bool>(W, false));
        cout << dfs(p.first, p.second) << endl;
    }
    return 0;
}