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;

void search(int x, int y);

int w, h;
char maze[22][22]; //行（y)　列（x）
bool reached[22][22];
int ans;

int main() {
  while(cin >> w >> h) {
    if(w == 0 && h == 0) return 0;

    // 初期化
    for(int k = 0; k < h; k++) {
      for(int l = 0; l < w; l++) {
        maze[k][l] = '#';
        reached[k][l] = false;
      }
    }
    ans = 0;

    //fill(reached, false);
    // スタート座標
    int a, b;
    for(int i = 0; i < h; i++) {
      for(int j = 0; j < w; j++) {
        cin >> maze[i][j];
        if(maze[i][j] == '@') {
          a = i;
          b = j;
          // cout << a << " " << b << endl;
        }
      }
    }

    search(a, b);
    cout << ans << endl;

    // for(int i = 0; i < h; i++) {
    //   for(int j = 0; j < w; j++) cout << maze[i][j];
    //   cout << endl;
    // }
  }
}

void search(int y, int x) {
  // 迷路の外側か壁の場合は何もしない
  if(x < 0 || w <= x || y < 0 || h <= y || maze[y][x] == '#') return;
  // 以前に到達していたら何もしない
  if(reached[y][x]) return;
  ans++;
  reached[y][x] = true;  //　到達したよ

  // ４方向を試す
  search(y, x + 1); //右
  search(y, x - 1); //左
  search(y + 1, x); //下
  search(y - 1, x); //上
}

C++: #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<string>
#include<random>
#include<map>
#include<queue>
using namespace std;

int main(){

  int w, h;
  while(cin >> w >> h){
    if( w == 0 && h == 0 ) break;

    vector<string> t(h);
    for(int i=0; i<h; i++) cin >> t[i];
    int x, y;
    for(int i=0; i<h; i++){
      for(int j=0; j<w; j++){
        if( t[i][j] == '@' ){
          x = j;
          y = i;
        }
      }
    }

    vector< vector<bool> > b(h, vector<bool> (w, false));
    b[y][x] = true;
    queue< pair<int, int> > q;
    q.push( make_pair(x, y) );
    int dx[4] = {0, 1, 0, -1};
    int dy[4] = {1, 0, -1, 0};
    while(true){
      if( q.empty() ) break;

      pair<int, int> p = q.front();
      q.pop();
      int px = p.first;
      int py = p.second;

      for(int dir=0; dir<4; dir++){
        int tx = px + dx[dir];
        int ty = py + dy[dir];

        if( tx < 0 || tx >= w || ty < 0 || ty >= h ) continue;
        if( b[ty][tx] ) continue;
        if( t[ty][tx] == '#' ) continue;

        b[ty][tx] = true;
        q.push( make_pair(tx, ty) );
      }
    }
    int cnt = 0;
    for(int i=0; i<h; i++){
      for(int j=0; j<w; j++){
        if( b[i][j] ) cnt++;
      }
    }
    cout << cnt << endl;
  }

  return 0;
}

C++: #include<iostream>
#include<queue>
using namespace std;
int main() {
	int w, h;
	while (cin >> w >> h) {
		if (!w && !h)return 0;
		queue<pair<int, int> > q;
		char map[114][514];
		for (int i = 1; i <= w; i++) {
			for (int j = 1; j <= h; j++) {
				cin >> map[i][j];
				if (map[i][j] == '@')q.push(make_pair(i, j));
			}
		}
		int ans = 0;
		while (q.size()) {
			ans++;
			int x = q.back().first;
			int y = q.back().second;
			q.pop();
			map[x][y] = '#';
			if (map[x - 1][y] == '.')
				q.push(make_pair(x - 1, y));
			if (map[x + 1][y] == '.')
				q.push(make_pair(x + 1, y));
			if (map[x][y - 1] == '.')
				q.push(make_pair(x, y - 1));
			if (map[x][y + 1] == '.')
				q.push(make_pair(x, y + 1));
		}
		cout << ans << endl;
	}
}

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

using namespace std;

int cor2Idx(const int i, const int j, const int width)
{
    return i * width + j;
}

void idx2Cor(const int idx, int &i, int &j, const int width)
{
    i = idx / width;
    j = idx % width;
}

bool isRange(const int i, const int j, const int w, const int h)
{
    return i >= 0 && i < h && j >= 0 && j < w;
}

void refine(vector<char> &v, const int i, const int j, const int w, const int h, int &ans)
{
    int dx[] = {1, 0, -1, 0};
    int dy[] = {0, 1, 0, -1};

    for (int k = 0; k < 4; k++)
    {
        if (!isRange(i + dx[k], j + dy[k], w, h))
            continue;
        int idx = cor2Idx(i + dx[k], j + dy[k], w);
        if (v[idx] == '-' || v[idx] == '#')
            continue;
        else if (v[idx] == '.')
        {
            v[idx] = '-';
            ans++;
            refine(v, i + dx[k], j + dy[k], w, h, ans);
        }
    }
}

int main()
{
    while (true)
    {
        int w;
        scanf("%d", &w);
        int h;
        scanf("%d", &h);

        if (w == 0 && h == 0)
            break;

        int si, sj;
        vector<char> v(w * h);
        for (int i = 0; i < w * h; i++)
        {
            char buf;
            scanf("%c", &buf);
            if (buf == '\n')
                i--;
            else
            {
                v[i] = buf;
                if (buf == '@')
                    idx2Cor(i, si, sj, w);
            }
        }

        int ans = 1;
        refine(v, si, sj, w, h, ans);
        printf("%d\n", ans);
    }

    return 0;
}

C++: #include <iostream>

using namespace std;

#define MAX_H 22
#define MAX_W 22

int W, H, X, Y, A;

char tile[MAX_H][MAX_W];

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

void dfs(int x, int y)
{
	tile[y][x] = '@';

	for (int i = 0; i < 4; i++)
	{
		if (0 <= x + dx[i] && x + dx[i] <= W - 1 && 0 <= y + dy[i] && y + dy[i] <= H - 1)
		{
			if (tile[y + dy[i]][x + dx[i]] == '.')
			{
				dfs(x + dx[i], y + dy[i]);
			}
		}
	}
}

int main()
{
	while (true)
	{
		cin >> W >> H;

		if (W == 0 && H == 0) { break; }

		for (int i = 0; i < H; i++)
		{
			for (int j = 0; j < W; j++)
			{
				cin >> tile[i][j];

				if (tile[i][j] == '@')
				{
					X = j;
					Y = i;
				}
			}
		}

		dfs(X, Y);

		A = 0;

		for (int i = 0; i < H; i++)
		{
			for (int j = 0; j < W; j++)
			{
				if (tile[i][j] == '@')
				{
					A++;
				}
			}
		}

		cout << A << endl;
	}

	return 0;
}