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 <cstdio>
#include <string>
#include <iostream>
using namespace std;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
char tile[40][40];
int h, w;

int search(int nx, int ny)
{
	int sum = 0;
	if (ny == 0 || nx == 0 || ny == h + 1 || nx == w + 1)return (sum);
	if (tile[nx][ny] == '.' || tile[nx][ny] == '@'){
		sum++;
	}
	
	tile[nx][ny] = '#';
	
	for (int i = 0; i < 4; i++){
		if (tile[nx + dx[i]][ny + dy[i]] != '#'){
			sum += search(nx + dx[i], ny + dy[i]);
		}
	}
	return (sum);
}
		
int main()
{
	while (1){
		
		scanf("%d %d", &w, &h);
		
		if (h == 0 && w == 0)return (0);
		int mx, my;
		for (int i = 1; i <= h; i++){
			for (int j = 1; j <= w; j++){
				scanf(" %c", &tile[j][i]);
				if (tile[j][i] == '@'){
					mx = j;
					my = i;
				}
			}
		}
		printf("%d\n", search(mx, my));
	}

}

C++: #include<iostream>
#include<iomanip>
#include<queue>
#include<string>
#include<stack>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<cassert>
#include<ctime>
#include<algorithm>
#include<utility>
#include<map>
#include<set>
#include<vector>
#include<functional>

using namespace std;

#ifdef DEBUG
#define dprintf      printf
#define dout(x)      cout<<#x" = "<<(x)<<endl
#define darray(x,n)    {int i;rep(i,n)cout<<#x"["<<i<<"] = "<<*((x)+i)<<endl;}
#define dloop(i,s,g) for((i)=(s);(i)<(g);(i)++)
#define drep(i,n)    for((i)=0;(i)<(n);(i)++)
#define dstop()      scanf("%*c")
#else
#define dprintf      (1)?0:printf
#define dout(x)
#define darray(x,n)
#define dloop(i,s,g) if(1){}else
#define drep(i,n)    if(1){}else
#define dstop()      if(1){}else
#endif

#define F    first
#define S    second
#define pb   push_back
#define mp   make_pair

#define loop(i,s,g) for((i)=(s);(i)<(g);(i)++)
#define rep(i,n)    for((i)=0;(i)<(n);(i)++)
#define all(x)      (x.begin(),x.end())
#define in(T,...) T __VA_ARGS__; impl(__VA_ARGS__);
#define array(T,id,n) T id[n]; rep(i,n)cin>>id[i];

//#define int long long;
#ifndef int
#define INF (0x7fffffff)
#else
#define INF (0x7fffffffffffffff)
#endif

typedef  long long           ll;
typedef  unsigned            ui;
typedef  unsigned long long  ull;
typedef  pair<int,int>       i_i;
typedef  pair<ll,int>        ll_i;
typedef  pair<ll,ll>         ll_ll;
typedef  pair<double,int>    d_i;
typedef  pair<double,double> d_d;

void impl(){};
template <typename T,typename... TS >
void impl(T &head,TS &... tail)
{
  cin>>head;
  impl(tail ...);
}

char a[30][30]={};
char n='#';
char o='.';						       

int dfs(int score,int x,int y)
{
  a[y][x]=n;
  score++;
  dprintf("x,y=%2d,%2d = %d\n",x,y,score);
  if(a[y+1][x]==o)
    score=dfs(score,x,y+1);
  if(a[y-1][x]==o)
    score=dfs(score,x,y-1);
  if(a[y][x+1]==o)
    score=dfs(score,x+1,y);
  if(a[y][x-1]==o)
    score=dfs(score,x-1,y);
  
  return score;
}

signed main(void)
{
  while(1)
    {
      in(int,w,h);
      if(w==0)
	break;
      int i,j;
      rep(i,30)rep(j,30)a[i][j]=0;
      int x,y;
      rep(i,h)
	rep(j,w)
	{
	  scanf(" %c",&a[i][j]);
	  if(a[i][j]=='@')
	    x=j,y=i;
	}
      cout<<dfs(0,x,y)<<endl;
    }
  return 0;
}

C++: #include<iostream>
#include<string>
#include<vector>

using namespace std;

void Tile1(vector<string>&, int, int, int, int);
void Tile2(vector<string>&, int, int, int, int);

int main(){
  int i, j, w, h, isFound, count;
  string str;
  vector<string> data;

  while(1){
    cin >> w >> h;
    if(w == 0 && h == 0) break;

    for(i=0; i<h; ++i){
      cin >> str;
      data.push_back(str);
    }

    for(i=0, isFound=0; i<h; ++i){
      for(j=0; j<w; ++j){
        if(data[i][j] == '@'){
	  isFound = 1;
	  break;
	}
      }
      if(isFound == 1) break;
    }

    Tile1(data, i, j, w, h);

    for(i=0, count=0; i<h; ++i){
      for(j=0; j<w; ++j){
        if(data[i][j] == '@') ++count;
      }
    }

    cout << count << endl;
    data.clear();
  }

  return 0;
}

void Tile1(vector<string>& data, int i, int j, int w, int h){
  data[i][j] = '@';
  Tile2(data, i-1, j, w, h);
  Tile2(data, i, j-1, w, h);
  Tile2(data, i+1, j, w, h);
  Tile2(data, i, j+1, w, h);
}

void Tile2(vector<string>& data, int i, int j, int w, int h){
  if(i>-1 && i<h && j>-1 && j<w && data[i][j]=='.')
    Tile1(data, i, j, w, h);
}