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;
int ans;
char room[22][22] = { '0' };

void judge(int x, int y) {
	ans++;
	room[x][y] = '#';
	if (room[x - 1][y] != '#') {
		judge(x - 1, y);
	}
	if (room[x + 1][y] != '#') {
		judge(x + 1, y);
	}
	if (room[x][y - 1] != '#') {
		judge(x, y - 1);
	}
	if (room[x][y + 1] != '#') {
		judge(x, y + 1);
	}
	return;
}

int main(void) {
	while (1) {
		int w, h, human_x, human_y;
		for (int i = 0; i <= 22; i++) {
			for (int j = 0; j <= 22; j++) {
				room[j][i] = { '#' };
			}
		}
		cin >> w >> h;
		if (w == 0) return 0;
		for (int i = 1; i <= h; i++) {
			for (int j = 1; j <= w; j++) {
				cin >> room[j][i];
				if (room[j][i] == '@') {
					human_x = j;
					human_y = i;
				}
			}
		}
		ans = 0;
		judge(human_x, human_y);

		cout << ans << endl;
	}

}

C++: #include <iostream>
#include <cstring>
#include <string>

using namespace std;

const int MAX_H = 20;
const int MAX_W = 20;

// 入力
int W, H;
char field[MAX_H][MAX_W+1]; // ヌル文字を考慮
int startx, starty;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

void dfs(int x, int y) {
	field[y][x] = '$';
	for (int i = 0; i < 4; i++) {
		int newX = x + dx[i];
		int newY = y + dy[i];
		if (field[newY][newX] == '.') {
			dfs(newX, newY);
		}
	}
}

int main() {
	while (true) {
		cin >> W >> H;
		if (W == 0) {
			break;
		}
		
		memset(field, 0, sizeof(field));
		for (int i = 0; i < H; i++) {
			for (int j = 0; j < W; j++) {
				cin >> field[i][j];
				if (field[i][j] == '@') {
					startx = j;
					starty = i;
				}
			}
		}
		
		dfs(startx, starty);
		
		int ans = 0;
		for (int i = 0; i < H; i++) {
			for (int j = 0; j < W; j++) {
				if (field[i][j] == '$') {
					ans++;
				}
			}
		}
		cout << ans << endl;
	}
	
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <map>
#include <stack>
#include <list>
#include <queue>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <math.h>

#define PREP(i, m, n) for(int i = m; i < n; i++)
#define MREP(i, m, n) for(int i = m - 1; i >= n; i--)
using namespace std;

const int dirX[] = {-1, 0, 1, 0};
const int dirY[] = {0, -1, 0, 1};

struct point{
	int x;
	int y;
	point(int x = 0, int y = 0):x(x), y(y){}
};

int wideSearch(point start, vector<string> &map){
	int count = 1;
	queue<point> usablepos;
	usablepos.push(start);
	while(1){
		if(usablepos.size() == 0) break;
		point current = usablepos.front();
		usablepos.pop();
		for(int i = 0; i < 4; i++){
			if((current.y + dirY[i] >= 0 && current.y + dirY[i] < map.size()) && (current.x + dirX[i] >= 0 && current.x + dirX[i] < map[0].size())){
				if(map[current.y + dirY[i]][current.x + dirX[i]] == '.'){
					map[current.y + dirY[i]][current.x + dirX[i]] = '#';
					point tmp(current.x + dirX[i], current.y + dirY[i]);
					count++;
					usablepos.push(tmp);
				}
			}
		}
	}
	return count;
}

int main(){
	int w = 0, h = 0;
	while(cin >> w >> h && (w != 0 || h != 0)){
		vector<string> map;
		point start;
		string input;
		for(int i = 0; i < h; i++){
			cin >> input;
			map.push_back(input);
			for(int j = 0; j < w; j++){
				if(map[i][j] == '@'){
					start.x = j;
					start.y = i;
				}
			}
		}
		cout << wideSearch(start, map) << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<vector>
using namespace std;
const int MAX = 20;
char map[MAX][MAX];
bool book[MAX][MAX];
struct Point
{
	int x;
	int y;
	Point(int x = 0, int y = 0) :x(x), y(y) {}
}start;
int sum = 0;
void depthFSearch(int H, int W, Point start)
{
	int next[4][2] = { {0,1},{1,0},{0,-1},{-1,0} };
	for (int k = 0; k <= 3; k++)
	{
		int nx = start.x + next[k][0];
		int ny = start.y + next[k][1];
		if (nx < 0 || nx >= H || ny < 0 || ny >= W)
			continue;
		if (!book[nx][ny] && map[nx][ny] == '.')
		{
			book[nx][ny] = true;
			Point p(nx, ny);
			sum++;
			depthFSearch(H, W, p);
		}
	}
	return;
}
int main()
{
	while (true)
	{
		int H, W;
		cin >> H >> W;
		if (!H && !W)
			break;
		for (int i = 0; i < H; i++)
			for (int j = 0; j < W; j++)
			{
				cin >> map[i][j];
				if (map[i][j] == '@')
					start = Point(i, j);
			}
		book[start.x][start.y] = true;
		sum = 1;
		depthFSearch(H, W, start);
		cout << sum << endl;
	}
}