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 <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
typedef long long ll;
const int INF = 100000000;
const int MOD = 1000000007;
int cou=0;
void dfs(int x,int y,int a,int b,vector<vector<char> > &v){
  v[x][y]='#';
  cou++;
  
  int dx[4]={0,0,-1,1},dy[4]={-1,1,0,0};
  for (int i = 0; i < 4; i++) {
    /* code */
    int nx=x+dx[i],ny=y+dy[i];
    if(0<=nx&&nx<a&&0<=ny&&ny<b&&v[nx][ny]=='.')dfs(nx,ny,a,b,v);
  }
}
int main(){
  for (;;) {
    int a,b;
    cou=0;
    std::cin >> a>>b;
    if(a==0&&b==0)exit(0);
    vector<vector<char> > v(a,vector<char>(b));
    int s1,s2;
     rep(j,b){
      rep(i,a){
        std::cin >> v[i][j];
        if(v[i][j]=='@'){s1=i;s2=j;}
      }
    }
    dfs(s1,s2,a,b,v);
    /* code */
    std::cout << cou << std::endl;
  }
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <functional>
#include <set>
#include <sstream>
#include <map>
#include <queue>

using namespace std;
 
const double eps=1e-10;

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

int main()
{

	int w,h;
	while(cin>>w>>h,w){
		vector<string> t(h);
		vector< vector<bool> > visited(h,vector<bool>(w,false));
		int x,y,res=0;
		for(int i=0;i<h;i++){
			cin>>t[i];
			if(t[i].find("@")!=string::npos){
				x=t[i].find("@"); y=i;
			}
		}

		queue< pair<int,int> > q;
		q.push(make_pair(y,x));

		while(!q.empty()){
			pair<int,int> p=q.front();
			q.pop();
			y=p.first; x=p.second;

			if(!visited[y][x]){
				res++;
				visited[y][x]=true;
				for(int i=0;i<4;i++){
					int nx=x+dx[i],ny=y+dy[i];
					if(0<=nx && nx<w && 0<=ny && ny<h && t[ny][nx]=='.') q.push(make_pair(ny,nx));
				}
			}
		}
		cout<<res<<endl;

	}


	return 0;
}

C++: #include <iostream>
#include <math.h>
#include <algorithm>
#include <string>
#include <stack>
#include <queue>
#include <set>
#include <cstdio>
#include <string.h>
using namespace std;

#define FOR(i, n) for(int i = 0; i < n; i++)
#define bit(i) static_cast<bitset<8> >(i)

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long long, long long>PLL;
typedef queue<int> QI;
typedef priority_queue<int> maxpq;
typedef priority_queue<int, vector<int>, greater<int> > minpq;

struct edge{int to, cost;};

void begin(){cin.tie(0); ios::sync_with_stdio(false);};
int gcd(int a, int b){if(a%b==0){return(b);}else{return(gcd(b,a%b));}};
int lcm(int m, int n){if((0 == m)||(0 == n)){return 0;} return ((m / gcd(m, n)) * n);};
unsigned long long comb(long n, long m){unsigned long long c = 1; m = (n - m < m ? n - m : m);
    for(long ns = n - m + 1, ms = 1; ms <= m; ns ++, ms ++){c *= ns; c /= ms;} return c;};

// ワーシャルフロイド法
void warshallFloyd(int d[100][100], int n){
    for(int k = 0; k < n; ++k)
        for(int i = 0; i < n; ++i)
            for(int j = 0; j < n; ++j)
                if(d[i][k] != -1 && d[k][j] != -1){
                    if(d[i][j] == -1){
                        d[i][j] = d[i][k] + d[k][j];
                    }else{
                        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
                    }
                }
};


// d:隣接行列  n:グラフのサイズ  s:始点  dist:始点からの距離が入る配列
void dijkstra(int d[1000][1000], int n, int s, int dist[1000]){
    FOR(i, n) dist[i] = -1;
    dist[s] = 0;
    priority_queue<PII, vector<PII>, greater<PII> > q;
    q.push(PII(0, s));
    while (!q.empty()) {
        PII p = q.top(); q.pop();
        int i = p.second;
        if(dist[i] < p.first) continue;
        for(int j = 0; j < n; j++){
            if(d[i][j] == -1) continue;
            if(dist[j] == -1){
                dist[j] = dist[i] + d[i][j];
                q.push(PII(dist[j], j));
            }else if(dist[j] > dist[i] + d[i][j]){
                dist[j] = dist[i] + d[i][j];
                q.push(PII(dist[j], j));
            }
        }
    }
};

// 線分の交差判定
bool isCross(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4){
    // 並行な場合
    int m = (x2-x1)*(y4-y3)-(y2-y1)*(x4-x3);
    if(m == 0) return false;
    // 媒介変数の値が0より大きく1以下なら交差する、これは問題の状況によって変わるかも。
    double r =(double)((y4-y3)*(x3-x1)-(x4-x3)*(y3-y1))/m;
    double s =(double)((y2-y1)*(x3-x1)-(x2-x1)*(y3-y1))/m;
    return (0 < r && r <= 1 && 0 < s && s <= 1);
};

void cp(int a1[], int a2[], int l){FOR(i, l) a2[i] = a1[i];};



/**
 * start
 * @author yoshikyoto
 */
int h, w;

int dfs(string tile[], int y, int x){
    int count = 1;
    // 上
    if(y > 0 && tile[y-1][x] == '.'){
        tile[y-1][x] = '#';
        count += dfs(tile, y-1, x);
    }
    
    // 下
    if(y < h-1 && tile[y+1][x] == '.'){
        tile[y+1][x] = '#';
        count += dfs(tile, y+1, x);
    }
    
    // 左
    if(x > 0 && tile[y][x-1] == '.'){
        tile[y][x-1] = '#';
        count += dfs(tile, y, x-1);
    }
    
    // 右
    if(x < w-1 && tile[y][x+1] == '.'){
        tile[y][x+1] = '#';
        count += dfs(tile, y, x+1);
    }
    
    return count;
};
       
int main(int argc, const char * argv[]){
    begin();
    while(true){
        cin >> w >> h;
        if(h == 0 && w == 0) return 0;
        
        string tile[20];
        FOR(i, h) cin >> tile[i];
        
        int sy = 0, sx = 0;
        FOR(i, h) FOR(j, w){
            if(tile[i][j] == '@'){
                sy = i; sx = j;
                break;
            }
        }
        
        tile[sy][sx] = '#';
        cout << dfs(tile, sy, sx) << endl;
    }
}