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 s;
    char c;
    for(b=0; b<10000; b++){
        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++){
            cin >> s;
            for(i=0;i<x;i++){
                c = s[i];
                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>
#include <cmath>
#include <algorithm>

using namespace std;

const char RED_TILE = '#';
const char AT_TILE = '@';

int vx[4] = {0,1,0,-1};
int vy[4] = {-1,0,1,0};

int w,h;
int visited[20][20];
int map[20][20];

int search(int x, int y){
	int count = 1;
	visited[x][y] = true;
	for (int i=0;i<4;++i){
		int tx=x+vx[i];
		int ty=y+vy[i];
		if (tx<0 || tx>=w || ty<0 || ty>=h) continue;
		if (!visited[tx][ty] && map[tx][ty]){
			count += search(tx,ty);
		}
	}
	return count;
}

int main(){
	while (true){
		cin >> w >> h;
		if (w==0 && h==0) break;
		int sx, sy;
		for (int i=0;i<h;++i){
			string s;
			cin >> s;
			for (int l=0;l<w;++l){
				if (s[l] == RED_TILE) {
					map[l][i] = false;
				} else if (s[l] == AT_TILE) {
					map[l][i] = true;
					sx = l;
					sy = i;
				} else {
					map[l][i] = true;
				}
			}
		}
		for (int i=0;i<20;++i) for (int l=0;l<20;++l) visited[i][l]=false;
		int result = search(sx,sy);
		cout << result << endl;
	}
	return 0;
}

C++: #include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
#include<climits>
#include<string>
#include<set>
#include<numeric>
#include<map>
#include<iostream>
using namespace std;
#define rep(i,n) for(int i = 0;i<((int)(n));i++)
#define reg(i,a,b) for(int i = ((int)(a));i<=((int)(b));i++)
#define irep(i,n) for(int i = ((int)(n)-1);i>=0;i--)
#define ireg(i,a,b) for(int i = ((int)(b));i>=((int)(a));i--)
typedef long long ll;
typedef pair<ll, ll> mp;

int W,H,f[20][20],ans[50],cnt=0,sx,sy;
string m[20],t,s;

void find(int x,int y){
	if(x>=W || x<0 || y>=H || y<0)return;
	t = m[x][y];
	if(t=="#")return;
	if(f[x][y]!=0)return;
	f[x][y] = 1;
	ans[cnt]++;
	find(x+1,y);
	find(x-1,y);
	find(x,y+1);
	find(x,y-1);
}

int main(void){
	while(1){
		cin>>W>>H;
		if(W==0 && H==0)break;
		ans[cnt]=0;
		rep(i,H){
			cin>>s;
			rep(j,W){
				f[j][i]=0;
				m[j][i]=s[j];
				t=m[j][i];
				if(t=="@"){
					sx=j;
					sy=i;
				}
			}
		}
		find(sx,sy);
		cnt++;
	}
	rep(i,cnt)cout<<ans[i]<<endl;
	return 0;
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<utility>

using namespace std;


//dpは行ったかどうか、vは行けるかどうか
void depth_search(int x,int y,vector< vector<int> >& dp,vector< vector<int> >& v,int w,int h){
  if(x>0){if(v[x-1][y]==1 and dp[x-1][y]!=1){dp[x-1][y]=1;depth_search(x-1,y,dp,v,w,h);}}
  if(x<h-1){if(v[x+1][y]==1 and dp[x+1][y]!=1){dp[x+1][y]=1;depth_search(x+1,y,dp,v,w,h);}}
  if(y>0){if(v[x][y-1]==1 and dp[x][y-1]!=1){dp[x][y-1]=1;depth_search(x,y-1,dp,v,w,h);}}
  if(y<w-1){if(v[x][y+1]==1 and dp[x][y+1]!=1){dp[x][y+1]=1;depth_search(x,y+1,dp,v,w,h);}}
}


void solve(int w,int h){
  vector< vector<int> > dp(h,vector<int>(w,0));
  vector< vector<int> > v(h,vector<int>(w,0));
  int x,y;
  for(int i=0;i<h;i++){
    string s;cin >>s;
    for(int j=0;j<w;j++){
      if(s[j]!='#') v[i][j]=1;
      if(s[j]=='@'){dp[i][j]=1;x=i;y=j;}
    }
  }
  depth_search(x,y,dp,v,w,h);
  int c=0;
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      if(dp[i][j]==1)c+=1;
    }
  }
  cout << c << endl;
}


int main(){
  int w,h;
  while(cin >> w >> h and w!=0){
    solve(w,h);
  }
}