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 <queue>
#include <cstring>
using namespace std;

typedef pair<int, int> P;

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

int main()
{
	char map[32][32];
	queue< P > que;
	while (true)
	{
		int w, h, x, y;
		cin >> w >> h;
		if (!(w || h)) break;
		for (int i = 0; i < h; i++)
		{
			cin >> map[i];
			char* x0 = index(map[i], '@');
			if (x0 != NULL)
			{
				x = x0 - map[i];
				y = i;
			}
		}
		int count = 0;
		que.push(P(x, y));
		map[y][x] = '#';
		while (!que.empty())
		{
			P current = que.front();
			que.pop();
			count++;
			for (int i = 0; i < 4; i++)
			{
				int nx = current.first + dx[i];
				int ny = current.second + dy[i];
				if ((0 <= nx && nx < w && 0 <= ny && ny < h) && (map[ny][nx] == '.'))
				{
					que.push(P(nx, ny));
					map[ny][nx] = '#';
				}
			}
		}
		cout << count << endl;
	}
	return 0;
}

C++: #include<iostream>

using namespace std;

const int maxSize = 21;
int mapData[maxSize][maxSize] = {0};
int W, H, Ans;

void dfs(int hh, int ww) {
  if(mapData[hh][ww] == 1) {
    mapData[hh][ww] = 0;
    Ans++;
    if(hh+1<H && mapData[hh+1][ww]==1) {
      dfs(hh+1, ww);
    }
    if(ww+1<W && mapData[hh][ww+1]==1) {
      dfs(hh, ww+1);
    }
    if(0<=hh-1 && mapData[hh-1][ww]==1) {
      dfs(hh-1, ww);
    }
    if(0<=ww-1 && mapData[hh][ww-1]==1) {
      dfs(hh, ww-1);
    }
  }
}

int main() {
  while(cin>>W>>H && !(W==0 && H==0)) {
    int startW, startH;
    Ans = 0;
    for(int i=0; i<H; i++) {
      string str;
      cin>>str;
      for(int j=0; j<W; j++) {
	if(str[j] == '.') {
	  mapData[i][j] = 1; // true
	}
	else if(str[j] == '#') {
	  mapData[i][j] = -1; // false;
	}
	else {
	  mapData[i][j] = 1; //true
	  startW = i;
	  startH = j;
	}
      }
    }
    /*
    for(int i=0; i<H; i++) {
      for(int j=0; j<W; j++) {
	cout<<mapData[i][j]<<" ";
      }
      cout<<endl;
    }
    */
    dfs(startW, startH);
    /*
    for(int i=0; i<H; i++) {
      for(int j=0; j<W; j++) {
	cout<<mapData[i][j]<<" ";
      }
      cout<<endl;
    }
    */
    cout<<Ans<<endl;
  }
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i, s, n) for(int i = s; i < (int)n; i++)
#define per(i, n) for(int i = n; i >= 0; i--)
#define ROF(i, s, n) for(int i = s; i >= (int)n; i--)
#define FORIT(i, A) for (auto i : A)
#define PRINT(x) cout << (x) << "\n"
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define MP make_pair
#define EACH(i, n) for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i)
#define SZ(a) int((a).size())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define CLR(a) memset((a), 0 ,sizeof(a))
#define dump(x) cout << #x << " = " << (x) << "\n";
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << "\n";
#define sq(n) (n) * (n)
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef priority_queue<int> maxpq;
typedef priority_queue< int, vector<int>, greater<int> > minpq;
static const double EPS = 1e-10;
static const double PI = acos( -1.0 );
static const int mod = 10007;
static const int INF = 1 << 25;
static const LL LL_INF = 1152921504606846976;
static const int dx[] = { 1, -1, 0, 0 };
static const int dy[] = { 0, 0, 1, -1 };

int W, H, a, b, ans;
VS mp;
string str;

int gcd( int m, int n ) {
	if ( n == 0 ) return m;
	return gcd( n, m % n );
}


int main() {
	while ( ~scanf("%d %d", &W, &H) && W && H ) {
		for ( int i = 0; i < W + 2; i++ ) {
			str += "#";
		}
		ans = 0;
		mp.resize( H + 2 );
		mp[ 0 ] = str;
		mp[ H + 1 ] = str;
		for ( int i = 1; i < H + 1; i++ ) {
			cin >> str;
			str = "#" + str + "#";
			mp[ i ] = str;
			for ( int j = 1; j < W + 2; j++ ) {
				if ( mp[ i ][ j ] == '@' ) {
					a = i;
					b = j;
				}
			}
		}
		queue<PII> q;
		q.push( PII( a, b ) );
		while ( q.size() ) {
			int y = q.front().first;
			int x = q.front().second;
			if ( mp[ y ][ x ] == '#' ) {
				q.pop();
				continue;
			}
			mp[ y ][ x ] = '#';
			ans++;
			q.pop();
			for ( int i = 0; i < 4; i++ ) {
				if ( mp[ y + dy[ i ] ][ x + dx[ i ] ] != '#' ) {
					q.push( PII( y + dy[ i ], x + dx[ i ] ) );
				}
			}
		}
		printf( "%d\n", ans );
		str.clear();
		mp.clear();
	}
	return 0;
}