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 <vector>
#include <algorithm>
#include <string>
#include <cstdio>
#include <queue>
#include <map>

#define rep(i, n) for(int i = 0; i < n; i++)
#define FOR(i, a, b) for(int i = a; i < b; i++)
#define all(v) (v).begin(), (v).end()

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> P;

void p(vi v){
	rep(i, v.size()){
		cout << v[i] << ' ';
	}
	cout << endl;
}

void pp(vector<string> v){
	rep(i, v.size()){
		cout << v[i] << endl;
	}
}

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

const char wall = '#';

int seedfill(vector<string> &v, P start){
	queue<P> q;
	q.push(start);
	int count = 0;
	while(!q.empty()){
		P p = q.front();
		q.pop();
		int x = p.first;
		int y = p.second;

		if(v[y][x] != wall){
			v[y][x] = wall;
			count++;
			rep(i, 4){
				q.push(P(x+dx[i], y+dy[i]));
			}
		}
	}
	return count;
}

int solve(vector<string> &v){
	rep(i, v.size()){
		rep(j, v[i].size()){
			if(v[i][j] == '@'){
				return seedfill(v, P(j, i));
			}
		}
	}
	return -1;
}

int main(){

	int W, H;
	while(cin >> W >> H){
		if(!W && !H) break;

		vector<string> field(H+2);
		rep(i, W+2) field[0].push_back(wall);
		field[H+1] = field[0];

		FOR(i, 1, H+1){
			string tmp;
			cin >> tmp;
			field[i] = wall + tmp + wall;
		}

		cout << solve(field) << endl;
	}

	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;
using ll = long long ;
using P = pair<int,int> ;
using pll = pair<long long,long long>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e17;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979323846;
vector<int> dx = {0,0,1,-1};
vector<int> dy = {1,-1,0,0};
int h,w;
char maze[23][23];
bool seen[23][23];

int id(int i,int j){
    return i + j * h;
}

void dfs(int now){
    int i = now % h;
    int j = now / h;
    if(seen[i][j]) return;
    seen[i][j] = true;

    rep(k,4){
        int next_i = i + dx[k];
        int next_j = j + dy[k];
        if(0 <= next_i && next_i < h && 0 <= next_j && next_j < w){
            if(seen[next_i][next_j]) continue;
            if(maze[next_i][next_j] == '#') continue;
            dfs(id(next_i,next_j));
        }
    }

    return;
}

int main(){
    while(1){
        cin >> w >> h;
        if(w==0 && h==0) break;
        rep(i,h)rep(j,w) seen[i][j] = false;
        rep(i,h)rep(j,w) cin >> maze[i][j];
        int si, sj;
        rep(i,h)rep(j,w){
            if(maze[i][j]=='@') si = i,sj = j;
        }
        dfs(id(si,sj));
        int ans = 0;
        rep(i,h)rep(j,w){
            ans += seen[i][j];
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define int long long
#define Rep(i, N) for(int i = 0; i < N; i++)

typedef pair<int, int> Pi;
#define fi first
#define se second

int mx[] = {1, 0, -1, 0};
int my[] = {0, 1, 0, -1};

signed main()
{
  int W, H;
  char mas[25][25];
  
  
  
  while(cin >> W >> H, W || H) {
    queue < Pi > que;
    int ans = 1;
    Rep(i, H) Rep(j, W) {
      cin >> mas[i][j];
      if(mas[i][j] == '@') {
	que.push(Pi(i, j));
	mas[i][j] = '#';
      }
      
    }

    while(!que.empty()) {
      Pi d = que.front(); que.pop();
      int y = d.fi, x = d.se;

      Rep(i, 4) {
	int ny = y + my[i], nx = x + mx[i];
	if(ny < 0 || nx < 0 || ny >= H || nx >= W || mas[ny][nx] == '#') continue;
	ans++, mas[ny][nx] = '#';
	que.push(Pi(ny, nx));
      }
    }

    cout << ans << endl;
  }
  
  return 0;
}