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 > -1 ){//上をチェック 上端でなければ
        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;

/*
    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>
#include<queue>
#include <utility>
#include <functional>
//#include<>
using namespace std;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
//#define int long long;
signed main(){
  int w, h;
  while(cin >> w >> h, w){
    int cnt{};
    vector<string> v(h);
    for(int i = 0; i < h; ++i){
      cin >> v[i];
    }
    //initialize
    pair<int, int> p;
    queue<pair<int, int>> q;	
    for(int i = 0; i < h; ++i){
      for(int j = 0; j < w; ++j){
	if(v[i][j] == '@'){
	  p = make_pair(i,j);
	  q.push(p);
	}
      }
    }
    while(!q.empty()){
      pair<int, int> p;
      p = q.front();
      q.pop();

      for(int k = 0; k < 4; ++k){
	int x = p.first + dx[k]; int y = p.second + dy[k];
	if(0 > x || h <= x || 0 > y || w <= y)continue;
	if('.' == v[x][y]){
	  v[x][y] = '@';
	  ++cn
	  q.push(make_pair(x, y));
	}
      }
    }
    cout << cnt+1 << endl;
  }
  return 0;
}

C++: #include<cstdio>
#include <iostream>
#include<stack>

using namespace std;

struct Coord {
  int row;
  int col;
};

int solve (int W, int H);
  
int main () {
  int W, H;

  while(W!=0 && H!=0)
    {
      scanf("%d%d", &W, &H);
      if(W!=0 && H!=0)
	{
          printf("%d\n", solve(W,H));
	}
    }
  return 0;
}

int solve (int W, int H) {
  int n=0;
  char M[H+2][W+2];
  Coord start;
  for (int i=0; i<=W+1; i++) { M[0][i] = '#'; M[H+1][i] = '#'; }
  for (int i=1; i<=H; i++) { M[i][0] = '#'; M[i][W+1] = '#'; } 
  for (int i=1; i<=H; i++) {
    for (int j=1; j<=W; j++) {
      scanf(" %c", &M[i][j]);
      if (M[i][j]=='@') {
	start.row = i;
	start.col = j;
      }
    }
  }

  
  stack<Coord> S;
  S.push(start);
  while (!S.empty()) {
    Coord current = S.top(); S.pop();
    n++;
    int D1[]={1,0,-1,0}; int D2[]={0,-1,0,1};
    for (int d=0; d<4; d++) {
      Coord nc = current;
      nc.row += D1[d];
      nc.col += D2[d];
      if(M[nc.row][nc.col]=='.')
	{
	  M[nc.row][nc.col] = '*';
	  S.push(nc);
	}
    } 
    
  }
  return n;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int (i) = (0); (i) < (n); ++(i))

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

int W, H;
char B[30][30];

int dfs(int y, int x) {
   int ret = 1;
   B[y][x] = '#';
   rep (i, 4) {
      int nx = x + dx[i], ny = y + dy[i];
      if (nx < 0 || nx >= W || ny < 0 || ny >= H || B[ny][nx] == '#') continue;
      ret += dfs(ny, nx);
   }
   return ret;
}

int main() {
   while (1) {
      cin >> W >> H;
      if (H*W == 0) break;

      rep (i, H) cin >> B[i];

      rep (i, H) {
         rep (j, W) {
            if (B[i][j] == '@') {
               cout << dfs(i, j) << endl;
            }
         }
      }
   }

   return 0;
}

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

char a[22][22];
int w, h;
int yy[4] = {-1, 0, 0, 1}, xx[4] = {0, 1, -1, 0};
int solve(int y, int x){
	if(a[y][x] == '#')return 0;
	a[y][x] = '#';
	int ans = 0;
	for(int i = 0;i < 4;i++){
		ans += solve(y+yy[i], x+xx[i]);
	}
	return ans+1;
}

int main(){
	int y, x;
	while(cin >> w >> h){
		fill((char*)a, (char*)(a+22), '#');
		for(int i = 1;i <= h;i++)
		for(int j = 1;j <= w;j++){
			cin >> a[i][j];
			if(a[i][j] == '@'){y = i;x = j;}
		}
		cout << solve(y, x) << endl;
	}
	return 0;
}