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

using namespace std;

int d[] = {-1,1,0,0};

int count(int x,int y, char map[22][22]){
        int c = 0; 
        map[x][y] = '#';
        c++;
        for(int i = 0; i < 4; i++){
                int nx = x+d[i];
                int ny = y+d[3-i];
                if(map[nx][ny] == '.'){
                        c += count(nx,ny,map);
                }  
        }  
        return c; 
}

int main(){
        int w,h;
        cin >> w >> h; 
        while(w){
                char map[22][22];
                memset(map,'#',sizeof(map));
                int x,y;
                for(int i = 1; i <= h; i++){
                        for(int j = 1; j <= w; j++){
                                cin >> map[i][j];
                                if(map[i][j] == '@'){
                                        x = i; y = j; 
                                        map[i][j] = '.';
                                }  
                        }  
                }  
                cout << count(x,y,map) << endl;
                cin >> w >> h; 
        }  
}

C++: #include<bits/stdc++.h>
using namespace std;
#define rep(i,N) for(int (i)=0;(i) < (N); (i)++)

const int d[4] = {0,0,-1,1};

int dfs(int& H, int& W, vector<vector<char>>& tiles, vector<vector<bool>>& visited, pair<int,int> now){
    int h = now.first;
    int w = now.second;
    int res = 0;
    if(h < 0 || h >= H || w < 0 || w >= W) return 0;
    if(tiles[h][w] == '#' || visited[h][w]) return 0;

    visited[h][w] = true;
    res++;

    for(int i=0;i<4;i++){
        int nh = h + d[i];
        int nw = w + d[i^2];
        res += dfs(H,W,tiles,visited,{nh,nw});
    }

    return res;
}

int main(){
    int H,W;
    while(cin >> W >> H, H != 0 && W != 0){
        vector<vector<char>> tiles(H,vector<char>(W));
        vector<vector<bool>> visited(H,vector<bool>(W,false));
        pair<int,int> start;

        for(int h=0;h<H;h++){
            for(int w=0;w<W;w++){
                cin >> tiles[h][w];
                if(tiles[h][w] == '@') start = {h,w};
            }
        }

        cout << dfs(H, W, tiles, visited, start) << endl;
    }
}

C++: // Red and Black.cpp : ??????????????? ??¢????????±????????§????????¨????????? ?????????????????????????????????
//

#include "stdafx.h"
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;
//Global
int x, y;
const int MAX_N = 20;
char maze[MAX_N][MAX_N];
const int INF = 1000000;
int X, Y;//"@"???????????????
int memo[MAX_N][MAX_N];
int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,1,0,-1 };
int counter;

void bsd(int a, int b) {
	maze[b][a] = '#';
	memo[b][a] = INF;
	counter++;
	for (int i = 0; i < 4; i++) {
		int nx = a + dx[i];
		int ny = b + dy[i];
		if (memo[ny][nx] == 0 && maze[ny][nx] == '.' && 0 <= nx && nx < x && 0 <= ny && ny < y) {
			bsd(nx, ny);
		}
	}

}



//solve
//??±???????????¢?´¢??§"@"??¨???????????£?????????"."????????°????±???????
void solve()
{
	//memo????????????
	for (int i = 0; i < y; i++) {
		for (int j = 0; j < x; j++) {
			memo[i][j] = 0;
		}
	}

	//"@"??¨??£??\????????????  ??????  INF??§??????  ??????  ????????????????????£?????????????????????"."??§??????
	counter = 0;//"@"??????
	bsd(X, Y);


	cout << counter << endl;

}






/*
void solve() {
	for (int i = 0; i < y; i++) {
		for (int j = 0; j < x; j++) {
			cout << maze[i][j];
		}
		cout << endl;
	}
	cout << "@???????????????" << X << Y << endl;
}
*/








//main
int main()
{
	while (true) {
		cin >> x >> y;
		if (x == 0 && y == 0)break;
		//"W"??¨"."??¨"@"?????\???
		for (int i = 0; i < y; i++) {
			for (int j = 0; j < x; j++) {
				cin >> maze[i][j];
				if (maze[i][j] == '@') {
					X = j; Y = i;//@???????????????????¨????
				}
			}
		}
		solve();
	}
    return 0;
}

C++: #include <cstdio>
using namespace std;

int n, m;
char map[20][20];
int ans = 0;

int dfs(int x, int y){
	if(0 <= x && x < m && 0 <= y && y < n){
		if(map[x][y] != '.') return 0;
		map[x][y] = '@';
		dfs(x-1, y);
		dfs(x+1, y);
		dfs(x, y-1);
		dfs(x, y+1);
	}
	return 0;
}

int main(){
	while(1){
		for(int i=0; i<20; i++){
			for(int j=0; j<20; j++){
				map[i][j] = '#';
			}
		}
		scanf("%d %d", &n, &m);
		if(n == 0 && m == 0) break;
		for(int i=0; i<m; i++){
			scanf("%s", map[i]);
		}
		int x, y;
		for(int i=0; i<m; i++){
			for(int j=0; j<n; j++){
				if(map[i][j] == '@'){
					x = i;
					y = j;
				}
			}
		}
		dfs(x-1, y);
		dfs(x+1, y);
		dfs(x, y-1);
		dfs(x, y+1);
		for(int i=0; i<m; i++){
			for(int j=0; j<n; j++){
				if(map[i][j] == '@') ans++;
			}
		}
		printf("%d\n", ans);
		ans = 0;
	}
	return 0;
}

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

#define INF 1.1e9
#define LINF 1.1e18
#define FOR(i,a,b) for (int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define BIT(x,n) bitset<n>(x)
#define PI 3.14159265358979323846

typedef long long ll;
typedef pair<int,int> P;
typedef pair<ll,P> PP;

//-----------------------------------------------------------------------------

int sx,sy,h,w;
string fld[20];
int dx[]={1,-1,0,0},dy[]={0,0,1,-1};

int bfs() {
	bool used[h][w];
	REP(i,h) REP(j,w) used[i][j]=false;
	int cnt=1;
	queue<P> q;
	used[sy][sx]=true;
	q.push(P(sx,sy));
	while(!q.empty()) {
		P p=q.front();q.pop();
		REP(i,4) {
			int nx=p.fi+dx[i],ny=p.se+dy[i];
			if(nx<0||nx>=w||ny<0||ny>=h||used[ny][nx]||fld[ny][nx]=='#') continue;
			used[ny][nx]=true;
			cnt++;
			q.push(P(nx,ny));
		}
	}
	return cnt;
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	while(true) {
		cin>>w>>h;
		REP(i,h) {
			cin>>fld[i];
			REP(j,w) if(fld[i][j]=='@') sx=j,sy=i;
		}
		cout<<bfs()<<endl;
	}

	return 0;
}