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

using namespace std;
vector<int> ans;

int main(){
	while(1){
		int w,h;
		cin >> w >> h;
		if(w + h == 0) break;
		int cf[20][20];
		for(int i = 0;i < h;i++){
			for(int j = 0;j < w;j++){
				cf[i][j] = -1;
			}
		}
		bool field[20][20];
		for(int i = 0;i < h;i++){
			for(int j = 0;j < w;j++){
				field[i][j] = false;
			}
		}
		int px,py;
		map<int,int> ch;
		for(int i = 0;i < h;i++){
			for(int j = 0;j < w;j++){
				char t;
				cin >> t;
				if(t == '.') field[i][j] = true;
				else if(t == '@'){ px = j; py = i;field[i][j] = true;}
			}
		}
		
		int counta = 0;
		for(int i = 0;i < h;i++){
			for(int j = 0;j < w;j++){
				if(field[i][j]){
					bool ue = (i != 0 && cf[i-1][j] != -1);
					bool yoko = (j != 0 && cf[i][j-1] != -1);
					if(ue)cf[i][j] = cf[i-1][j];
					else if(yoko) cf[i][j] = cf[i][j-1];
					else{cf[i][j] = counta;counta++;}
					if(ue && yoko){
						if(cf[i-1][j] != cf[i][j-1]){
							if(cf[i-1][j] < cf[i][j-1]) ch[cf[i][j-1]] = cf[i-1][j];
							else ch[cf[i-1][j]] = cf[i][j-1];
						}
					}
				}
			}
		}
		for(int i = 0;i < h;i++){
			for(int j = 0;j < w;j++){
				if(cf[i][j] != -1){
					map<int,int>::iterator itr;
					while((itr = ch.find(cf[i][j])) != ch.end()){
						cf[i][j] = ch[cf[i][j]];
					}
				}
			}
		}
		
		int pn = cf[py][px];
		int count = 0;
		for(int i = 0;i < h;i++){
			for(int j = 0;j < w;j++){
				if(cf[i][j] == pn) count++;
			}
		}
		ans.push_back(count);
	}
	for(int i = 0;i < ans.size();i++) cout << ans[i] << endl;
	return 0;
}

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

//repetition
#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define rep(i, n) for(ll i = 0; i < (ll)(n); i++)

//container util
#define all(x) (x).begin(),(x).end()

//typedef
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VLL;
typedef vector<VLL> VVLL;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;

//const value
//const ll MOD = 1e9 + 7;
const int dx[] = {0,1,0,-1};//{0,0,1,1,1,-1,-1,-1};
const int dy[] = {1,0,-1,0};//{1,-1,0,1,-1,0,1,-1};

//conversion
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
inline ll toLL(string s) {ll v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

void dfs(int ci, int cj, vector<string>& board, VVI& visited, int h,int w){
  visited[ci][cj] = 1;
  rep(dir,4){
    if((ci + dy[dir]) < h && (cj + dx[dir]) < w &&
       (ci + dy[dir]) >= 0 && (cj + dx[dir]) >= 0  &&
       visited[(ci + dy[dir])][(cj + dx[dir])] == 0 &&
       board[(ci + dy[dir])][(cj + dx[dir])] == '.'){
      dfs((ci + dy[dir]),(cj + dx[dir]),board,visited,h,w);
    }
  }
}

void solve(int h, int w){
  vector<string> board(h);
  rep(i,h) cin >> board[i];
  VVI visited(h,VI(w,0));
  int sy = 0;
  int sx = 0;

  rep(i,h)rep(j,w){
    if(board[i][j] == '@'){
      sx = j;
      sy = i;
      break;
    }
  }
  dfs(sy,sx,board,visited,h,w);

  int cnt = 0;
  rep(i,h)rep(j,w) if(visited[i][j]) cnt++;
  cout << cnt << endl;
}
int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);
  int h,w;
  while(true){
    cin >> w >> h;
    if(h == 0 &&  w == 0){
      break;
    }
    solve( h,  w);
  }
  return 0;
}