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>
#include<map>

using namespace std;

int main(){
  int w, h;

  while(cin >> w >> h, !(w == 0 && h == 0)){
    char tiles[h+2][w+2];
    pair<int, int> start;

    for(int i = 0; i < h+2; i++){
      for(int j = 0; j < w+2; j++){
        if(i == 0 || j == 0 || i == h+1 || j == w+1){
          tiles[i][j] = '#';
        }else{
          cin >> tiles[i][j];
          if(tiles[i][j] == '@') start = make_pair(i, j);
        }
      }
    }

    /*for(int i = 0; i < h+2; i++){
      for(int j = 0; j < w+2; j++){

        cout << tiles[i][j];
      }
      cout << endl;
    }
    cout << "start is (" << start.first << start.second << ")" << endl;
*/
    int dh[4] = {-1,  0, 1, 0};
    int dw[4] = { 0, -1, 0, 1};

    int cnt = 0;
    stack< pair<int, int> > s;
    s.push(start);
    pair<int, int> cur;

    while(!s.empty()){
      cur = s.top();
      s.pop();
      cnt++;

      for(int i = 0; i < 4; i++){
        if(tiles[cur.first + dh[i]][cur.second + dw[i]] == '.'){
          tiles[cur.first + dh[i]][cur.second + dw[i]] = '#';
          pair<int, int> next = make_pair(cur.first + dh[i], cur.second + dw[i]);
          s.push(next);
        }
      }
    }
    cout << cnt << endl;
  }
}

C++: #include <bits/stdc++.h>
using namespace std;
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define REP(i,n) for(int i=0;i<n;i++)
#define FOR(i,n1,n2) for(int i=n1;i<n2;i++)
#define speed_up    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

typedef long long int ll;
typedef pair<ll,ll> Pi;
const int INF=(ll)(1LL<<31)-1;
const ll INFl=(ll)9223372036854775807;
const int MAX=10000;
const ll MOD=(ll)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a, b)*b;}
//int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};

template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}

int room[23][23];
int main(){
	
	while(1){
		int w,h;
		cin>>w>>h;
		if(w==0&&h==0)break;
		int sh,sw;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				char a;
				cin>>a;
				if(a=='.')room[i][j]=0;
				if(a=='#')room[i][j]=-1;
				if(a=='@'){
					room[i][j]=1;
					sh=i;sw=j;
				}
			}
		}
		int ans=1;
		queue<pair<int,int> > q;
		q.push(pair<int,int>(sh,sw) );
		while(q.size()){
			pair<int,int> pp=q.front();q.pop();
			if(pp.first>0&&room[pp.first-1][pp.second]==0){
				q.push(pair<int,int>(pp.first-1,pp.second));
				room[pp.first-1][pp.second]=1;
				ans++;
			}
			if(pp.first<h-1&&room[pp.first+1][pp.second]==0){
				q.push(pair<int,int>(pp.first+1,pp.second));
				ans++;
				room[pp.first+1][pp.second]=1;
			}
			if(pp.second>0&&room[pp.first][pp.second-1]==0){
				q.push(pair<int,int>(pp.first,pp.second-1));
				ans++;
				room[pp.first][pp.second-1]=1;
			}
			if(pp.second<w-1&&room[pp.first][pp.second+1]==0){
				q.push(pair<int,int>(pp.first,pp.second+1));
				ans++;
				room[pp.first][pp.second+1]=1;
			}
		}
		cout<<ans<<endl;
		for(int i=0;i<23;i++){
			for(int j=0;j<23;j++){
				room[i][j]=0;
			}
		}
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int ans;
vector<string> Tiles;
void counts(int NW,int NH,int W,int H){
	if(NW!=0){
		if(Tiles[NH][NW-1]=='.'){
			ans++;
			Tiles[NH][NW-1] = 'D';
			counts(NW-1,NH,W,H);
		}
	}
	if(NH!=0){
		if(Tiles[NH-1][NW]=='.'){
			ans++;
			Tiles[NH-1][NW] = 'D';
			counts(NW,NH-1,W,H);
		}
	}
	if(NW<W-1){
		if(Tiles[NH][NW+1]=='.'){
			ans++;
			Tiles[NH][NW+1] = 'D';
			counts(NW+1,NH,W,H);
		}
	}
	if(NH<H-1){
		if(Tiles[NH+1][NW]=='.'){
			ans++;
			Tiles[NH+1][NW] = 'D';
			counts(NW,NH+1,W,H);
		}
	}
	return;
}

int main(){
	int W,H;
	int FW,FH;
	cin >> W >> H;
	int loop,loop2;
	while(W){
		vector<string> (H).swap(Tiles);
		for(loop=0;loop<H;loop++){
			cin >> Tiles[loop];
		}
		for(loop=0;loop<H;loop++){
			for(loop2=0;loop2<W;loop2++){
				if(Tiles[loop][loop2]=='@'){
					FW = loop2;
					FH = loop;
					loop = H;
					break;
				}
			}
		}
		ans = 1;
		counts(FW,FH,W,H);
		cout << ans << endl;
		cin >> W >> H;
	}
	return 0;
}

C++: #include <iostream>
#define REP(i, a, n) for(int i = (a); i < (n); i++)
using namespace std;

int W, H, sx, sy;
char T[20][20];
bool visited[20][20];

bool isin(int y, int x) {
  return 0 <= y && y < H && 0 <= x && x < W;
}

int dfs(int sy, int sx) {
  int dd[5] = { 0, 1, 0, -1, 0 };
  int ret = 1;

  visited[sy][sx] = true;

  REP(i, 0, 4) {
    int ny = sy + dd[i + 0];
    int nx = sx + dd[i + 1];
    if(isin(ny, nx) && !visited[ny][nx] && T[ny][nx] != '#') ret += dfs(ny, nx);
  }

  return ret;
}

int main(void) {
  while(cin >> W >> H, W) {
    REP(i, 0, H) {
      cin >> T[i];
      REP(j, 0, W) {
        if(T[i][j] == '@') {
          sy = i;
          sx = j;
        }
      }
    }
    REP(i, 0, H) REP(j, 0, W) visited[i][j] = false;

    cout << dfs(sy, sx) << endl;
  }
}