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>
#include <cassert>
#define REP(i,l,n) for(int i=l;i<n;++i)
#define rep(i,n) REP(i,0,n)
#define MAX 21
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);
  }
}

C++: /*http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1130&lang=jp*/

#include <iostream>

using namespace std;

struct point{
    int x;
    int y;
};

char map[20][20];

int move(const struct point p,int h,int w){
    int count=1;
    map[p.x][p.y]='/';
    for (int i = 0; i < 4; ++i) {
        struct point tmp=p;

        switch (i){
            case 0:
                tmp.x++;
                break;
            case 1:
                tmp.x--;
                break;
            case 2:
                tmp.y++;
                break;
            case 3:
                tmp.y--;
                break;
            default:
                break;
        }
        if (tmp.x<w && tmp.y<h && map[tmp.x][tmp.y]=='.') {
            count+=move(tmp,h,w);
        }
    }
    return count;
}

int main(){
    int w,h;

    while (cin>>w>>h && (w>0 || h>0)){
        struct point start;

        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;
                }
            }
        }
        cout<<move(start,h,w)<<endl;
    }
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
const int INF = 1e9;

//pair<int,int>;

int W,H;
vector<string> s(20);
int nextX[] = {1,0,-1,0};
int nextY[] = {0,1,0,-1};
vector<vector<int>> count_map(20,vector<int>(20,0));

bool range_check(int y,int x){
  if(y<0||x<0||y>=H||x>=W){
    return false;
  }
  return true;
}

void bfs(int y,int x){
  if(!range_check(y,x)){
    return;
  }
  //printf("%d %d",y,x);
  if(s[y][x]=='#'){
    //printf("aaa\n");
    return;
  }
  if(count_map[y][x]){
    return;
  }
  count_map[y][x]=1;
  queue<pair<int,int>> node;
  for(int i=0;i<4;i++){
    int ny = y+nextY[i],nx = x + nextX[i];
    if(!range_check(ny,nx)){
      continue;
    }
    if(s[ny][nx]=='.'){
      node.emplace(ny,nx);
    }
  }
  while(!node.empty()){
    pair<int,int> yx = node.front();
    node.pop();
    bfs(yx.first,yx.second);
  }
}

int main(){
  while(1){
    cin>>W>>H;
    if(W==0&&H==0){
      break;
    }
    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]=='@'){
          bfs(i,j);
        }
      }
    }
    int ans=0;
    for(int i=0;i<H;i++){
      for(int j=0;j<W;j++){
        //printf("%3d",count_map[i][j]);
        if(count_map[i][j]){
          ans++;
          count_map[i][j]=0;
        }
      }
      //cout<<endl;
    }
    cout<<ans<<endl;
  }
  return 0;
}

C++: #include<iostream>
#include<string>
#include<queue>
#include<vector>
using namespace std;
#define frp(i,n) for((i)=0;(i)<(n);i++)
int vx[4]={0,1,0,-1};
int vy[4]={1,0,-1,0};
int H,W;
int check(int n1,int n2,int rednum,vector<int> red1,vector<int> red2){
  for(int i=0;i<rednum;i++){
    if(n1==red1[i]&&n2==red2[i]||n1<0||n2<0||n1>=H||n2>=W){
      return 0;
    }
  }
  return 1;
}
int main(){
  int i,j,now1,now2;
  while(cin>>W>>H,W+H!=0){
    int num=1;
    string str2;
    vector<string> str;
    vector<int> red1;
    vector<int> red2;
    frp(i,H){
      cin>>str2;
      str.push_back(str2);
    }
    frp(i,H){
      frp(j,W){
	if(str[i][j]=='#'){
	  red1.push_back(i);
	  red2.push_back(j);
	}
      }
    }
    queue<pair<int,int> > qii;
    frp(i,H){
      frp(j,W){
	if(str[i][j]=='@'){
	  qii.push(make_pair(i,j));
	  red1.push_back(i);
	  red2.push_back(j);
	}
      }
    }
    while(!qii.empty()){
      now1=qii.front().first;
      now2=qii.front().second;
      qii.pop();//?????????????´??????´?£?
      for(i=0;i<4;i++){
	if(check(now1+vx[i],now2+vy[i],red1.size(),red1,red2)==1){
	  cout<< now1<<" "<<now2<<endl;
	  red1.push_back(now1+vx[i]);
	  red2.push_back(now2+vy[i]);
	  qii.push(make_pair(now1+vx[i],now2+vy[i]));
	  num++;
	}
      }
    }
    cout<<num<<endl;
  }
  return 0;
}

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

//探索関数
//引数の座標が移動可能なら1を足し、上下左右を再探索。
//同じ地点をカウントしないよう、一度カウントした座標はtに置き換える。
int tansaku(char tile[20][20],int w1,int h1){
    if(tile[w1][h1]!='.'&&tile[w1][h1]!='@')return 0;
    tile[w1][h1]='t';
    return 1+tansaku(tile,w1+1,h1)+tansaku(tile,w1-1,h1)+tansaku(tile,w1,h1+1)+tansaku(tile,w1,h1-1);
}

int main(){
    int w,h;
    int w1,h1; //スタート地点 @ の座標
   while(1){
    std::cin >> w;
    std::cin >> h;
    if(w==0&&h==0)break;
    //配列の初期化
    char tile[20][20]={};
    // 配列を取得.
    for(int i=0; i<h; i++){
        for(int j=0; j<w; j++){
            std::cin >> tile[j][i];
            if(tile[j][i]=='@'){
                w1=j; h1=i;
            }
        }
    }
    std::cout << tansaku(tile,w1,h1) << std::endl;
   }
    return 0;
}