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>
using namespace std;

void search(int a, int b);

char wh[22][22];
bool judge[22][22];

int ans = 0;

int main(void){

while(1) {
	int x;
	int y;
	cin >> x >> y;
	if(x == 0 && y == 0) return 0;

	int i;
	int j;

	// 初期化
	for(i = 0; i < 22; i++){
		for(j = 0; j < 22; j++){
			wh[j][i] = '#';
			judge[j][i] = false;
		}
	}

// 入力
	int xs, ys;
	for(i = 1; i < y+1; i++){
		for(j = 1; j < x+1; j++){
			cin >> wh[j][i];
			if(wh[j][i] == '@') {
				xs = j;
			 	ys = i;
			}
		}
	}


	// for(i = 0; i < 22; i++){
	// 	for(j = 0; j < 22; j++){
	// 		printf("%c",wh[j][i]);
	// 	}
	// 	printf("\n");
	// }

	search(xs, ys);

	cout << ans << endl;
	ans = 0;
}

	return 0;
}

void search(int a,int b) {
	if(wh[a][b] == '#' || judge[a][b] == true) return;

	ans++;
	judge[a][b] = true;
	// 左
	search(a-1,b);
	// 右
	search(a+1,b);
	// 上
	search(a,b+1);
	// 下
	search(a,b-1);
}

C++: #include <iostream>
using namespace std;
int a=0;
int box[20][20];
int  kazoe(int p,int P,int H,int W){
  int i,I;
  a++;
  for(i=0;i<H;i++){
    for(I=0;I<W;I++){
      cout<<box[i][I];
    }
    cout<<endl;
  }
  box[p][P]=0;
  if(box[p][P+1]==1) kazoe(p,P+1,H,W);
  if(box[p][P-1]==1) kazoe(p,P-1,H,W);
  if(box[p-1][P]==1) kazoe(p-1,P,H,W);
  if(box[p+1][P]==1) kazoe(p+1,P,H,W);
  else {
  return a;
  }
}
int main(){
  int i,I,H,W,p,P;
  
  char v;
  while(1){
  cin>>W>>H;
  if(W==0&&H==0) break;
  for(i=0;i<H;i++){
    for(I=0;I<W;I++){
      cin>>v;
      if(v=='.') box[i][I]=1;
      if(v=='#') box[i][I]=2;
      if(v=='@') {
	box[i][I]=0;
	p=i;
	P=I;
      }
    }
  }
  cout<<kazoe(p,P,H,W)<<endl;
  a=0;
  for(i=0;i<H;i++){
    for(I=0;I<W;I++){
      box[i][I]=0;
    }
  }
  
 
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int ans;
bool dfs(int y,int x,vector<vector<bool> >& canGo){
canGo[x][y]=false;
ans+=1;
if(canGo[y+1][x])dfs(y+1,x,canGo);
if(canGo[y-1][x])dfs(y-1,x,canGo);
if(canGo[y][x+1])dfs(y,x+1,canGo);
if(canGo[y][x-1])dfs(y,x-1,canGo);
return false;
}

int main(void){
int h,w;
int i,j,k;
int x,y;
string s;
vector<vector<bool> >canGo;

while(true){
cin>>h>>w;
if(h==0)return 0;
canGo=vector<vector<bool> >(h+2,vector<bool>(w+2,false));
ans=0;
for(i=0;i<h;i++){
	cin>>s;
	for(j=0;j<w;j++){
		if(s[j]=='.')canGo[i+1][j+1]=true;
		else if(s[j]=='@'){y=i+1;x=j+1;canGo[i+1][j+1]=true;}
	}
}
dfs(y,x,canGo);
cout<<ans<<endl;
}
return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <map>
#include <set>
#include <cstdio>
#include <cmath>
#define rep(i,l,n) for(lint i=l;i<n;i++)
#define rer(i,l,n) for(lint i=l;i<=n;i++)
#define all(a) a.begin(),a.end()
#define o(a) cout<<a<<endl
#define pb(a) push_back(a)
#define mk(a,b) make_pair(a,b)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef vector<int> vi;
typedef vector<lint> vli;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

int h, w, ans, sy, sx;
char s[21][21] = {};
int d[21][21] = {};
int dy[4] = { 1, -1, 0, 0 };
int dx[4] = { 0, 0, 1, -1 };

void dfs(int y, int x){
	if (y < 0 || h <= y || x < 0 || w <= x || d[y][x]||s[y][x]=='#') return;
	ans++;
	d[y][x] = 1;
	rep(i, 0, 4){
		int ny = y + dy[i], nx = x + dx[i];
		dfs(ny, nx);
	}
	return;
}

int main(){
	while (1){
		cin >> w >> h;
		rep(i, 0, 21){
			rep(j, 0, 21){
				d[i][j] = 0;
			}
		}
		if (h == 0 && w == 0) break;
		rep(i, 0, h){
			rep(j, 0, w){
				cin >> s[i][j];
				if (s[i][j] == '@'){
					sy = i;
					sx = j;
				}
			}
		}
		ans = 0;
		dfs(sy, sx);
		o(ans);
	}

}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <cstring>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)

using namespace std;

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

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

        vector<string> s(h);
        rep(i,h) cin >> s[i];

        int sy,sx;
        rep(i,h) {
            rep(j,w) {
                if(s[i][j] == '@') {
                    sy = i;
                    sx = j;
                }
            }
        }
        
        int ans = 1;
        bool memo[25][25];
        memset(memo,0,sizeof(memo));

        queue<pair<int,int> > que;
        que.push(make_pair(sy,sx));
        memo[sy][sx] = true;
        
        while(que.size()) {
            pair<int,int> p = que.front();
            que.pop();
            
            rep(i,4) {
                int ny = p.first + dy[i];
                int nx = p.second + dx[i];

                if(0 <= ny && ny < h && 0 <= nx && nx < w) {
                    if(s[ny][nx] == '.' && !memo[ny][nx]) {
                        ans++;
                        memo[ny][nx] = true;
                        que.push(make_pair(ny,nx));
                    }
                }
            }
        }

        cout << ans << endl;
    }

    return 0;
}