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<iomanip>
#include<cstdio>
#include<string>
#include<cstring>
#include<climits>
#include<algorithm>
#include<vector>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<deque>
#include<tuple>
#include<list>
#include<unordered_map>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vii vector<int>
#define vll vector<ll>
#define lb lower_bound
#define pb push_back
#define mp make_pair
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep2(i,a,b) for(ll i=a;i<b;i++)
#define repr(i,n) for(ll i=n-1;i>=0;i--)
#define all(x) x.begin(),x.end()
// #define int ll
using namespace std;
const int INF (1 << 30);
const ll LLINF (1LL << 55LL);
const int MOD = 1000000007;
const int MAX = 510000;

ll dx[4] ={1,0,-1,0} , dy[4] ={0,1,0,-1};
ll h,w;
ll nowx,nowy;
char field[30][30];

void bfs(){
    ll d[30][30];
    queue<pll> que;
    rep(i,h)rep(j,w) d[i][j]=0;
    d[nowx][nowy]=1;
    que.push(pll(nowx,nowy));
    ll ans=1;
    while(!que.empty()){
        pll p = que.front();
        que.pop();
        rep(i,4){
            ll nx = p.first + dx[i] , ny = p.second + dy[i];
            if( nx>=0 && nx <h && ny >=0 && ny < w  && d[nx][ny] == 0 ){
                if(field[nx][ny]=='.'){
                    que.push(pll(nx,ny));
                    ans++;
                }
                d[nx][ny]=1;
            }
        }
    }
    cout<<ans<<endl;
    return ;
}


int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    
    while(true){
        cin>>w>>h;
        if(w==0&&h==0) break;
        rep(i,h)rep(j,w){
            cin>>field[i][j];
            if(field[i][j]=='@') {
                nowx=i,nowy=j;
            }
        }
        bfs();
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

bool flags[21][21];
char maps[21][21];
int W, H;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};

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

  flags[x][y] = true;
  int sum = 0;
  for(int i = 0; i < 4; i++){
    sum += dfs(x + dx[i], y + dy[i]);
  }
  
  return sum + 1;
}

int main()
{
  while(cin >> W >> H && !((W * H) == 0))
    {
      memset(maps, 0, 21 * 21 * sizeof(char));
      memset(flags, 0, 21 * 21 * sizeof(bool));

      int sx, sy;

      for(int i = 0; i < H; i++) {
        for(int j = 0; j < W; j++) {
          cin >> maps[i][j];
          if(maps[i][j] == '@'){
            sx = i;sy = j;
          }
        }
      }
      cout << dfs(sx, sy) << endl;
    }
  return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) { return n ? 1 + __builtin_popcount(n & (n - 1)) : 0; }
#endif

#define foreach(it, c) for (__typeof__((c).begin()) it=(c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount

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

template <class T> void max_swap(T& a, const T& b) { a = max(a, b); }
template <class T> void min_swap(T& a, const T& b) { a = min(a, b); }

typedef long long ll;
typedef pair<int, int> pint;

const double PI = acos(-1.0);
const int dx[] = { 0, 1, 0, -1 };
const int dy[] = { 1, 0, -1, 0 };



int main()
{
    int w, h;
    while (scanf("%d%d", &w, &h), w | h)
    {
	char tile[32][32];
	memset(tile, '#', sizeof(tile));
	for (int i = 1; i <= h; ++i)
	{
	    scanf("%s", tile[i] + 1);
	    tile[i][w + 1] = '#';
	}

	int sx, sy;
	for (int y = 1; y <= h; ++y)
	    for (int x = 1; x <= w; ++x)
		if (tile[y][x] == '@')
		    sx = x, sy = y;

	int res = 0;
	queue<pint> q;
	q.push(pint(sx, sy));
	while (!q.empty())
	{
	    pint t = q.front(); q.pop();
	    int x = t.first, y = t.second;

	    if (tile[y][x] == '#')
		continue;

	    ++res;
	    tile[y][x] = '#';

	    for (int i = 0; i < 4; ++i)
		q.push(pint(x + dx[i], y + dy[i]));
	}
	printf("%d\n", res);
    }
}