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 <stdio.h>
#include <stack>
#include <math.h>
#include <queue>

using namespace std;

int W,H;
bool visited[20][20];
char table[20][21];

struct Tile{
	int row,col;
};

bool rangeCheck(int row,int col){
	if(row >= 0 && row <= H-1 && col >=0 && col <= W-1)return true;
	else{
		return false;
	}
}

void func(int row,int col){
	queue<Tile> Q;
	Tile tile;
	tile.row = row;
	tile.col = col;

	Q.push(tile);

	Tile tmp;
	while(!Q.empty()){
		tmp = Q.front();
		Q.pop();

		if(rangeCheck(tmp.row-1,tmp.col) == true && visited[tmp.row-1][tmp.col] == false && table[tmp.row-1][tmp.col] == '.'){
			visited[tmp.row-1][tmp.col] = true;

			Tile newTmp;
			newTmp.row = tmp.row-1;
			newTmp.col = tmp.col;
			Q.push(newTmp);
		}
		if(rangeCheck(tmp.row+1,tmp.col) == true && visited[tmp.row+1][tmp.col] == false && table[tmp.row+1][tmp.col] == '.'){
			visited[tmp.row+1][tmp.col] = true;
			Tile newTmp2;
			newTmp2.row = tmp.row+1;
			newTmp2.col = tmp.col;
			Q.push(newTmp2);
		}
		if(rangeCheck(tmp.row,tmp.col-1) == true && visited[tmp.row][tmp.col-1] == false && table[tmp.row][tmp.col-1] == '.'){
			visited[tmp.row][tmp.col-1] = true;
			Tile newTmp3;
			newTmp3.row = tmp.row;
			newTmp3.col = tmp.col-1;
			Q.push(newTmp3);
		}
		if(rangeCheck(tmp.row,tmp.col+1) == true && visited[tmp.row][tmp.col+1] == false && table[tmp.row][tmp.col+1] == '.'){
			visited[tmp.row][tmp.col+1] = true;
			Tile newTmp4;
			newTmp4.row = tmp.row;
			newTmp4.col = tmp.col+1;
			Q.push(newTmp4);
		}
	}
}

int main(){

	int x,y,count;

	while(true){
		scanf("%d %d",&W,&H);
		if(W == 0 && H == 0)break;

		for(int i = 0; i < H; i++){
			for(int k = 0; k < W; k++)visited[i][k] = false;
		}

		for(int i = 0; i < H; i++){
			scanf("%s",table[i]);

			for(int k = 0; k < W; k++){
				if(table[i][k] == '@'){
					y = i;
					x = k;
				}
			}
		}

		visited[y][x] = true;
		func(y,x);

		count = 0;

		for(int i = 0; i < H; i++){
			for(int k = 0; k < W; k++){
				if(visited[i][k] == true)count++;
			}
		}

		printf("%d\n",count);

	}


    return 0;
}

C++: #include <bits/stdc++.h>
using namespace	std;
static int dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
int h,w,r;
string s[20];
void dfs(int y,int x){
  r++;
  s[y][x]='#';
  for(int i=0;i<4;i++)
      if(0<=y+dy[i]&&y+dy[i]<h&&0<=x+dx[i]&&x+dx[i]<w)
	if(s[y+dy[i]][x+dx[i]]=='.') dfs(y+dy[i],x+dx[i]);
}
int main(){

  int y,x;
  for(;cin>>w>>h,w;){
  r=0;
  for(int i=0;i<h;i++) cin>>s[i];
  for(int i=0;i<h;i++) for(int j=0;j<w;j++) if(s[i][j]=='@') y=i,x=j;
  dfs(y,x);
  cout<<r<<endl;
  }
  return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <functional>
using namespace std;
#define MOD 1000000007
#define ADD(X,Y) ((X) = ((X) + (Y)%MOD) % MOD)
typedef long long i64; typedef vector<int> ivec; typedef vector<string> svec; typedef pair<int, int> pi;
int dd[] = { 0, 1, 0, -1 };


int main()
{
	while (true)
	{
		int W, H;
		cin >> W >> H;
		if (W == 0 && H == 0) break;

		char C[30][30];
		int B[30][30];

		int sx = -1; int sy = -1;
		for (int i = 0; i < H; i++)
		{
			for (int j = 0; j < W; j++)
			{
				cin >> C[i][j];
				B[i][j] = 0;
				if (C[i][j] == '@')
				{
					sy = i;
					sx = j;
				}
			}
		}

		queue<pi> Q;
		Q.push(pi(sy, sx));
		B[sy][sx] = 1;
		int ans = 1;
		while (!Q.empty())
		{
			int ty = Q.front().first; int tx = Q.front().second;
			Q.pop();

			for (int r = 0; r < 4; r++)
			{
				int y = ty + dd[r];
				int x = tx + dd[r + 1];
				if (y < 0 || y >= H || x < 0 || x >= W)continue;
				if (C[y][x] == '#')continue;
				if (B[y][x])continue;
				ans++;
				B[y][x] = 1;
				Q.push(pi(y, x));
			}
		}

		cout << ans << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>

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

using namespace std;

char field[20][20];
int w, h;
int cnt = 0;

int dfs(int x, int y){
    field[x][y] = '#';
    cnt++;

    for(int dx=-1; dx<=1; dx++){
        for(int dy=-1; dy<=1; dy++){
            if(dx!=0 && dy!=0) continue;
            int nx = x + dx;
            int ny = y + dy;
            if(0 <= nx && nx < w && 0 <= ny && ny < h && field[nx][ny] == '.') dfs(nx,ny);
        }
    }


}

int main(){
    while(cin >> w >> h){
        if(w==0 && h==0) break;
        char line[w];
        int start_x, start_y;
        REP(j,h){
            cin >> line;
            REP(i,w){
                if(line[i] == '@'){
                    start_x = i;
                    start_y = j;
                    field[i][j] = '.';
                }
                field[i][j] = line[i];
            }
        }
        cnt = 0;
        dfs(start_x, start_y);
        cout << cnt << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <stdio.h>
#include <string.h>
using namespace std;

int x[100][100]={0};
int count;

void foo(int s,int t){
	if(x[s][t]==0){
	}
	else{
		count++;
		x[s][t]=0;
		if(t-1>=0) foo(s,t-1);
		foo(s,t+1);
		if(s-1>=0) foo(s-1,t);
		foo(s+1,t);
	}
}
	
int main(){

	int w,h;
	int i,j;
	char tmp;
	int pos1,pos2;
	
	cin>>w>>h;
	while(!(w==0&&h==0)){
		memset(x,0,sizeof(x));
		count=0;
	for(i=0;i<h;i++){
		for(j=0;j<w;j++){
			cin>>tmp;
			//printf("%c\n",tmp);
			if(tmp=='.'){
				x[i][j]=1;
			}
			else if(tmp=='#'){
				x[i][j]=0;
			}
			else if(tmp=='@'){
				pos1=i;
				pos2=j;
				x[i][j]=1;
				//cout<<"pos 1 2  "<<pos1<<" "<<pos2<<endl;
			}
		}
		//cout<<endl;
	}
		//printf("hoge");
		
		/*for(i=0;i<h;i++){
			for(j=0;j<w;j++){
				cout<<x[i][j]<<" ";
			}
			cout<<endl;
		}
		cout<<endl;
		*/
		foo(pos1,pos2);
		//cout<<"hoge";
		cout<<count<<endl;
		cin>>w>>h;
		
	}
	
	
	return 0;
}