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 int long long   // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;

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

int H, W;
vector<string> s;

bool inside(int x, int y) {
    return 0 <= x && x < H && 0 <= y && y < W;
}

int bfs(int sx, int sy) {
    queue<P> que;
    que.push(P(sx, sy));
    vector<vector<int>> d(H, vector<int>(W, INF));
    d[sx][sy] = 0;
    int ans = 0;

    while (!que.empty()) {
        P p = que.front(); que.pop();
        ans++;

        rep(k, 4) {
            int nx = p.first + dx[k], ny = p.second + dy[k];

            if (inside(nx, ny) && s[nx][ny] != '#' && d[nx][ny] == INF) {
                que.push(P(nx, ny));
                d[nx][ny] = d[p.first][p.second] + 1;
            }
        }
    }
    return ans;
}

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

    while (cin >> W >> H, W) {
        s.clear();
        s.resize(H);
        int sx, sy;
        rep(i, H) {
            cin >> s[i];
            rep(j, W) {
                if (s[i][j] == '@') {
                    sx = i, sy = j;
                }
            }
        }
        cout << bfs(sx, sy) << endl;
    }

}

C++: #include<iostream>
#include<queue>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
using namespace std;

class place{
public:
	int x;
	int y;
};

int main(){
	int W,H;
	while(1){
		cin>>W>>H;
		if(W==0 && H==0)break;
		bool dt[H][W];
		int startx,starty;
		rep(i,H){
			rep(j,W){
				char c;
				cin>>c;
				if(c=='.'){
					dt[i][j]=false;
				}else if(c=='#'){
					dt[i][j]=true;
				}else{
					startx=j;
					starty=i;
					dt[i][j]=false;
				}
			}
		}
		queue<place> que;
		place p;
		p.x=startx;
		p.y=starty;
		que.push(p);
		int cnt=0;
		int dirx[4]={1,0,-1,0};
		int diry[4]={0,1,0,-1};
		while(!que.empty()){
			p=que.front();
			que.pop();
			if(!dt[p.y][p.x])cnt++;
			dt[p.y][p.x]=true;
			rep(k,4){
				int nx=p.x+dirx[k];
				int ny=p.y+diry[k];
				if(nx>=0 && ny>=0 && nx<W && ny<H && !dt[ny][nx]){
					place pp;
					pp.x=nx;
					pp.y=ny;
					que.push(pp);
				}
			}
		}
		cout<<cnt<<endl;
	}
	return 0;
}

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

int w,h;
int mcnt=0;
char maps[22][22];
int dy[]={-1,0,1,0};
int dx[]={0,-1,0,1};

void bfs(int y,int x,int nowcnt){
  mcnt++;
  maps[y][x]='#';
  for(int i=0;i<4;i++){
    int ny=y+dy[i];
    int nx=x+dx[i];
    if(ny>=0 && ny<h && nx>=0 && nx<w && maps[ny][nx]!='#' ){
      bfs(ny,nx,nowcnt);
      //    maps[ny][nx]='.';
    }
  }
  return;
}
int main(){
  int sy,sx;
  while(1){
    mcnt=0;
    cin>>w>>h;
    if(!(w || h ))break;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	cin>>maps[i][j];
	if(maps[i][j]=='@'){
	  sy=i;
	sx=j;
	}
      }
    }
    bfs(sy,sx,0);
    cout<<mcnt<<endl;
  }
}

C++: // 2005à\IB Red and Black

#include <iostream>
#include <string>
#include <queue>

using namespace std;

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

int main(){
	int w, h;
	string b[20];
	while(cin >> w >> h, w){
		for(int i=0;i<h;i++) cin >> b[i];
		int sx, sy;
		for(int i=0;i<h;i++)
			for(int j=0;j<w;j++)
				if(b[i][j]=='@') sx = i, sy = j;
		queue< pair<int, int> > qu; qu.push(make_pair(sx,sy));
		int res = 0;
		while(!qu.empty()){
			pair<int, int> pr = qu.front(); qu.pop();
			int x = pr.first, y = pr.second;
			res++;
			for(int i=0;i<4;i++){
				int nx = x+dx[i], ny = y+dy[i];
				if(nx<0||h<=nx||ny<0||w<=ny||b[nx][ny]!='.') continue;
				b[nx][ny] = '#';
				qu.push(make_pair(nx,ny));
			}
		}
		cout << res << endl;
	}
}

C++: #include <cstdio>
#include <cstring>
#include <iostream>

int dxy[]={1,0,-1,0,1};
int ans=0;

char tile[21][21];
int H,W;

void dfs(int x, int y){
	if(tile[x][y]=='.'){
		ans++;
		tile[x][y]='#';
	}
	for(int i = 0; i < 4; ++i){
		int nx = dxy[i] + x, ny = y + dxy[i + 1];
		if(nx < H && nx >= 0 && ny >= 0 && ny < W){
			if(tile[nx][ny] == '.')dfs(nx, ny);
		}
	}
}

int main(){
	while(std::cin>>W>>H && W+H){
		ans = 0;
		memset(tile, '#', sizeof(tile));
		int sx,sy;
		for(int i = 0; i < H; ++i){
			for(int j = 0; j < W; ++j){
				char c;
				std::cin>>c;
				tile[i][j]=c;
				if(tile[i][j] == '@')sx = i, sy = j;
			}
		}
		tile[sx][sy] = '.';
		dfs(sx,sy);
		printf("%d\n",ans);
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
char tile[20][21];
bool vis[20][20];
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
int W,H;
int dfs(int x,int y) {
    int sum=1;
    vis[y][x]=1;
    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||tile[ny][nx]=='#'||vis[ny][nx]) continue;
        sum+=dfs(nx,ny);
    }
    return sum;
}
int main() {
    while(scanf("%d %d",&W,&H),W) {
        for(int i=0;i<H;i++) scanf("%s",tile[i]);
        int sx,sy;
        for(int i=0;i<H;i++) {
            for(int j=0;j<W;j++) if(tile[i][j]=='@') {sx=j;sy=i;tile[i][j]='.';}
        }
        memset(vis,0,sizeof(vis));
        printf("%d\n",dfs(sx,sy));
    }
}