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 <stack>
using namespace std;

typedef pair<int,int> P;

int w, h, sx, sy;
char t[22][22];

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

void solve(){
  stack<P> open;
  int ans = 0;

  open.push(P(sx, sy));

  while(!open.empty()){
    P p = open.top();
    open.pop();
    ans++;

    for(int i = 0; i < 4; i++){
      int nx = p.first + dx[i];
      int ny = p.second + dy[i];

      if(nx < 0 || w <= nx || ny < 0 || h <= ny || t[ny][nx] != '.') continue;
      t[ny][nx] = '#';
      open.push(P(nx, ny));
    }
  }

  cout << ans - 1 << endl;
}

int main(){
  while(cin >> w >> h, w || h){
    for(int i = 0; i < h; i++){
      cin >> t[i];
      for(int j = 0; j < w; j++){
        if(t[i][j] == '@'){
          sx = j;
          sy = i;
          t[i][j] = '.';
        }
      }
    }
    solve();
  }
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <string>
#include <utility>
#include <unordered_set>

using namespace std;

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

int main(){
    int w, h;
    while(cin >> w >> h && w > 0 && h > 0){
        string tmp;
        pair<int, int> init;
        vector<vector <int> > grid(25, vector<int>(25, -1));
        vector<vector <int> > visited(25, vector<int>(25, 0));
        for(int i = 0; i < h; i++){
            cin >> tmp;
            for (int j = 0; j < w; j++){
                if(tmp[j] == '.'){
                    grid[i][j] = 0;
                }else if(tmp[j] == '#'){
                    grid[i][j] = -1;
                }else if(tmp[j] == '@'){
                    grid[i][j] = 0;
                    init = make_pair(i, j);
                }
            }
        }
        /*for(int i = 0; i < h; i++){
            for (int j = 0; j < w; j++){
                if(grid[i][j] == 0){
                    if(i == init.first && j == init.second){
                        cout << "@";
                    }else{
                        cout << ".";
                    }
                }else if(grid[i][j] == -1){
                    cout << "#";
                }
            }
            cout << endl;
        }
        */
        queue<pair<int, int> > Q;
        Q.push(init);
        visited[init.first][init.second] = 1; 
        while(!Q.empty()){
            pair<int, int> cur = Q.front(); 
            Q.pop();
            int cur_x = cur.first;
            int cur_y = cur.second;
            for(int i = 0; i < 4; i++){
                pair<int, int> dst = make_pair(cur_x + dx[i], cur_y + dy[i]);
                int x = dst.first;
                int y = dst.second;
                if(0 <= x && 0 <= y && grid[x][y] == 0 && visited[x][y] == 0){
                    Q.push(dst);
                    visited[x][y] = 1;
                }
            }
        }
        int ans = 0;
        for(int i = 0; i < h; i++){
            for (int j = 0; j < w; j++){
                ans += visited[i][j];
            }
        }
        cout << ans << endl << endl << endl;
    }
}

C++: #include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
int n,m;
vector<string> area;
bool ok(int x,int y,int nx,int ny){
  bool res = 0<=nx&&nx<n&&0<=ny&&ny<m;
  if(res){
    res = (area[y][x]=='.'||area[y][x]=='@')&&(area[ny][nx]=='.'||area[ny][nx]=='@');
  }
  return res;
}
struct union_find{//rank,経路圧縮で高速化
  vector<long long> data;//そのノードがrootであれば-（集合のサイズ）を表し、そうでなければ親ノードidを示す。
  union_find(long long size):data(size,-1){}//コンストラクタ,size個のノードを作成し、-1で初期化
  inline long long root(long long a){//根を探す,経路圧縮
    return data[a] < 0 ? a : data[a]=root(data[a]);
  }
  inline bool find(long long a,long long b){//a,bが同じ集合に属するか調べる
    return root(a) == root(b);
  }
  inline bool unite(long long a,long long b){//小さい方の木の根に大きい方の根を上書きする
    a = root(a),b = root(b);
    if(a==b) return false;//根が同じなら終了,uniteが失敗したことを出力
    if(data[a]<data[b]) swap(a,b); //集合サイズの大きい方の根に低い方の根をつける
    data[b] += data[a];//集合サイズを統合
    data[a] = b;//yの親にxをつける
    return true;//unite成功を出力
  }
  inline long long size(long long a){//aが属する集合のサイズを出力
    return -data[root(a)];
  }
};
int main(){
  while(cin>>n>>m,n){
    union_find uf(n*m);
    area.resize(m);
    REP(i,m) cin >> area[i];
    int person = -1;
    REP(i,m)REP(j,n){
      if(area[i][j]=='@') person = i*n+j;
      REP(k,4){
        if(ok(j,i,j+dx[k],i+dy[k])){
          uf.unite(i*n+j,(i+dy[k])*n+j+dx[k]);
        }
      }
    }
    cout << uf.size(person) << endl;
  }
}