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<vector>
#include<queue>

using namespace std;
vector<int> ans;
typedef pair <int, int> P;

int  main(){
  int W,H;
  cin >> W >> H;
  int tate[4] = {0,1,0,-1};
  int yoko[4] = {1,0,-1,0};
  while(W){
    int count = 0;
    queue < P > zahyou;
    vector< vector<int> > room(H, vector<int>(W));
    for(int i = 0 ; i < H ; i++){
      for(int j = 0 ; j < W ; j++){
        char a;
        cin >> a;
        if(a == '.')room[i][j] = 1;
        else if(a == '@'){
          zahyou.push( P (i,j) );
          room[i][j] = 1;
        }
        else room[i][j] = 0;
      }
    }
    while(zahyou.size()){
      pair<int, int> now = zahyou.front();
      zahyou.pop();
      if(room[now.first][now.second] == 1){
        count++;
        room[now.first][now.second] = 0;
        for(int i = 0; i < 4 ; i++){
          if(now.first+tate[i]>=0 && now.first+tate[i] < H && now.second+yoko[i] >= 0 && now.second+yoko[i] < W && room[now.first+tate[i]][now.second+yoko[i]] == 1)
            zahyou.push( P (now.first+tate[i], now.second+yoko[i]));
        }
      }
    }
    ans.push_back(count);
    cin >> W >> H;
  }
  for(int i = 0 ; i < ans.size(); i++)
    cout << ans[i] << endl;
}

C++: #include <iostream>
#include <queue>
#include <array>

const int kSize = 20 + 2;

std::array<int, 4> ways = {-1, -kSize, +1, +kSize};

inline int xy2pos(const int x, const int y){
    return x + y * kSize;
}

std::array<bool, kSize*kSize> Bfs(const std::array<bool, kSize*kSize> &tiles, int pos){
    std::array<bool, kSize*kSize> arrived;
    std::queue<int> q;
    q.push(pos);
    std::fill(arrived.begin(), arrived.end(), false);
    while(!q.empty()){
        pos = q.front();
        q.pop();
        arrived[pos] = true;
        for(int way : ways)
            if(tiles.at(pos + way) && !arrived.at(pos + way))
                q.push(pos + way);
    }
    return arrived;
}

int main(){
    int w,h;
    std::array<bool,kSize*kSize> tiles; ///< true if black
    while((std::cin >> w >> h) && w != 0){
        std::fill(tiles.begin(), tiles.end(), false);
        int pos;
        for(int y=1; y<=h; ++y){
            for(int x=1; x<=w; ++x){
                char c;
                std::cin >> c;
                tiles[xy2pos(x,y)] = (c == '.');
                if(c == '@'){
                    pos = xy2pos(x,y);
                    tiles[pos] = true;
                }
            }
        }
        auto arrived = Bfs(tiles, pos);
        int sum = 0;
        for(bool v : arrived)
            sum += (v) ? 1 : 0;
        std::cout << sum << std::endl;
    }
}

C++: #include<iostream>
using namespace std;
int main(){
  while(1){
    int dfs(int x,int y,int count);
    
    int w,h;
    int dx[4]={0,-1,0,1};
    int dy[4]={-1,0,1,0};
    int sum=0;
    char maps[21][21];
    
    int main(){
      int sx,sy;
      cin >> h >> w;
      if(h==0 && w==0) 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;
	  }
	}
      }
      int ans = dfs(sx,sy,0);
      cout << ans << endl;
    }
    
    int dfs(int x,int y,int count){
      int nx,ny;
      for(int i=0;i<4;i++){
	nx=x+dx[i];
	ny=y+dy[i];
	maps[y][x]='#';
	if(0<=nx && nx<w && 0<=ny && ny<h && maps[ny][nx]!='#'){
	  sum++;
	  dfs(nx,ny,count+1);
	}
      }
      return sum;
    }
  }
  return(0);
}

C++: #include<iostream>
#include<iomanip>
#include<cstdlib>
#include<stdio.h>
#include<time.h>
#include<math.h>
#include<algorithm>
#include<string>
#include<vector>
#include<list>
#include<queue>
#include<stack>
#include<deque>
#include<set>
#include<map>
#define REP(i, N) for(ll i = 0; i < N; ++i)
#define FOR(i, a, b) for(ll i = a; i < b; ++i)
#define ALL(a) (a).begin(),(a).end()
#define pb push_back
#define INF 1000000000
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int qx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int qy[8] = {0, 1, 1, 1, 0, -1, -1, -1};

vector<vector<bool>> d;
vector<string> m;
int w, h;

void cal(ll x, ll y) {
	REP(i, 4) {
		ll nx = x + dx[i];
		ll ny = y + dy[i];
		if(nx >= 0 && nx < h && ny >= 0 && ny < w && m[nx][ny] == '.' && !d[nx][ny]) {
			d[nx][ny] = true;
			cal(nx, ny);
		}
	}
}

int main(void) {
	while(true) {
		cin>>w>>h;
		if(w == 0 && h == 0) break;
		m.resize(h);
		d.resize(h);
		REP(i, h) {
			d[i].resize(w);
			REP(j, w) {
				d[i][j] = false;
			}
		}
		REP(i, h) cin>>m[i];
		REP(i, h) {
			REP(j, w) {
				if(m[i][j] == '@') {
					d[i][j] = true;
					cal(i, j);
					break;
				}
			}
		}
		ll ans = 0;
		REP(i, h) {
			REP(j, w) {
				if(d[i][j]) ++ ans;
			}
		}
		cout<<ans<<endl;
	}
}