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 <bits/stdc++.h>
using namespace std;

#define rep(i,N) for(int i=0;i<(int)(N);i++)
#define REP(i,N,k) for(int i=k;i<(int)(N);i++)

#define vi vector<int>
#define pb push_back

#define pii pair<int,int>
#define fi first
#define se second
int main(){


    ifstream in("b_in.txt");
    cin.rdbuf(in.rdbuf());

    int w,h;
    while(cin >> w >> h,w){
        int sx,sy;
        string s[30];
        rep(i,h) cin >> s[i];
        rep(y,h)rep(x,w) if(s[y][x]=='@') s[y][x]='.',sx=x,sy=y;

        queue<pii> que;
        que.push(pii(sx,sy));
        bool u[30][30] = {};
        u[sy][sx] = true;
        int cnt = 1;
        while(!que.empty()){
            int x,y;
            tie(x,y) = que.front();
            que.pop();
            rep(i,4){
                const int dx[] = {0,1,0,-1,0};
                int nx = x + dx[i];
                int ny = y + dx[i+1];
                if(nx < 0 || w <= nx || ny < 0 || h <= ny) continue;
                if(s[ny][nx] == '#') continue;
                if(u[ny][nx]) continue;
                u[ny][nx] = true;
                cnt += 1;
                que.push(pii(nx,ny));
            }
        }
        cout << cnt << endl;
    }

    return 0;
}

C++: #include <iostream>
#define RED '#'
#define BLACK '.'
using namespace std;

int w,h;
char tiles[20][20];

int rc(int x,int y){
  if( x<0 || x>=h )
    return 0;
  if( y<0 || y>=w )
    return 0;
  if( tiles[x][y]==RED )
    return 0;
  tiles[x][y]=RED;

  int rtn=1;
  rtn += rc( x+1,y  );
  rtn += rc( x-1,y  );
  rtn += rc( x,y+1  );
  rtn += rc( x,y-1  );

  return rtn;
}
void pr(){
  for( int i=0;i<h;i++){
    for(int j=0;j<w;j++ )
      cout << tiles[i][j];
    cout << endl;
  }
}

int main(){
  int x,y;

  while( true ){
    cin >> w >> h;
    if( !w&&!h ) break;

    for( int i=0;i<h;i++ )
      for( int j=0;j<w;j++ ){
	cin >> tiles[i][j];
	if( tiles[i][j]=='@' ){
	  tiles[i][j]=BLACK;
	  x=i; y=j;
	}
      }
    //   pr();
    cout << rc( x,y ) << endl;
    //pr();
    //break;
  }

  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define fi first
#define se second
#define pb push_back
#define rep(i, s, n) for (int i = s; i < n; i++)
#define rrep(i, s, n) for (int i = (n)-1; i >= (s); i--)
#define all(a) a.begin(),a.end()
typedef pair<int,int>pint;
typedef vector<int>vint;
typedef vector<pint>vpint;
const long long MOD = 1000000007, INF = 1e17;
 
#define endl '\n'
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0)
 
template<class T>inline bool chmax(T& a,T b){if(a<b){a=b;return true;}return false;}
template<class T>inline bool chmin(T& a,T b){if(a>b){a=b;return true;}return false;}


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

signed main()
{
	IOS();
	int H,W;
	vector<int>ans;
	while(1){
		cin>>W>>H;
		if(H==0&&W==0)break;
		string s[H];
		rep(i,0,H)cin>>s[i];
		queue<pair<int,int>>que;
		rep(i,0,H){
			rep(j,0,W){
				if(s[i][j]=='@')que.push({i,j});
			}
		}
		vector<vector<int>>visit(H,vector<int>(W,0));
		while(!que.empty()){
			int i = que.front().fi;
			int j = que.front().se;
			que.pop();
			if(visit[i][j])continue;
			visit[i][j] = true;
			rep(k,0,4){
				int ni = i + dx[k];
				int nj = j + dy[k];
				if(ni<0||nj<0||H<=ni||W<=nj)continue;
				if(s[ni][nj]=='#')continue;
				if(visit[ni][nj])continue;
				que.push({ni,nj});
			}
		}
		int res = 0;
		rep(i,0,H)rep(j,0,W)if(visit[i][j])res++;
		ans.push_back(res);
	}
	for(auto e:ans){
		cout<<e<<endl;
	}
}

C++: #include <iostream>
#include <cassert>
#define REP(i,l,n) for(int i=l;i<n;++i)
#define rep(i,n) REP(i,0,n)
#define MAX 21
int ans,baseX,baseY;
int w,h;
char tile[MAX][MAX];
void seek(int x,int y);
using namespace std;
int main() {
  while(1){
    cin >> w;
    cin >> h;
    if(w==0&&h==0) return 0;
    rep(i,h){
      rep(j,w){
        cin >> tile[i][j];
        if(tile[i][j] == '@'){
          baseX = i;
          baseY = j;
        }
      }
      tile[i][w] = '#';
    }
    rep(i,w) tile[h][i] = '#';
    ans = 1;
    seek(baseX,baseY);
    cout << ans << endl;
  }
  return 0;
}

void seek(int x,int y){
  if(tile[x+1][y] == '.'){
    tile[x+1][y] = '#';
    ans++;
    seek(x+1,y);
  }
  if(tile[x][y+1] == '.'){
    tile[x][y+1] = '#';
    ans++;
    seek(x,y+1);
  }
  if(tile[x-1][y] == '.'){
    tile[x-1][y] = '#';
    ans++;
    seek(x-1,y);
  }
  if(tile[x][y-1] == '.'){
    tile[x][y-1] = '#';
    ans++;
    seek(x,y-1);
  }
}