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>

bool done[21][21];
int W,H;
char s[21][21];
bool map[21][21];

void init(){
  for(int i=0;i<21;i++){
    for(int j=0;j<21;j++){
      done[i][j]=false;
      map[i][j]=false;
      s[i][j]=0;
    }}
}

void solve(int h,int w){
  if(s[h][w]=='.' || s[h][w]=='@'){
    map[h][w]=true;
    done[h][w]=true;
    if(h-1 >=0 && done[h-1][w]==false) solve(h-1,w);
    if(h+1 < H && done[h+1][w]==false) solve(h+1,w);
    if(w-1 >=0 && done[h][w-1]==false) solve(h,w-1);
    if(w+1 <W && done[h][w+1]==false) solve(h,w+1);
  }
  else{
    map[h][w]=false;
    done[h][w]=true;
  }

  return;

}
int main(){
  while(1){
    init();
    scanf("%d%d",&W,&H);
    if(W==0 && H==0) break;
    for(int i=0;i<H;i++)
      scanf("%s",&s[i]);
    int sw,sh;
    for(int i=0;i<H;i++){
      for(int j=0;j<W;j++){
        if(s[i][j]=='@'){
          sw=j;
          sh=i;
        }}}

    solve(sh,sw);


    int ans=0;

    for(int i=0;i<H;i++){
      for(int j=0;j<W;j++){
        if(map[i][j]==true)
          ans++;
      }}

    printf("%d\n",ans);
  }
  return 0;

}

C++: #include <iostream>

#define N 22

using namespace std;


int dx[4] = {0,1,0,-1};
int dy[4] = {-1,0,1,0};
/* ue migi shita hidari */

void dfs(int f[N][N], int x, int y, int *count);

int main(void)
{

	int w,h;

	int f[N][N];

	char c;
	int x,y;


	int count;

	int i1,i2;

	while(1) {

		/* syoki */
		for (i1=0;i1<N;i1++) {
			for (i2=0;i2<N;i2++) {
				f[i1][i2] = 0;
			}
		}

		/* nyuryoku */
		cin >> w;
		cin >> h;
		if (w == 0 && h == 0) break;

		for (i1=1;i1<=h;i1++) {
			for (i2=1;i2<=w;i2++) {

				cin >> c;

				if (c == '.') {
					f[i1][i2] = 1;
				}
				if ( c == '@') {
					f[i1][i2] = 1;
					x = i2;
					y = i1;
				}
			}

		}

		/* print */
		/*
		for (i1=0;i1<N;i1++) {
			for (i2=0;i2<N;i2++) {
				cout << f[i1][i2] << " ";
			}
			cout << endl;
		}

		cout << x << " " << y << endl;
		*/

		count = 1;
		dfs(f,x,y,&count);

		cout << count << endl;
	}

	return 0;
}


void dfs(int f[N][N], int x, int y, int *count)
{
	int i1;

	int xx,yy;

	f[y][x] = 0;

	for (i1=0;i1<4;i1++) {
		xx = x + dx[i1];
		yy = y + dy[i1];

		if ( f[yy][xx] == 1) {

			(*count)++;
			dfs(f,xx,yy,count);

		}
	}


}

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;
    count = 1;
  }

  return 0;
}

C++: #include<iostream>
#include<string>
using namespace std;
int w,h;
string map[40];
int vec[4][2]={{0,1},{0,-1},{-1,0},{1,0}};
void move(int x,int y){
	int ans=0;
	map[x][y]='#';
	for(int i=0;i<4;i++){
		if((x+vec[i][0]>=0)&&(y+vec[i][1]>=0)&&(x+vec[i][0]<h)&&(y+vec[i][1]<w)){
			if(map[x+vec[i][0]][y+vec[i][1]]=='.'){
				ans+=move(x+vec[i][0],y+vec[i][1]);
			}
		}
	}
	return ans;
}

int main(){
	while(cin>>w>>h,w){
		int fx,fy;
		for(int i=0;i<h;i++){
			cin>>map[i];
		}
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(map[i][j]=='@'){
					fx=i;
					fy=j;
					break;
				}
			}
		}
		int ans=move(fx,fy);
		cout<<ans<<endl;
		
	}
}

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

int field[20][20];
int w,h;
int bfs(int, int);

int main(){
  int x,y;

  while(cin>>w>>h&&w!=0&&h!=0){
    for(int j=0;j<h;j++){
      for(int i=0;i<w;i++){
        char c;
        cin >> c;
        if(c=='.')field[i][j]=1;
        else if(c=='#')field[i][j]=-1;
        else if(c=='@'){
          field[i][j]=0; 
          x=i;
          y=j;
        }
      }
    }
    int ans=bfs(x, y);
    cout << ans+1 << endl;
  }

  return 0;
}

int bfs(int x, int y){
  int count=0;
  if(y-1>=0&&field[x][y-1]==1){
    field[x][y-1]=-1;
    count++;
    count+=bfs(x, y-1);
  }
  if(y+1<h&&field[x][y+1]==1){
    field[x][y+1]=-1;
    count++;
    count+=bfs(x, y+1);
  }
  if(x-1>=0&&field[x-1][y]==1){
    field[x-1][y]=-1;
    count++;
    count+=bfs(x-1, y);
  }
  if(x+1<w&&field[x+1][y]==1){
    field[x+1][y]=-1;
    count++;
    count+=bfs(x+1, y);
  }
  return count;
}