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<cstdio>


int ans[1024];
int w,h,k;
bool b[22][22];//??°???

int d(int,int);

int main(void){
  int i,j;
  char c[20][20];
  int p,q;//???????????§?¨?

    k =0;
  while(true){
    //??\???
    scanf("%d%d",&w,&h);
    if(w==0 && h==0)break;
    
    for(i=0;i<h;i++){
	scanf("%s",c[i]);
//printf("%d,%s\n",i,c[i]);
      }
//printf("%c\n",c[0][1]);
    for(i=0;i<h;i++){
      for(j=0;j<w;j++){
        if(c[i][j] =='@'){
            p=i;
            q=j;
        }
          if(c[i][j] =='#'){
              b[i][j] = false;
            
          }else {
              b[i][j] = true;
          }
      }
    }
      ans[k] = 1;
      //printf("Ok");
    d(p,q);
      k++;
  }
    for (i=0;i<k;i++){
        printf("%d\n",ans[i]);
    }
  return 0;
}
int d(int p,int q){
  int i,j,p1,q1;
    b[p][q]  = false;
  for(i=0;i<4;i++){
    if(i==0){
      p1=p+1;
      q1=q;
    } else if(i==1){
      p1=p-1;
      q1=q;
    }else if(i==2){
      p1=p;
      q1=q+1;
    }else {
      p1=p;
      q1=q-1;
    }
    if(p1>=0 && p1<h && q1>=0 && q1<w){
      if(b[p1][q1] == true){
	ans[k]++;
          
        d(p1,q1);
      }
    }
  }
  return 0;
}

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

#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[30][30]; //??§?¨?????????????????????¨???

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);
	while(!q.empty()){
		p=q.front();
		q.pop();
		for(int i=0;i<4;++i){
			if(valid(p.first+dx[i],p.second+dy[i]))
				tot+=1;
				p.first+=dx[i]; p.second+=dy[i];
				q.push(p);
		}
	}
}

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

C++: #include <iostream>
#include <math.h>
using namespace std;

void rec(int x,int y);
char map[30][30];
bool isVisit[30][30];
int count=0;
//start_pos
int start_x;
int start_y;
int h,w;

int main()
{
    while (cin >> w >> h) 
    {
        count = 0;
        //start pos

        for (int i = 0; i < h; i++) 
        {
            for (int j = 0; j < w; j++) 
            {
                cin >> map[j][i];
                if (map[j][i] == '@') 
                {
                    start_x=j;
                    start_y=i;
                }
                //ついでにisVisit初期化
                isVisit[j][i]=false;
            }
        }
        rec(start_x,start_y);
        cout <<count<<endl;
    }

}

void rec(int x,int y)
{
    if ( y<0 || x<0 || h<=y || w<=x) {return;}
    if ( map[x][y] == '#') {return;}
    if (isVisit[x][y]) { return;}
    isVisit[x][y] = true;
    count++;

    rec(x, y-1);
    rec(x+1, y);
    rec(x, y+1);
    rec(x-1, y);

    return;
}

C++: #include <iostream>
#include <algorithm>
#define MH 20
#define MW 20
using namespace std;
char f[MH][MW];
bool v[MH][MW];
int dx[4] = { 1, 0, -1, 0 };
int dy[4] = { 0, 1, 0, -1 };
int w, h;
int c = 0;

void search(int x, int y) {
	++c;
	v[y][x] = true;
	for (int i = 0; i < 4; ++i) {
		int nx = x + dx[i]; int ny = y + dy[i];
		if (0 <= nx && nx < w && 0 <= ny && ny < h && v[ny][nx] == false && f[ny][nx] != '#') {
			search(nx, ny);
		}
	}
	return;
}

int main() {
	int sx, sy;

	while (cin >> w >> h, !(w == 0 && h == 0)) {
		for (int i = 0; i < h; ++i) {
			for (int j = 0; j < w; ++j) {
				cin >> f[i][j];
				if (f[i][j] == '@') {
					sx = j;
					sy = i;
				}
			}
		}
		c = 0;
		fill(v[0], v[MH], false);
		search(sx, sy);
		cout << c << endl;
		
	}
	return 0;
}

C++: #include<iostream>
using namespace std;
char maze[21][21];
int maz[21][21];
int w,h;
int f(int x,int y){
	if(maz[y][x]||maze[y][x]=='#')return 0;
	maz[y][x]=1;
	y<h-1&&f(x,y+1);
	0<y&&f(x,y-1);
	x<w-1&&f(x+1,y);
	0<x&&f(x-1,y);
	return 0;
}
int main(){
	int x,y;
	for(;cin>>w>>h,w;){
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				cin>>maze[i][j];
				maz[i][j]=0;
				if(maze[i][j]=='@'){y=i;x=j;}
			}
		}
		f(x,y);
		int res=0;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				res+=maz[i][j];
			}
		}
		cout<<res<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <cassert>
#define REP(i,l,n) for(int i=l;i<n;++i)
#define rep(i,n) REP(i,0,n)
#define MAX 20
int ans,baseX,baseY;
int w,h;
char tile[MAX][MAX];
void seek(int x,int y);
using namespace std;
int main() {
  while(1){
    cin >> w;
    cin >> h;
    if(w==0&&h==0) return 0;
    rep(i,h){
      rep(j,w){
        cin >> tile[i][j];
        if(tile[i][j] == '@'){
          baseX = i;
          baseY = j;
        }
      }
      tile[i][w] = '#';
    }
    rep(i,w) tile[h][i] = '#';
    ans = 1;
    seek(baseX,baseY);
    cout << ans << endl;
  }
  return 0;
}

void seek(int x,int y){
  if(tile[x+1][y] == '.'){
    tile[x+1][y] = '#';
    ans++;
    seek(x+1,y);
  }
  if(tile[x][y+1] == '.'){
    tile[x][y+1] = '#';
    ans++;
    seek(x,y+1);
  }
  if(tile[x-1][y] == '.'){
    tile[x-1][y] = '#';
    ans++;
    seek(x-1,y);
  }
  if(tile[x][y-1] == '.'){
    tile[x][y-1] = '#';
    ans++;
    seek(x,y-1);
  }
}