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>
using namespace std;

struct tile
{
    int row;
    int col;
    bool is_visited;
    bool is_black;
};

tile start;
tile map[20][20];

int W, H;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};

bool valid(int row, int col)
{
    return row >= 0 && row < H && col >= 0 && col < W;
}

int bfs(tile map[20][20])
{
    queue<tile> Q;
    Q.push(start);
    int ans = 0;

    while (!Q.empty())
    {
        ans += 1;
        tile src = Q.front();
        Q.pop();

        for (int i = 0; i < 4; i++)
        {
            int row = src.row + dx[i];
            int col = src.col + dy[i];

            // map の範囲外の場合
            if (!valid(row, col))
            {
                continue;
            }

            if (map[row][col].is_black && !map[row][col].is_visited)
            {
                map[row][col].is_visited = 1;
                Q.push(map[row][col]);
            }
        }
    }

    return ans;
}

int main()
{
    while (cin >> W >> H && W)
    {
        // 入力
        for (int row = 0; row < H; row++)
        {
            for (int col = 0; col < W; col++)
            {
                char c;
                cin >> c;

                if (c == '.')
                {
                    map[row][col] = {row, col, 0, 1};
                }
                else if (c == '@')
                {
                    map[row][col] = {row, col, 1, 1};
                    start = map[row][col];
                }
                else
                {
                    map[row][col] = {row, col, 0, 0};
                }
            }
        }

        // BFS
        cout << bfs(map) << endl;
    }
}

C++: #include<iostream>
#include<vector>
#include<queue>
#include<string>


int main(){
    std::vector<int> tiles;
    std::queue<int> unvisited_tile_q;
    int width, height, counter, pos;
    std::string line;

    while(true){
        std::cin >> width >> height;
        if( width == 0 && height == 0 ) break;
        tiles.resize(width*height);
        pos = 0;
        for( int h = 0; h < height; h++ ){
            std::cin >> line;
            for(std::string::iterator it = line.begin(); it != line.end(); ++it) {
                switch(*it){
                    case '#':
                        tiles[pos] = 0;
                        break;
                    case '@':
                        unvisited_tile_q.push(pos);
                    case '.':
                        tiles[pos] = 1;
                }
                pos++;
            }
        }

        counter = 0;
        while( !unvisited_tile_q.empty() ){
            pos = unvisited_tile_q.front();
            unvisited_tile_q.pop();
            if( tiles[pos] == 1 ){
                counter++;
                tiles[pos] = -1;
            } else continue;
            //left
            if ( pos - 1 >= 0 && pos % width > 0 && tiles[pos-1] == 1 ) unvisited_tile_q.push(pos-1); 
             //right
            if ( pos + 1 <= tiles.size() && pos % width < width - 1 && tiles[pos+1] == 1 ) unvisited_tile_q.push(pos+1);
            //up
            if ( pos - width >= 0 && tiles[pos-width] == 1 ) unvisited_tile_q.push(pos-width);
            //down
            if ( pos + width < tiles.size() && tiles[pos+width] == 1 ) unvisited_tile_q.push(pos+width);
        }
	    //output result
	    std::cout << counter << std::endl;
    }   
    return 0;
}

C++: #include<stdio.h>
char a[21][21];
int c;
void dfs(int y,int x){
  c++;
  a[y][x]=0;
  if(a[y-1][x]=='.'&&y!=0)dfs(y-1,x);
  if(a[y+1][x]=='.'&&y!=20)dfs(y+1,x);
  if(a[y][x-1]=='.'&&x!=0)dfs(y,x-1);
  if(a[y][x+1]=='.'&&x!=20)dfs(y,x+1);
  return;
}
int main(){
  int w,h,i,j;
  while(1){
    scanf("%d%d",&w,&h);
    if(w==0&&h==0)break;
    for(i=0;i<21;i++){
      for(j=0;j<21;j++){
	a[i][j]=0;
      }
    }
    for(i=0;i<h;i++){
      for(j=0;j<=w;j++){
	scanf("%c",&a[i][j]);
      }
    }
    c=0;
    for(i=0;i<h;i++){
      for(j=0;j<w;j++){
	if(a[i][j]=='@')dfs(i,j);
      }
    }
    printf("%d\n",c);

   
  }
  return 0;
}

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

#define loop(i,a,b) for(int i=(a); i<int(b); i++)
#define rep(i,b) loop(i,0,b)
typedef vector<int> vi;
typedef vector<string> vs;

int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};

inline bool in(int x, int y,int w,int h){
    return 0<=x&&0<=y && y<h && x<w;
}

int solve(vs & g){
    int h=g.size();int w=g[0].size();
    int sx,sy;
    rep(i,h)rep(j,w)if(g[i][j]=='@'){
        sx=j;sy=i;
    }


    while(1){
        bool fine=true;
        
        rep(y,h)rep(x,w){
            if(g[y][x]=='@'){
                rep(d,4){
                    int nx=x+dx[d];
                    int ny=y+dy[d];
                    if(!in(nx,ny,w,h)) continue;
                    if(g[ny][nx]!='.') continue;
                    g[ny][nx]='@';
                    fine=false;
                }
            }
        }
        
        if(fine)break;
    }

    // rep(i,h)cout<<g[i]<<endl;

    int c=0;
    rep(i,h)rep(j,w)c+=g[i][j]=='@';
    return c;
}


int main(){
    int w,h;
    while(cin>>w>>h && w){
        vs g(h);
        rep(i,h)cin>>g[i];
        cout<<solve(g)<<endl;
    }
}

C++: #include <iostream>
#include <limits.h>
#include <queue>
#include <algorithm>
#include <map>


using namespace std;

typedef pair<int,int> P;

const int INF=INT_MAX;
const int N=30;


char tiles[N][N];
int W,H;
int ans=0;


void solve(int i, int j){
  
  tiles[i][j]='x';
  ans++;

  int di[4]={-1,0,1,0};
  int dj[4]={0,-1,0,1};

  for(int k=0;k<4;k++){
    int ni=i+di[k],nj=j+dj[k];
    if(0<=ni&&ni<H && 0<=nj&&nj<W)
      if(tiles[ni][nj]=='.'){
	solve(ni,nj);
      }
  }   
  
}



int main(){

  P start;

  
  while(1){

    cin>>W>>H;
    start = P(INF,INF);

    if(W==0 && H==0) break;
    ans=0;
    
    for(int i=0;i<H;i++){
      cin>>tiles[i];
      if(start.first==INF){
	for(int j=0;j<W;j++)
	  if(tiles[i][j]=='@') start=P(i,j);
      }
    }
    solve(start.first,start.second);
    cout<<ans<<endl;
  }

  return 0;
}