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 search(int y, int x, int h, int w, char** field) {
    field[y][x] = '0';
    int res = 1, ny[] = {y - 1, y, y, y + 1, }, nx[] = {x, x - 1, x + 1, x, };
    for (int i = 0; i < 4; i++) {
        if (0 <= ny[i] && ny[i] < h && 0 <= nx[i] && nx[i] < w && field[ny[i]][nx[i]] == '.') {
            res += search(ny[i], nx[i], h, w, field);
        }
    }
    return res;
}

int main() {
    while (true) {
        int w, h;
        cin >> w >> h;
        if ((w | h) == 0) {
            break;
        }
        char** field = new char* [h];
        for (int i = 0; i < h; i++) {
            field[i] = new char [w + 1];
        }
        for (int i = 0; i < h; i++) {
            cin >> field[i];
        }
        int output;
        for (int y = 0; y < h; y++) {
            for (int x = 0; x < w; x++) {
                if (field[y][x] == '@') {
                    output = search(y, x, h, w, field);
                }
            }
        }
        cout << output << endl;
        for (int i = 0; i < h; i++) {
            delete[] field[i];
        }
        delete[] field;
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
int h, w;
int sx, sy;
int ct;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};

void dfs(int x, int y, vector<vector<char> > &v) {
  v[x][y] = '#';
  ++ct;
  for(int i = 0; i < 4; ++i) {
    int nx = dx[i] + x;
    int ny = dy[i] + y;
    if(nx >= 0 && ny >= 0 && ny < h && nx < w && v[nx][ny] == '.') {
      dfs(nx, ny, v);
    }
  }
  return;
}
int main() {
  for(;;) {
    scanf("%d %d", &w, &h);
    if(w == 0 && h == 0) break;
    vector<vector<char> > v(w, vector<char>(h));
    for(int i = 0; i < h; ++i) {
      for(int j = 0; j < w; ++j) {
        scanf(" %c", &v[j][i]);
      }
    }

    for(int i = 0; i < h; ++i) {
      for(int j = 0; j < w; ++j) {
        if(v[j][i] == '@') {
          sx = j, sy = i;
          break;
        }
      }
    }
    dfs(sx, sy, v);
    printf("%d\n", ct);
    h = 0, w = 0, sx = 0, sy = 0, ct = 0;
  }
  return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;
int dx[] = { -1, 0, 1, 0 };
int dy[] = { 0, -1, 0, 1 };

int main(){
	int W, H;
	while( cin >> W >> H, W, H ){
		char room[20][20];
		queue< pair<int, int> > xy;
		for( int i = 0; i < H; i++ ){
			for( int j = 0; j < W; j++ ){
				cin >> room[i][j];
				if( room[i][j] == '@' ){
					xy.push(pair<int, int>(i, j));
					room[i][j] = '#';
				}
			}
		}
		//cout << xy.front().first << ' ' << xy.front().second << endl;
		int sum = 1;
		while( !xy.empty() ){
			for( int i = 0; i < 4; i++ ){
				int x = xy.front().second + dx[i];
				int y = xy.front().first + dy[i];
				//cout << room[y][x];
				if( x < H && y < W && room[y][x] == '.' ){
					room[y][x] = '*';
					xy.push(pair<int, int>(y, x));
					sum++;
				}
			}
			//cout << endl;
			xy.pop();
		}
		for( int i = 0; i < H; i++ ){
			for( int j = 0; j < W; j++ ){
				cout << room[i][j];
			}
			cout << endl;
		}
		cout << sum << endl;
	}
}

C++: #include<iostream>

using namespace std;

const int vecx[4] = { -1,0,1,0 };
const int vecy[4] = { 0,1,0,-1 };

int h, w;
char table[20][20];
bool iswalked[20][20];
int si, sj;
int ans;

void dfs(int _i, int _j) {
	ans++;
	iswalked[_i][_j] = true;
	for (int i = 0; i < 4; i++) {
		if (_i + vecx[i] >= h || _i + vecx[i] < 0 || _j + vecy[i] >= w || _j + vecy[i] < 0)continue;
		if (!iswalked[_i + vecx[i]][_j + vecy[i]] && table[_i + vecx[i]][_j + vecy[i]] == '.') {
			dfs(_i + vecx[i], _j + vecy[i]);
		}
	}
}

int main() {
	while (cin >> w >> h, w*h) {
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				cin >> table[i][j];
				if (table[i][j] == '@')si = i, sj = j;
			}
		}
		dfs(si, sj);
		cout << ans << endl;
		ans = 0;
		for (int i = 0; i < h; i++)for (int j = 0; j < w; j++)iswalked[i][j] = false;
	}
	return 0;
}

C++: #include <iostream>
#include <cmath>
#include <vector>
#include <cctype>
#include <algorithm>
#include <string>
#include <queue>
using namespace std;

char map[21][21];
int w, h;
int tile = 0;

void dfs(int x, int y){
    if (!(0 <= x && x < w && 0 <= y < h)) return;
    tile++;
    map[y][x] = '#';
    if (map[y-1][x] == '.') dfs(x, y-1);
    if (map[y+1][x] == '.') dfs(x, y+1);
    if (map[y][x-1] == '.') dfs(x-1, y);
    if (map[y][x+1] == '.') dfs(x+1, y);
}

int main(){
    int sx, sy;
    while (cin >> w >> h, w || h){
	memset(map, '#', sizeof(map));
	tile = 0;
	for (int i = 0; i < h; i++){
	    cin >> map[i];
	    for (int p = 0; p < w; p++){
		if (map[i][p] == '@'){
		    sx = p;
		    sy = i;
		}
	    }
	}

	dfs(sx, sy);

	cout << tile << endl;
    }
    
    return 0;
}