Go: package main

import (
	"bufio"
	"encoding/json"
	"fmt"
	"os"
	"strconv"
	"strings"
)

var (
	sc = bufio.NewScanner(os.Stdin)
)

type Color int

const (
	ColorBlack Color = iota
	ColorRed
)

var strByColor = map[Color]string{
	ColorBlack: "black",
	ColorRed:   "red",
}

func (c Color) String() string {
	return strByColor[c]
}

type Node struct {
	ID      int
	X, Y    int
	Color   Color
	NextIDs map[int]struct{}
}

func (n *Node) String() string {
	d, _ := json.Marshal(n)
	return string(d)
}

type Map struct {
	Width, Height int
	Nodes         map[int]*Node
}

func (m *Map) GetByID(id int) *Node {
	return m.Nodes[id]
}

func (m *Map) Get(x, y int) *Node {
	return m.Nodes[m.getID(x, y)]
}

func (m *Map) SetColor(x, y int, c Color) {
	m.Nodes[m.getID(x, y)].Color = c
}

func (m *Map) getID(x, y int) int {
	return x + y*m.Width
}

func (m *Map) init() {
	for x := 1; x <= m.Width; x++ {
		for y := 1; y <= m.Height; y++ {
			id := m.getID(x, y)
			m.Nodes[id] = &Node{ID: id, X: x, Y: y}
		}
	}
	for x := 1; x <= m.Width; x++ {
		for y := 1; y <= m.Height; y++ {
			nextIDs := map[int]struct{}{}
			if x > 1 {
				nextIDs[m.getID(x-1, y)] = struct{}{}
			}
			if x < m.Width {
				nextIDs[m.getID(x+1, y)] = struct{}{}
			}
			if y > 1 {
				nextIDs[m.getID(x, y-1)] = struct{}{}
			}
			if y < m.Height {
				nextIDs[m.getID(x, y+1)] = struct{}{}
			}
			m.Get(x, y).NextIDs = nextIDs
		}
	}
}

func NewMap(w, h int) *Map {
	m := &Map{Width: w, Height: h, Nodes: map[int]*Node{}}
	m.init()
	return m
}

type Searcher struct {
	Map        *Map
	queue      []*Node
	touchedIDs map[int]struct{}
	visitedIDs map[int]struct{}
}

func NewSearcher(m *Map) *Searcher {
	return &Searcher{
		Map:        m,
		queue:      []*Node{},
		touchedIDs: map[int]struct{}{},
		visitedIDs: map[int]struct{}{},
	}
}

func (s *Searcher) Search(start *Node) {
	s.enqueue(start)

	for {
		if len(s.queue) == 0 {
			break
		}
		for nextID := range s.dequeue().NextIDs {
			n := s.Map.GetByID(nextID)
			if n.Color == ColorRed || s.hasTouched(n) {
				continue
			}
			s.enqueue(n)
		}
	}
}

func (s *Searcher) VisitedNodeCount() int {
	return len(s.visitedIDs)
}

func (s *Searcher) hasTouched(n *Node) bool {
	_, ok := s.touchedIDs[n.ID]
	return ok
}

func (s *Searcher) enqueue(n *Node) {
	s.queue = append(s.queue, n)
	s.touchedIDs[n.ID] = struct{}{}
}

func (s *Searcher) dequeue() *Node {
	n := s.queue[0]
	s.queue = s.queue[1:]
	s.visitedIDs[n.ID] = struct{}{}
	return n
}

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])

		m := NewMap(w, h)

		// Set colors
		var start *Node
		for y := 1; y <= h; y++ {
			sc.Scan()
			for i, c := range sc.Text() {
				switch c {
				case '.', '@':
					m.SetColor(i+1, y, ColorBlack)
					if c == '@' {
						start = m.Get(i+1, y)
					}
				case '#':
					m.SetColor(i+1, y, ColorRed)
				}
			}
		}

		// Search
		s := NewSearcher(m)
		s.Search(start)
		fmt.Println(s.VisitedNodeCount())
	}
}

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;
}