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 <bits/stdc++.h>

using namespace std;

int W, H;
char room[20][20];

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

int sx, sy;

int dfs(int x, int y)
{
  room[y][x] = '#';
  int cnt = 1;
  for(int i = 0; i < 4; i++){
    int nx = x + dx[i], ny = y + dy[i];
    if(0 <= nx && nx < W && 0 <= ny && ny < H && room[ny][nx] != '#'){
      cnt += dfs(nx, ny);
    }
  }
  return cnt;
}

int main()
{
  while(cin >> W >> H, W || H){
    for(int i = 0; i < H; i++){
      for(int j = 0; j < W; j++){
	cin >> room[i][j];
	if(room[i][j] == '@') sx = j, sy = i;
      }
    }
    cout << dfs(sx, sy) << endl;
  }
  return 0;
}

C++: #include <iostream>

using namespace std;

int w, h;
char map_[20][20];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

int fill(int x, int y) {
    int count = 1;
    map_[x][y] = '#';
    for (int i=0; i<4; i++) {
        int x_new = x+dx[i];
        int y_new = y+dy[i];
        if (0 <= x_new && x_new < w && 0 <= y_new && y_new < h) {
            if (map_[x_new][y_new] == '.') {
                count += fill(x_new, y_new);
            }
        }
    }
    return count;
}

int main() {
    int x_start, y_start;
    while (true) {
        cin >> w >> h;
        if (w == 0) {
            break;
        }
        for (int i=0; i<h; i++) {
            for (int j=0; j<w; j++) {
                cin >> map_[j][i];
                if (map_[j][i] == '@') {
                    x_start = j;
                    y_start = i;
                }
            }
            cin.ignore(1);
        }
        cout << fill(x_start, y_start) << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<queue>
using namespace std;
int main() {
	int w, h;
	while (cin >> w >> h) {
		if (!w && !h)return 0;
		queue<pair<int, int> > q;
		char map[114][514];
		bool b[114][514] = {};
		for (int i = 1; i <= h; i++) {
			for (int j = 1; j <= w; j++) {
				cin >> map[i][j];
				if (map[i][j] == '@')q.push(make_pair(i, j));
			}
		}
		int ans = 0;
		while (q.size()) {
			ans++;
			int x = q.front().first;
			int y = q.front().second;
			q.pop();
			map[x][y] = '#';
			b[x][y] = true;
			if (map[x - 1][y] == '.' && !b[x - 1][y]) {
				q.push(make_pair(x - 1, y));
				b[x - 1][y] = true;
			}
			if (map[x + 1][y] == '.' && !b[x + 1][y]) {
				q.push(make_pair(x + 1, y));
				b[x + 1][y] = true;
			}
			if (map[x][y - 1] == '.' && !b[x][y - 1]) {
				q.push(make_pair(x, y - 1));
				b[x][y - 1] = true;
			}
			if (map[x][y + 1] == '.' && !b[x][y + 1]) {
				b[x][y + 1] = true;
				q.push(make_pair(x, y + 1));
			}
		}
		cout << ans << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
char maze[21][21];
#define r(z,x) for(int z=0;z<x;z++)
int ans,a,b;
void dfs(int x,int y){
	int dx[4]={0,-1,0,1},dy[4]={1,0,-1,0};
	maze[x][y]='#',ans++;
	r(i,4)if(maze[x+dx[i]][y+dy[i]]=='.')dfs(x+dx[i],y+dy[i]);
}
int main(){
	while(cin>>a>>b,a||b){
		ans=0;
		fill(maze[0],maze[20],'#');
		r(i,b)r(j,a)cin>>maze[i][j];
		r(i,b)r(j,a)if(maze[i][j]=='@')dfs(i,j),cout<<ans<<endl;
	}
}

C++: #if 0
08-162015?????????????????°???????????????????????????
pair???
#endif

#include <iostream>
#include <queue>
#include <string.h>
#include <utility>
using namespace std;

int w,h;
int tot=1; //?????£????????????(?????????????????????)
const int dx[]={1,0,-1,0}, dy[]={0,-1,0,1}; //??????????§??????¨
char P[100][100]; //??§?¨?????????????????????¨???

bool valid(int x,int y){ //??????????????????
	return (x>=0)&&(x<w)&&(y>=0)&&(y<h)&&(P[x][y]!='#');
}

void bfs(int x,int y){ //???????????¢?´¢
	queue<pair<int,int> > q;
	pair<int,int> p;
	p = make_pair(x,y); //pair??????????????¨??????????????????
	q.push(p);
	while(!q.empty()){
		pair<int,int> cur=q.front(); //?????¨??°?????????????????????
		q.pop();
		for(int i=0;i<4;++i){
			if(valid(cur.first+dx[i],cur.second+dy[i])){
				tot+=1;
				q.push(make_pair(cur.first+dx[i],cur.second+dy[i]));
				P[cur.first+dx[i]][cur.second+dy[i]]='#'; //???????????£?????´???????°???????
			}
		}
		P[cur.first][cur.second]='#';
	}
}

int main(){
	int x1,y1;
	while((cin>>w>>h)&&(w!=0)){
		for(int j=0;j<h;++j){
			for(int i=0;i<w;++i){
				cin>>P[i][j];
				if(P[i][j]=='@')
					x1=i; y1=j; 
			}
		}
		bfs(x1,y1); //??????????????°????????£??\????????¢?´¢????§?
	}
	cout << tot <<endl;
}

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

char **data;
int w,h;

int r(int,int);

int main(){
	int i,j;
	int num;
	int x,y;
	while(cin>>w>>h){
		if(w==0 && h==0) break;
		data=new char*[h];
		for(i=0;i<h;i++){
			data[i]=new char[w+1];
			cin>>data[i];
			for(j=0;j<w;j++){
				if(data[i][j]=='@'){
					data[i][j]='.';
					x=j;
					y=i;
				}
			}
		}
		num=r(x,y);
		cout<<num<<endl;
	}
	return 0;
}

int r(int x,int y){
	int count=0;
	if(x>=w || x<0) return 0;
	if(y>=h || y<0) return 0;

	if(data[y][x]=='.'){
		data[y][x]='@';
		count++;
	}
	else return 0;
	count+=r(x+1,y);
	count+=r(x-1,y);
	count+=r(x,y+1);
	count+=r(x,y-1);
	return count;
}