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 <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>

#define rep(i, n) for(int i = 0; i < (n); i++)
#define pb push_back

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
int dx[4] = {0,1,0,-1};
int dy[4] = {-1, 0, 1, 0};
const int INF = 1 << 29;
const double EPS = 1e-9;
const int MAX_N = 20;
int W, H, ans = 1;
vector<string> field;

int dfs(int x, int y){
		rep(i, 4){
				int tx = x + dx[i], ty = y + dy[i];
				if(0 <= ty && 0 <= tx && ty < H && tx < W && field[ty][tx] == '.' ){
						ans++;
						field[ty][tx] = '#';
						dfs(tx, ty);
				}
		}
		return ans;
}

int main(){
		while(1){
				cin >> W >> H;
				if(W == 0 && H == 0) break;

				field.resize(H);
				rep(i,H) cin >> field[i];
				int sx, sy;

				//rep(i,H) cout << field[i] << endl;

				rep(y,H)rep(x,W)if(field[y][x] == '@'){ sx = x; sy = y; }

				//cout << sx << " " <<sy <<endl;
				dfs(sx, sy);
				//rep(i,H) cin >> field[i];
				cout << ans <<endl;
				ans = 1;
		}
}

C++: #include<iostream>

using namespace std;

void dfs(int mx,int my);

	char math[21][21];
	const int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
	int cnt;
	int x,y;
	int sx,sy;
	
int main(){
	
	while(cin>>x>>y,x&&y){
	    cnt=0;
		for(int i=0;i<y;i++){
			for(int j=0;j<x;j++){
				cin>>math[i][j];
				if(math[i][j]=='@'){
					sx=j;
					sy=i;
				}
			}
		}
		dfs(sx,sy);
		cout<<cnt+1<<endl;
	}
}

void dfs(int mx,int my){
	math[my][mx]='T';
	//移動する方向
	for(int i=0;i<4;i++){
		int nx=mx+dx[i],ny=my+dy[i];
		
		if(0<=nx && nx<x && 0<=ny && ny<y &&
		   (math[ny][nx]=='.' ||math[ny][nx]=='@')
		  )
		  {
			cnt++;
			dfs(nx,ny);
		}
	}
	return ;
}

C++: /*http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1130&lang=jp*/

#include <iostream>

using namespace std;

struct point{
    int x;
    int y;
};

char map[20][21];

int move(const struct point p,int h,int w){
    int count=1;
    map[p.x][p.y]='/';
    for (int i = 0; i < 4; ++i) {
        struct point tmp=p;

        switch (i){
            case 0:
                tmp.x++;
                break;
            case 1:
                tmp.x--;
                break;
            case 2:
                tmp.y++;
                break;
            case 3:
                tmp.y--;
                break;
            default:
                break;
        }
        if (tmp.x<w && tmp.y<h && map[tmp.x][tmp.y]=='.') {
            count+=move(tmp,h,w);
        }
    }
    return count;
}

int main(){
    int w,h;

    while (cin>>w>>h && (w>0 || h>0)){
        struct point start;

        for (int i = 0; i < h; ++i) {
            for (int j = 0; j < w; ++j) {
                cin>>map[j][i];
                if(map[j][i]=='@'){
                    start.x=j;
                    start.y=i;
                }
            }
        }
        cout<<move(start,h,w)<<endl;
    }
    return 0;
}

C++: #include<bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n ; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define whole(f, x, ...) ([&](decltype((x)) whole) { return (f)(begin(whole), end(whole), ## __VA_ARGS__); })(x) // decltypeで型取得、引数があればva_argsのところに入れる
using namespace std;
typedef long long ll; // long longをllでかけるようにした
const int INF = 1e9;
int n, m, ans = 0;
char W[100][100];

void dfs(int y, int x){
    W[y][x] = '#';
    for(int dx = -1; dx <= 1; dx++){
        for(int dy = -1; dy <= 1; dy++){
            if(dx == dy and dx != 0 and dy != 0) continue;
            else if(dx == -dy) continue;
            int nx = dx + x;
            int ny = dy + y;
            if(W[ny][nx] == '.' and 0 <= nx and nx < n and 0 <= ny and ny < m){
                dfs(ny, nx);
                ans++;
            }
        }
    }
    return ;
}

int main(void){
    while(true){
        ans = 0; // 新しいデータセットの度に初期化
        cin >> n >> m;
        if(n == 0 and m == 0) return 0;
        REP(i, m){
            REP(j, n){
                cin >> W[i][j];
            }
        }

        REP(i, m){
            REP(j, n){
                if(W[i][j] == '@'){
                    ans++;
                    dfs(i, j);
                }
            }
        }
        cout << ans << endl;
    }
}