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++: /*
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1130&lang=jp
*/

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const ll MOD = 1000000007;

const int di[4] = {+1, 0, -1, 0};
const int dj[4] = {0, -1, 0, +1};

int w, h;
string tiles[20];

int solve(int si, int sj){
    int res = 0;
    queue<pair<int, int> > Q;
    bool visited[h][w];
    memset(visited, false, sizeof visited);
    visited[si][sj] = true;
    res++;
    Q.push(make_pair(si, sj));
    while(!Q.empty()){
        pair<int, int> p = Q.front();
        Q.pop();
        int pi = p.first, pj = p.second;
        for(int k=0;k<4;k++){
            int ni = pi + di[k];
            int nj = pj + dj[k];
            if(ni < 0 || ni >= h || nj < 0 || nj >= w) continue;
            if(!visited[ni][nj] && tiles[ni][nj] == '.'){
                visited[ni][nj] = true;
                Q.push(make_pair(ni, nj));
                res++;
            }
        }
    }
    return res;
}

int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(false);
    while(1){
        cin >> w >> h;
        if(w == 0 && h == 0) break;
        int si, sj;
        for(int i=0;i<h;i++){
            cin >> tiles[i];
            for(int j=0;j<w;j++){
                if(tiles[i][j] == '@'){
                   si = i;  
                   sj = j;
                }
            }
        }
        int ans = solve(si, sj);
        cout << ans << endl;
    }
}

C++: #include <bits/stdc++.h>

#define FOR(i, a, b) for(long long int i=(a);i<=(b);i++)
#define RFOR(i, a, b) for(long long int i=(a);i>=(b);i--)
#define MOD 1000000007
#define INF 1000000000 //2000000000
#define LLINF 1000000000000000000 //9000000000000000000
#define PI 3.14159265358979

using namespace std;
typedef long long int ll;
typedef pair< long long int, long long  int> P;

vector<string> c(20);

int dfs(int x, int y,int h,int w) {
	int point = 1;
	if (c[y][x] == '#') {
		return 0;
	}
	c[y][x] = '#';
	if (y + 1 <= h - 1) {
		point+=dfs(x, y + 1, h, w);
	}
	if (y - 1 >= 0) {
		point+=dfs(x, y - 1, h, w);
	}
	if (x + 1 <= w - 1) {
		point+=dfs(x + 1, y, h, w);
	}
	if (x - 1 >= 0) {
		point+=dfs(x - 1, y, h, w);
	}
	return point;
}

int main(void) {
	
	while (1) {
		int h, w;
		int posx = -1, posy = -1;
		int count = 0;

		cin >> w >> h;
		if (h == 0) {
			break;
		}
		

		FOR(i, 0, h - 1) {
			cin >> c[i];
		}
		FOR(i, 0, h - 1) {
			FOR(t, 0, w - 1) {
				if (c[i][t] == '@') {
					posx = t;
					posy = i;
					break;
				}
			}
			if (posx != -1) {
				break;
			}
		}
		
		cout << dfs(posx, posy, h, w) << endl;
	}

}

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

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

int ans;
vector<string> f;

void solve(int x, int y)
{
  if (x < 0 || x >= f[0].size())
    return;
  if (y < 0 || y >= f.size())
    return;
  if (f[y][x] == '#')
    return;
  
  ++ans;
  f[y][x] = '#';

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

int main()
{
  int W, H, x, y;
  for ( ; ; ) {
    cin >> W >> H;
    if (W == 0 && H == 0)
      break;

    f.clear();
    for (int i = 0; i < H; ++i) {
      string tmp;
      cin >> tmp;
      int a = tmp.find_first_of("@", 0);
      if (a != string::npos) {
	x = a;
	y = i;
      }
      f.push_back(tmp);
    }
    ans = 0;
    solve(x, y);
    cout << ans << endl;
  }
  return 0;
}

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

int s = 0;
int x[] = { -1,0,0,1 }; int y[] = { 0,-1,1,0 };
bool p[20][20]{};
void shima(int a, int b) {
	s++;
	p[a][b] = false;
	for (int c = 0; c < 4; c++) {
		int d = a + x[c]; int e = b + y[c];
		if (d >= 0 && d < 20 && e>=0 && e < 20 && p[d][e])shima(d, e);
	}
}
int main() {
	int a, b;
	while (cin >>b>>a, a | b) {
		s = 0;
		for (int n = 0; n < 20; n++) {
			for (int m = 0; m < 20; m++) {
				p[n][m] = false;
			}
		}
		int l = 0, r = 0;
		for (int c = 0; c < a; c++) {
			for (int d = 0; d<b; d++) {
				char e;
				cin >> e;
				if (e == '.')p[c][d] = true;
				else if (e == '@') { l = c; r = d; }
			}
		}
		shima(l, r);
		cout << s << endl;
	}
}

C++: #include<stdio.h>
#include<iostream>
#include<utility>
#include<string.h>
#include<queue>
using namespace std;
typedef long long ll;
#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;
#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define FORR(i,a,b) for(int i=(int)(b)-1;i>=(int)(a);i--)
#define CHMIN(a,b) (a) = min((a),(b))
#define CHMAX(a,b) (a) = max((a),(b))

int main()
{
    std::queue<pair<int,int> > que;
    int w,h;
    char ban[25][25]={};
    int dx[4]={1,0,-1,0};
    int dy[4]={0,-1,0,1};
    int ans;
    while(1==1)
    {
        ans=0;
        scanf("%d %d\n",&w,&h);
        if(w==0&&h==0)return 0;
        REP(i,25)REP(j,25)ban[i][j]='\0';
        REP(i,h)
        {
            REP(j,w)
            {
                scanf("%c",&ban[i][j]);
                if(ban[i][j]=='@')que.push(pair<int,int>(i,j));
            }
            scanf("\n");
        }
        while(!que.empty())
        {
            pair<int,int> p=que.front();
            que.pop();
            if(ban[p.first][p.second]=='.'||ban[p.first][p.second]=='@')
            {
                ban[p.first][p.second]='#';
                ans++;
                REP(i,4)
                {
                    if(ban[p.first+dy[i]][p.second+dx[i]]=='.')
                    {
                        que.push(pair<int,int>(p.first+dy[i],p.second+dx[i]));
                    }
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}