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

//グローバルで宣言するとmain関数、再帰関数の両方から参照できて便利
int h,w;
string grid[20];
bool visit[20][20];

void rec(int y,int x);

int main(){
  while(cin >> w >> h,h){
    for(int i=0;i<h;i++){
      cin >> grid[i];      //string型を使うと入力読み込みが楽になる
      for(int j=0;j<w;j++)visit[i][j] = false;
    }

    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	if(grid[i][j] == '@')rec(i,j);  //初期位置から再帰探索をスタート
      }
    }

    int ans = 0;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	if(visit[i][j])ans++;           //到達できたタイルの数を数える
      }
    }

    cout << ans << endl;
  }
}

int dy[4] = {-1,0,1,0}, dx[4] = {0,1,0,-1};     //次の移動を定義する配列

void rec(int y,int x){
  if(visit[y][x])return;
  visit[y][x] = true;

  for(int i=0;i<4;i++){
    int ny = y+dy[i], nx = x+dx[i];             //次の移動

    if(ny<0 || nx<0 || ny>=h || nx>=w)continue; //枠外
    if(grid[ny][nx] == '#')continue;            //赤いタイル=障害物
    rec(ny,nx);
  }
}

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

using namespace std;

int tile[22][22] = {};
int w, h;
int cnt;

void fill(int x, int y) {
	tile[x][y] = 0;
	cnt++;
	if (tile[x - 1][y] == 1) fill(x - 1, y);
	if (tile[x][y - 1] == 1) fill(x, y - 1);
	if (tile[x][y + 1] == 1) fill(x, y + 1);
	if (tile[x + 1][y] == 1) fill(x + 1, y);
}

int main() {
	int x = 0, y = 0;
	while (cin >> w >> h && (w || h)) {
		cnt = 0;
		for (int i = 1; i <= h; ++i){
			for (int j = 1; j <= w; ++j) {
				char tmp;
				cin >> tmp;
				if (tmp == '.') tile[i][j] = 1;
				else if (tmp == '#') tile[i][j] = 0;
				else x = i, y = j, tile[i][j] = 1;
			}
		}
		fill(x, y);
		cout << cnt << endl;
	}

	return 0;
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
typedef unsigned int ui;
int main(){
	int w, h;
	string s;
	int i, j;
	while (cin >> w >> h) {
		if (w == 0)break;
		char tile[22][22]; int used[22][22];
		for (i = 0; i < 22; i++) {
			for (j = 0; j < 22; j++) {
				used[i][j] = 0; tile[i][j] = '#';
			}
		}
		vector<int> q1,q2;
		for (j = 1; j <= h; j++) {
			cin >> s;
			for (i = 1; i <= w; i++) {
				tile[i][j] = s[i - 1];
				if (s[i - 1] == '@') {
					q1.push_back(i); q2.push_back(j);
				}
			}
		}
		while (!q1.empty()) {
			int x = q1[0]; int y = q2[0]; used[x][y]=1;
			q1.erase(q1.begin() + 0); q2.erase(q2.begin() + 0);
			if (used[x - 1][y] == 0 && tile[x - 1][y]=='.') {
				q1.push_back(x - 1); q2.push_back(y);
			}
			if (used[x + 1][y] == 0 && tile[x + 1][y] == '.') {
				q1.push_back(x + 1); q2.push_back(y);
			}
			if (used[x][y-1] == 0 && tile[x][y-1] == '.') {
				q1.push_back(x); q2.push_back(y-1);
			}
			if (used[x][y+1] == 0 && tile[x][y+1] == '.') {
				q1.push_back(x); q2.push_back(y+1);
			}
		}
		int count = 0;
		for (i = 0; i < 22; i++) {
			for (j = 0; j < 22; j++) {
				if (used[i][j] == 1)count++;
			}
		}
		cout << count << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> VI;
typedef vector<VI> VVI;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = numeric_limits<int>::max() / 2;
const int NEG_INF = numeric_limits<int>::min() / 2;

char tiles[21][21];
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
int ans=0;
int w,h;

void dfs(int x,int y){
    tiles[x][y]='#';
    ans++;

    for(int i=0;i<4;i++){
        int nx=x+dx[i],ny=y+dy[i];

        if(0<=nx && nx<w && 0<=ny && ny<h &&
          tiles[nx][ny] == '.') dfs(nx,ny);
    }
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  while(true){
    for(int i=0;i<21;i++){
      for(int j=0;j<21;j++){
        tiles[i][j]='#';
      }
    }
    ans=0;
    cin >> w >> h;
    if(w==0) break;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
        cin >> tiles[j][i];
      }
    }
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
        if(tiles[j][i]=='@') dfs(j,i);
      }
    }
    cout << ans << endl;
  }
}

C++: #include<stdio.h>

int m,n,ans;
char ice[22][22];

void saiki(int w,int h){
	ice[w+1][h+1]='*';
	
	if(ice[w+2][h+1]=='.')saiki(w+1,h);
	if(ice[w+1][h+2]=='.')saiki(w,h+1);
	if(ice[w][h+1]=='.')saiki(w-1,h);
	if(ice[w+1][h]=='.')saiki(w,h-1);
}

int main(void){

	for(;;){
		scanf("%d %d",&m,&n);
		if(!m||!n)break;
		
		ans=0;
		
		for(int i=0;i<n;i++){
			scanf("%s",&ice[i+1]);
		}
		
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				if(ice[i+1][j+1]=='@')saiki(i,j);
			}
		}
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				if(ice[i+1][j]=='*')ans++;
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}