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<iostream>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<list>
#include<map>
#include<set>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
#define pb push_back
int dy[]={0, 0, 1, -1, 0};
int dx[]={1, -1, 0, 0, 0};
  
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
char room[100][100];
bool fin[100][100];
int w,h;

int dfs(int i,int j){
    int sum = 1;
 
    fin[i][j] = false;
 
    for(int k = 0;k < 4;k++){
        int ii = i + dy[k];int jj = j + dx[k];
        if(ii >= 0 && ii < h && jj >= 0 && jj < w && fin[ii][jj] && room[ii][jj] == '.')
            sum += dfs(ii,jj);
    }
    return sum;
}
 
int main(){
    while(1){
        scanf("%d%d",&w,&h);
 
        if(w == 0 && h == 0)
            break;
 
        for(int i = 0;i < h;i++)
            scanf("%s",&room[i][0]);
 
        for(int i = 0;i < h;i++)
            for(int j = 0;j < w;j++)
                fin[i][j] = true;
 
        for(int i = 0;i < h;i++){
            for(int j = 0;j < w;j++){
                if(room[i][j] == '@')
                    printf("%d\n",dfs(i,j));
            }
        }
    }
    return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;

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

void dfs(vector< vector<char> > &plane, int x, int y, int &ans){  
  plane[x][y] = '#';
  ans++;
  for(int i=0; i<4; i++){
    if(0 <= x+dx[i] && x+dx[i] < w && 0 <= y+dy[i] && y+dy[i] < h)
      if(plane[x + dx[i]][y + dy[i]] != '#')
        dfs(plane, x + dx[i], y + dy[i], ans);
  }
  
  return;
}

int main(){
  while(true){
    cin >> w >> h;
    if(w == 0)
      break;

    int sx, sy;
    vector< vector<char> > plane(w, vector<char>(h));
    for(int i=0; i<h; i++){
      for(int j=0; j<w; j++){
        cin >> plane[j][i];
        if(plane[j][i] == '@'){
          sx = j;
          sy = i;
        }
      }
    }

    int ans = 0;
    dfs(plane, sx, sy, ans);
    cout << ans << endl;
  }
  
  return 0;
}

C++: #include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<cmath>
#include<climits>
#include<ctime>
#include<cstring>
#include<numeric>

#define ALL(v) (v).begin(),(v).end()
#define REP(i,p,n) for(int i=p;i<(int)(n);++i)
#define rep(i,n) REP(i,0,n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list) cout << "{ "; for(auto nth : list){ cout << nth << " "; } cout << "}" << endl;

using namespace std;

int W, H;
int sx, sy;
int ans;
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};
vector<vector<char>>  field;

bool is_out_of_range(int x, int y) {
    return x < 0 || x >= W || y < 0 || y >= H;
}

void dfs(int x, int y) {
    if (is_out_of_range(x, y)) return;
    if (field[y][x] == '#') return;

    field[y][x] = '#';
    ans++;

    for (int i = 0; i < 4; ++i) {
        dfs(x + dx[i], y + dy[i]);
    }
    return;
}

int main() {
	while(cin >> W >> H, W) {
        ans = 0;
        field.assign(H, vector<char>(W));
        for (int y = 0; y < H; ++y) {
            for (int x = 0; x < W; ++x) {
                cin >> field[y][x];
                if (field[y][x] == '@') {
                    sx = x; sy = y;
                }
            }
        }
    
        dfs(sx, sy);
        cout << ans << endl; 
	}
	return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

typedef pair<int,int> P ;

int main(){

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

	
	while(1){
		int cnt=1;
		int w,h;
		int sx,sy;
		char c[21][21];

		cin >> w >> h ;
		if( h==0 && w==0) return 0;

		for(int y = 0; y < h; y++){
			scanf("%s", c[y]);
			for(int x = 0; x < w; x++){
				if( c[y][x] == '@' ){
					sx = x; 
					sy = y;
				 }
			}
		}

		queue<P> q;
		q.push(P(sx,sy));
		c[sy][sx] = '#';

		while(!q.empty()) {
			int x = q.front().first;
			int y = q.front().second;
			q.pop();
			
			for(int i=0;i<4;i++){
				int mx = x+dx[i] , my = y+dy[i];
				if( mx<0 || my<0 || w<=mx || h<=my || c[my][mx] == '#'||c[my][mx] =='@') continue;
				q.push(P(mx,my));	
				c[my][mx] = '#';	
				cnt++;			
			}
			
		}
		cout << cnt << endl;
	}
}

C++: #include <iostream>
using namespace std;
int W,H;
int x[4]={0,0,1,-1};
int y[4]={1,-1,0,0};
int i,j;
int count=0;
char room[100][100];

int dfs(int, int);

int main(){
  while(1){
    int sy,sx;
    scanf("%d %d",&W,&H);
    if(W==0&&H==0)break;
    for(i = 0 ; i < H ; i++){
      scanf("%c",&room[i][0]);
      for(j = 0 ; j < W ; j++){
	scanf("%c",&room[i][j]);
	if(room[i][j] == '@'){
	  sy = i;
	  sx = j; 
	}
      }
    }
    printf("%d\n", dfs(sx,sy));
  }
  return 0;
}

int dfs(int ax,int ay){
  if(ax<0||ay<0)return 0;
  if(ax>=W||ay>=H)return 0;
  if(room[ay][ax]=='#')return 0;
  room[ay][ax]='#';
  int cnt=1;
  int i;
  for(i=0;i<4;i++){
    int nx=ax+x[i];
    int ny=ay+y[i];
    cnt+=dfs(nx,ny);
  }
  return cnt;
  /*
    for(i = 0 ; i < 4 ; i++){
    int nx=ax+x[i]; int ny=ay+y[i];
    if(nx>0&&nx<W&&ny>0&&ny<H&&room[ax][ay]=='-'){
    dfs(nx,ny);
    count++;
    }else if(nx>0&&nx<W&&ny>0&&ny<H&&room[ax][ay]=='#'){
    dfs(ax,ay);
    }
    }
    printf("%d\n",count);
  */
}