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<bits/stdc++.h>
using namespace std;

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

int main(){
    int w,h;
    while(cin>>w>>h,w){
        vector<string> s(h);
        for(int i=0;i<h;i++)
            cin>>s[i];
        int x,y;
        for(int i=0;i<h;i++)
            for(int j=0;j<w;j++)
                if(s[i][j]=='@')
                    x=j,y=i;
        vector<vector<int>> able(h,vector<int>(w,0));
        able[y][x]=1;
        for(int k=0;k<w*h;k++)
            for(int i=0;i<h;i++)
                for(int j=0;j<w;j++)
                    if(able[i][j]){
                        for(int r=0;r<4;r++)
                            if(0<=i+dy[r] && i+dy[r]<h && 0<=j+dx[r] && j+dx[r]<w && s[i+dy[r]][j+dx[r]]!='#')
                            able[i+dy[r]][j+dx[r]]=1;



                    }
        int res=0;
        for(int i=0;i<h;i++)
            for(int j=0;j<w;j++)
                res+=able[i][j];
        cout<<res<<endl;


    }


    return 0;
}

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

#define REP(i,n) for(int i=0;i<(n);i++)
#define REAP(i,a,n) for(int i=(a);i<(n);i++)
#define YES cout<<"Yes"<<endl
#define NO cout<<"No"<<endl
#define fr first
#define sc second
#define pb push_back
#define All(v) v.begin(),v.end()
typedef long long ll;
typedef pair<int,int> P;

int h,w;
string table[21];
int dp[21][21];
int ans;

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

void puts_max(){
    REP(i,h+1){
        REP(j,w+1){
            dp[i][j]=INT_MAX;
        }
    }
}

void solve(int y,int x){
    ans++;
    dp[y][x]=0;
    REP(i,4){
        if(y+dy[i]>=0 && y+dy[i]<h && x+dx[i]>=0 && x+dx[i]<w){
            if(dp[y+dy[i]][x+dx[i]]!=0 && table[y+dy[i]][x+dx[i]]!='#'){
                solve(y+dy[i],x+dx[i]);
            }
        }
    }
}

int main(){
    P st;
    while(cin>>w>>h,w){
        ans=0;
        REP(i,h)cin >> table[i];

        //cout << 1 << endl;
        REP(i,h){
            REP(j,w){
                if(table[i][j]=='@'){
                    st.fr=i;
                    st.sc=j;
                }
            }
        }
        puts_max();
        solve(st.fr,st.sc);
        cout << ans << endl;
    }
}

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