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 <string>
#include <queue>
using namespace std;

class DoubleArray {
    int X,Y;
    int *top;
    public:
    DoubleArray(int *p,int x,int y);
    int *operator[](int i);
    int getx();
    int gety();
};

DoubleArray::DoubleArray(int *p,int x,int y){
    top = p;
    X = x;
    Y = y;
}

int *DoubleArray::operator[](int i){
    return top + Y*i;
}
int DoubleArray::getx(){
    return X;
}
int DoubleArray::gety(){
    return Y;
}

int calc(DoubleArray &map){
    int i,j,a=0;
    int x = map.getx();
    int y = map.gety();
    queue<int> q;
    for(i=0;i<x;i++){
        for(j=0;j<y;j++){
            if(map[i][j]==2){
                q.push(i);
                q.push(j);
                a=1;
                map[i][j]=0;
            }
        }
    }
    while(!q.empty()){
        i=q.front();
        q.pop();
        j=q.front();
        q.pop();
        if(i>0) if(map[i-1][j]==1){
            q.push(i-1);
            q.push(j);
            a++;
            map[i-1][j]=0;
        }
        if(j>0) if(map[i][j-1]==1){
            q.push(i);
            q.push(j-1);
            a++;
            map[i][j-1]=0;
        }
        if(i<x-1) if(map[i+1][j]==1){
            q.push(i+1);
            q.push(j);
            a++;
            map[i+1][j]=0;
        }
        if(j<y-1) if(map[i][j+1]==1){
            q.push(i);
            q.push(j+1);
            a++;
            map[i][j+1]=0;
        }
    }
    return a;
}

int main(){
    int i,j,x,y,a[10000],b = 0;
    string c;
    while(114514){
        cin >> x;
        cin >> y;
        if(x==0&&y==0) break;
        if(x>20) x=20;
        if(y>20) y=20;
        if(x<1||y<1) continue;
        int m[x][y];
        DoubleArray map(&m[0][0],x,y);
        for(j=0;j<y;j++){
            for(i=0;i<x;i++){
                cin >> c;
                if(c=="#") map[i][j]=0;
                if(c==".") map[i][j]=1;
                if(c=="@") map[i][j]=2;
            }
        }
        a[b++] = calc(map);
    }
    for(i=0;i<b;i++){
        cout << a[i] << endl;
    }
    return 0;
}

C++: #include <iostream>
#define MAX 20
using namespace std;

typedef pair<int,int> P;
int X, Y, ans;

char field[MAX][MAX];
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};

void bfs(int px, int py)
{
     field[px][py] = '#';
     ans++;
     for (int i = 0; i < 4; i++) {
	  int x = px + dx[i], y = py + dy[i];
	  if (0 <= x && x < X && 0 <= y && y < Y && field[x][y] == '.') bfs(x,y);
     }
}

int main(void)
{
     int x, y;
     while (cin >> Y >> X && X && Y) {
	  for (int i = 0; i < X; i++)
	       for (int j = 0; j < Y; j++) {
		    cin >> field[i][j];
		    if (field[i][j] == '@') {
			 x = i;
			 y = j;
		    }
	       }
	  ans = 0;
	  bfs(x,y);
	  cout << ans << endl;
     }

     return 0;
}

C++: //#include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <cmath>
#include <vector>
#include <cstring>
#include <climits>
#include <queue>
#include <utility>

#define INF INT_MAX / 2
using namespace std;

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

int dx[] = {-1,0,1,0};
int dy[] = {0,-1,0,1};
bool used[30][30];

int bfs(int sy,int sx,const vector<vector<int> >&tiles){
	queue<pair<int,int> > q;
	q.push(make_pair(sy,sx));
	int X = tiles[0].size();
	int Y = tiles.size();
	memset(used,0,sizeof(used));
	used[sy][sx] = true;
	int ret= 0;
	while(!q.empty()){
		P p = q.front();q.pop();
		int y = p.first;int x = p.second;
		for(int i=0;i<4;i++){
			int ny = y + dy[i];
			int nx = x + dx[i];
			if( ny < 0 || nx < 0 || ny >= Y || nx >= X || used[ny][nx] || tiles[ny][nx]==0)continue;
			used[ny][nx] = true;
			q.push(make_pair(ny,nx));
			ret++;
		}
	}
	return ret+1;
}

int main(void) {
	int w,h;
	while(cin >> w >> h,w+h!=0){
	vector<vector<int> >tiles(h,vector<int>(w));
	int sx,sy;
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			char c;cin >> c;
			if(c=='.')
				tiles[i][j] = 1;
			else if(c=='#')
				tiles[i][j] = 0;
			else{
				tiles[i][j] = 1;
				sx = j;sy = i;
			}
		}
	}
	cout << bfs(sy,sx,tiles) << endl;
}
}

// command ctrl 上下
// command D
// command caps G → '' ""
// vector<vector<int>> a(N,vector<int>(M,-1));

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cstdio>
using namespace std;


int W, H;
char Map[25][25];
int d[4][2]={{0,1},{1,0},{0,-1},{-1,0}};

int dfs(int x, int y){
	if(Map[y][x] == '#') return 0;
	Map[y][x] = '#';
	int ret = 1;
	for(int i=0;i<4;i++){
		ret += dfs(x+d[i][1],y+d[i][0]);
	}
	return ret;
}
main(){
	int i,j;
	while(cin >> W >> H, W){
		int sx, sy;
		for(i=0;i<=H+1;i++)
			for(j=0;j<=W+1;j++)
				Map[i][j] = '#';
		for(i=1;i<=H;i++){
			for(j=1;j<=W;j++){
				cin >> Map[i][j];
				if(Map[i][j] == '@'){
					sx = j;sy = i;
				}
			}
		}
		cout << dfs(sx, sy) << endl;
	}
	return 0;
}

C++: #include <iostream>

using namespace std;

#define MAX 21
 
string str[MAX];
int n,m,cnt,dx[4] = {1,0,-1,0},dy[4] = {0,1,0,-1};
 
void dfs(int x,int y){
    str[x][y] = '#';
 
    for(int i = 0 ; i < 4 ; i++){
	int nx = x + dx[i] , ny = y + dy[i];
	if(0 <= nx && nx < m && 0 <= ny && ny < n){
	    if(str[nx][ny] == '.'){
		cnt++;
		dfs(nx,ny);
	    }
	}
    }
}
 
int main(){
    while(cin >> n >> m , n + m){
	for(int i = 0 ; i < m ; i++){
	    cin >> str[i];
	}
     
	int x,y;
	for(int i = 0 ; i < n ; i++){
	    for(int j = 0 ; j < m ; j++){
		if(str[j][i] == '@'){
		    x = j;
		    y = i;
		    break;
		}
	    }
	}
	cnt = 1;
	dfs(x,y);
	cout << cnt << endl;
    }
    return 0;
}