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>
#include<utility>
#include<queue>
#include<map>
using namespace std;
typedef pair<int, int> P;

const int MAX_H = 22;
const int MAX_W = 22;

char grid[MAX_H][MAX_W];
bool is_checked[MAX_H][MAX_W];

void solve(int W, int H) {
    map<P, bool> is_exist;
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            cin >> grid[i][j];
            is_checked[i][j] = false;
            is_exist[P(i, j)] = true;
        }
    }
    queue<P> Q;
    int dh[4] = {-1, 0, 1, 0}, dw[4] = {0, -1, 0, 1};
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            if (grid[i][j] == '@') {
                Q.push(P(i, j));
                is_checked[i][j] = true;
                while (!Q.empty()) {
                    P now_pos = Q.front();
                    Q.pop();
                    for (int k = 0; k < 4; k++) {
                        P new_pos = P(now_pos.first + dh[k], now_pos.second + dw[k]);
                        if (!is_exist[new_pos] || is_checked[new_pos.first][new_pos.second]) continue;
                        if (grid[new_pos.first][new_pos.second] == '#') continue;
                        is_checked[new_pos.first][new_pos.second] = true;
                        Q.push(new_pos);
                    }
                }
            }
        }
    }
    int ans = 0;
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            if (is_checked[i][j]) ans++;
        }
    }
    cout << ans << endl;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int W, H;
    while (true) {
        cin >> W >> H;
        if (W == 0 && H == 0) break;
        solve(W, H);
    }
    return 0;
}

C++: #include <iostream>
#include <string>
using namespace	std;
int result,w,h;
int d1[4]={-1,0,1,0},d2[4]={0,1,0,-1};
string a[21];

void dfs(int x,int y){

  int i;

  result++;
  a[y][x]='#';
  for(i=0;i<4;i++) if(0<=y+d2[i] && y+d2[i]<h && 0<=x+d1[i] && x+d1[i]<w) if(a[y+d2[i]][x+d1[i]] == '.') dfs(x+d1[i],y+d2[i]);

}

int main(){

  int x1,y1,i,j;

  while(cin>>w>>h,w){

    result=0;
    for(i=0;i<h;i++) cin>>a[i];

    for(i=0;i<h;i++) for(j=0;j<w;j++) if(a[i][j] == '@'){
          x1=j;
          y1=i;
	}

    dfs(x1,y1);

    cout<<result<<endl;


  }

  return 0;

}

C++: #include <iostream>
#include <vector>
#include <algorithm> 
#include <queue>
#include <functional>
using namespace std;

int w, h;
int vx[4] = { 1, 0, -1, 0 };
int vy[4] = { 0, 1, 0, -1 };
bool used[20][20];
char c[20][20];

int dfs(int x, int y){
    int ret = 1;
    for (int i = 0; i < 4; i++){
        int nextx = x + vx[i];
        int nexty = y + vy[i];

        if (0 < nextx && nextx < w + 1 && 0 < nexty && nexty < h + 1 && c[nextx][nexty] == '.' && !used[nextx][nexty]){
            used[nextx][nexty] = true;
            ret += dfs(nextx, nexty);
        }
    }

    return ret;
    
}

int main(void){

    while (true){

        cin >> w >> h;

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

        for (int i = 0; i <= 20; i++){
            for (int j = 0; j <= 20; j++){
                used[i][j] = false;
            }
        }

        int sx, sy;

        for (int i = 1; i <= h; i++){
            for (int j = 1; j <= w; j++){
                cin >> c[j][i];
                if (c[j][i] == '@'){
                    sx = j; sy = i;
                }
            }
        }

        cout << dfs(sx, sy) << endl;
    }
 

}

C++: #include<iostream>
#include<algorithm>
using namespace std;

char tile[30][30];
int W, H;
int ix, iy;
int cnt;
int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,1,0,-1 };

void dfs(int x, int y){
	tile[y][x] = '#';
	cnt++;
	for (int i = 0; i < 4; i++){
		int X = x + dx[i];
		int Y = y + dy[i];
		if (0<=X && X<W && 0<=Y && Y<H && tile[Y][X]!='#'){
			dfs(X, Y);//関数の再起
		}
	}
}





int main(){

	while (cin >> W >> H, W + H != 0){
		for (int y = 0; y < H; y++){
			for (int x = 0; x < W; x++){
				cin >> tile[y][x];
				if (tile[y][x] == '@'){
					ix = x;
					iy = y;
				}
			}
		}
		dfs(ix, iy);

		cout << cnt << endl;
		cnt = 0;
	}

	return 0;
}

C++: #include <cstdio>

using namespace std;

#define rep(i,x) for(int i=0;i<(x);++i)

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

char mp[20][22];
int H, W;

void dfs(int y, int x)
{
	if (y < 0 || y >= H || x < 0 || x >= W || mp[y][x] == '#' || mp[y][x] == ' ') return;

	mp[y][x] = ' ';

	rep(i, 4) dfs(y + dy[i], x + dx[i]);
}

int main()
{
	while (scanf("%d %d", &W, &H), H || W) {
		int sy, sx;

		rep(i, H) rep(j, W) {
			scanf("\n%c", &mp[i][j]);
			if (mp[i][j] == '@') sy = i, sx = j, mp[i][j] = '.';
		}

		dfs(sy, sx);

		int cnt = 0;

		rep(i, H) rep(j, W) if (mp[i][j] == ' ') cnt++;

		printf("%d\n", cnt);
	}
}