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 <vector>
#include <queue>

int main(){
  char c;
  int x, y, atx, aty, ind, count = 1;
  std::vector<int> real;
  std::queue<int> q;

  std::cin >> x;
  std::cin >> y;

  while(x != 0 || y != 0){

    real.resize(x*y);

    for(int i = 0; i < y; i++){         //地図の読み込みと＠の座標の格納
      for(int j = 0; j < x; j++){
        std::cin >> c;
        if(c == '.'){

          real[i*x + j] = 1;

        }else if(c == '#'){

          real[i*x + j] = 0;

        }else if(c == '@'){

          real[i*x + j] = -1;
          atx = j;//atx と aty は＠の座標を意味している
          aty = i;

        }else{

          std::cout << "unexpected char input!" << std::endl;
          return 0;

        }
      }
    }

    //＠を起点にして探索を開始する。

    q.push(atx + aty * x);

    while( !q.empty() ){
      
      ind = q.front();
      q.pop();


      if( ind%x != 0){//左をチェック 左端でなければ
        if(real[ ind-1 ] == 1 ){

          real[ ind-1 ] = -1;
          q.push( ind-1 );
          count++;
        }
      }

      if( ind%x != x-1 ){//右をチェック 右端でなければ
        if(real[ ind+1 ] == 1){

          real[ ind+1 ] = -1;
          q.push( ind+1 );
          count++;
        }
      }

      if( ind-x > 0 ){//上をチェック 上端でなければ
        if(real[ ind-x ] == 1){

          real[ ind-x ] = -1;
          q.push( ind-x );
          count++;
        }
      }

      if( ind+x < x * y){//下をチェック 下端でなければ
        if(real[ ind+x ] == 1){

          real[ ind+x ] = -1;
          q.push( ind+x );
          count++;
        }
      }

    }

    std::cout << count << std::endl;
    std::cout << std::endl;

///*
    for(int i = 0; i < y;i++){
      for(int j = 0; j < x; j++){
        std::cout << real[i*x + j];
      }
      std::cout << std::endl;
    }
//*/
    std::cin >> x;
    std::cin >> y;
  }

  return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<queue>
#include<utility>
int dx[4] = {0,1,0,-1};
int dy[4] = {1,0,-1,0};

using namespace std;

int main(){
  int w,h;
  vector<string> s;
  queue<pair<int,int>> que;
  vector<vector<int>> d;
  int defi,defj;
  while(cin>>w&&cin>>h){
    if(!w&&!h)break;
    s.resize(h);
    d.resize(h);
    for(int i=0;i<h;i++)
      d[i].resize(w,0);
    for(int i=0;i<h;i++){
      cin >> s[i];
      for(int j=0;j<w;j++)
	if(s[i][j] == '@'){defi=i;defj=j;}
    }
    
    que.push(make_pair(defi,defj));
    int ct=1;//???????????????
    while(que.size()){
      pair<int,int> defp = que.front();
      que.pop();
      pair<int,int> p;
      for(int i=0;i<4;i++){
	p.first = defp.first + dy[i];p.second = defp.second + dx[i];
	if(p.first>=0 && p.second>=0 && p.first<h && p.second<w){
	  if(d[p.first][p.second])continue;
	  else if(s[p.first][p.second] == '.'){
	    ct++;
	    d[p.first][p.second]++;
	    que.push(p);
	  }else if(s[p.first][p.second] == '#'){
	    d[p.first][p.second]++;
	  }
	}
      }
    }
    cout << ct << endl;
    s.clear();
    d.clear();
  }
  return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
int dy[]={0,0,-1,1},dx[]={-1,1,0,0};
class data{
public:
    int y,x;
    data(int _y,int _x){
        y=_y;x=_x;
    }
    data(){}
};
int main(){
    int w,h;
    while(cin>>w>>h,h||w){
        char field[20][21];
        bool f[20][20]={{0}};
        for(int i=0;i<h;i++)cin>>field[i];
        int sy,sx;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(field[i][j]=='@'){
                    sy=i;
                    sx=j;
                }
            }
        }
        queue<data>Q;
        Q.push(data(sy,sx));
        int cnt=0;
        while(Q.size()){
            data a=Q.front();Q.pop();
            int y=a.y,x=a.x;
            if(y<0||y>=h||x<0||x>=w)continue;
            if(f[y][x])continue;
            if(field[y][x]=='#')continue;
            f[y][x]=true;
            cnt++;
            for(int i=0;i<4;i++){
                Q.push(data(y+dy[i],x+dx[i]));
            }
        }
        cout<<cnt<<endl;
    }
}

C++: #include<iostream>
using namespace std;
int w,h;
char tail[21][21];
int a[]={-1,0,1,0};
int b[]={0,1,0,-1};
int cnt=0;
void func(int x,int y);
main(){
  int sx,sy;
  while(1){
    cin>> w>> h;
      if(w==0&&h==0) break;
    cnt=0;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	cin>> tail[i][j];
	if(tail[i][j]=='@'){
	  sx=j;
	  sy=i;
	}
      }
    }
    func(sx,sy);
    cout<< cnt<< endl;
  }
}

void func(int x,int y){
  tail[y][x]='#';
  cnt++;
  for(int i=0;i<4;i++){
    int nx=x+a[i];
    int ny=y+b[i];
    if(nx>=0 && nx<w && ny>=0 && ny<h && tail[ny][nx]!='#'){
      func(nx,ny);
    }
  }
}

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

int main() {
	int w,h;// =<20
	char map[20][20];
	int res[20][20]={};
	int result=0;
	int x,y;
	int start;// xxyy
	int stage;
	int check;
	int finish=0;
	
	scanf("%d%d",&w,&h);
	while(finish==0){
	for(y=0;y<h;y++){
		for(x=0;x<w;x++){
			cin >> map[x][y];
			if(map[x][y]=='@'){start=x*100+y;}
		}
	}
	
	x=start/100;
	y=start%100;
	res[x][y]=1;
	stage=1;
	check=1;
	while(check==1){
		check=0;
		for(y=0;y<h;y++){
			for(x=0;x<w;x++){
				if(res[x][y]==stage){
					if(x>0){
						if((map[x-1][y]=='.')&&(res[x-1][y]==0)){res[x-1][y]=stage+1;check=1;}
					}
					if(y>0){
						if((map[x][y-1]=='.')&&(res[x][y-1]==0)){res[x][y-1]=stage+1;check=1;}
					}
					if(x<w-1){
						if((map[x+1][y]=='.')&&(res[x+1][y]==0)){res[x+1][y]=stage+1;check=1;}
					}
					if(y<h-1){
						if((map[x][y+1]=='.')&&(res[x][y+1]==0)){res[x][y+1]=stage+1;check=1;}
					}
				}
			}
		}
		stage+=1;
	}

	result=0;
	for(y=0;y<h;y++){
		for(x=0;x<w;x++){
			if(res[x][y]!=0){result+=1;}
		}
	}
	cout << result << endl;

	for(y=0;y<h;y++){
		for(x=0;x<w;x++){
			map[x][y]=0;
			res[x][y]=0;
		}
	}

	scanf("%d%d",&w,&h);
	if((w==0)&&(h==0)){
		finish=1;
	}
	}
	return 0;
}