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<string>
#include<vector>
#include<utility>

using namespace std;


//dpは行ったかどうか、vは行けるかどうか
void depth_search(int x,int y,vector< vector<int> >& dp,vector< vector<int> >& v,int w,int h){
  if(x>0){if(v[x-1][y]==1 and dp[x-1][y]!=1){dp[x-1][y]=1;depth_search(x-1,y,dp,v,w,h);}}
  if(x<h-1){if(v[x+1][y]==1 and dp[x+1][y]!=1){dp[x+1][y]=1;depth_search(x+1,y,dp,v,w,h);}}
  if(y>0){if(v[x][y-1]==1 and dp[x][y-1]!=1){dp[x][y-1]=1;depth_search(x,y-1,dp,v,w,h);}}
  if(y<w-1){if(v[x][y+1]==1 and dp[x][y+1]!=1){dp[x][y+1]=1;depth_search(x,y+1,dp,v,w,h);}}
}


void solve(int w,int h){
  vector< vector<int> > dp(h,vector<int>(w,0));
  vector< vector<int> > v(h,vector<int>(w,0));
  int x,y;
  for(int i=0;i<h;i++){
    string s;cin >>s;
    for(int j=0;j<w;j++){
      if(s[j]!='#') v[i][j]=1;
      if(s[j]=='@'){dp[i][j]=1;x=i;y=j;}
    }
  }
  depth_search(x,y,dp,v,w,h);
  int c=0;
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      if(dp[i][j]==1)c+=1;
    }
  }
  cout << c << endl;
}


int main(){
  int w,h;
  while(cin >> w >> h and w!=0){
    solve(w,h);
  }
}

C++: #include<iostream>
#include<utility>
using namespace std;

int tile[21][21]={0};
int d[21][21]={0};
int lapse=0;
int W,H;
char a;
pair<int,int> person;
int answer=0;
const int dx[]={1,0,-1,0},dy[]={0,1,0,-1};

bool valid(int x,int y){
    return (x>=0 && x<H && y>=0 && y<W && tile[x][y]==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 );
}

void dfs(int x,int y){
    ++answer;
    d[x][y]=1;
    //cout<<"visited ("<<x+1<<","<<y+1<<")\n";

    for(int i=0;i<4;++i){
        if(valid(x+dx[i],y+dy[i]) && d[x+dx[i]][y+dy[i]]==0){
            dfs(x+dx[i],y+dy[i]);
        }
    }
}

int main(){
    
    
    while(cin>>W>>H && W!=0){
        for(int i=0;i<H;++i){
            for(int j=0;j<W;++j){
                cin>>a;
                if(a=='.'){
                    tile[i][j]=1;
                }
                else if(a=='@'){
                    person.first=i;
                    person.second=j;
                }
            }
        }

        dfs(person.first,person.second);

        cout<<answer<<"\n";
        answer=0;
        Fill(tile,0);
        Fill(d,0);
    }
}

C++: 
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <stack>
#include <cctype>
#include <complex>
#include <vector>
#include <algorithm>
#include <cassert>

using namespace std;


int ans = 0;
void dfs(vector<vector<int> > &a, int x, int y){
	ans++;
	a[x][y] = 0;
	if(a[x-1][y]) dfs(a, x-1, y);
	if(a[x][y-1]) dfs(a, x, y-1);
	if(a[x+1][y]) dfs(a, x+1, y);
	if(a[x][y+1]) dfs(a, x, y+1);
//	return res;		//テ」ツ?ゥテ」ツ??」ツつ?」ツつ凝」ツつ禿」ツ??」ツつ催」ツ??」ツ??、ツサツ甘・ツコツヲ
}

bool solve(){

	while(1){
		ans = 0;
		
		int w,h;
		cin>> w>> h;
		if(w==0 && h==0) break;
		
		vector<vector<int> > a(h+2, vector<int> (w+2,0));
		complex<int> p;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				char in;
				cin>> in;
				if(in == '#') a[i+1][j+1] = 0;
				if(in == '.') a[i+1][j+1] = 1;
				if(in == '@'){
					a[i+1][j+1] = 2;
					p.real() = i+1;
					p.imag() = j+1;
				}
			}
		}
/*		for(int i=0;i<h+2;i++){
			for(int j=0;j<w+2;j++){
				cout<< a[i][j];
			}
			cout<< endl;
		}
*/		
		dfs(a, p.real(), p.imag());
		
		cout<< ans<< endl;
	}
	
	
	return true;
}

int main(){
	cout.setf(ios::fixed);
	cout.precision(10);
	solve();

	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()

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

int const dx[] = {0,1,0,-1};
int const dy[] = {-1,0,1,0};
int go(int sx, int sy) {
  int ans = 0;
  REP(i,4) {
    int xx = sx+dx[i];
    int yy = sy+dy[i];
    if (xx<0||xx>=w||yy<0||yy>=h) continue;
    if (ba[xx][yy] == '.') {
      ba[xx][yy] = '#';
      ans++;
      ans += go(xx,yy);
    }
  }
  return ans;
}

int main() {
  while(cin >> w >> h, w+h) {
    int sx, sy;
    REP(y,h) {
      REP(x,w) {
        cin >> ba[x][y];
        if (ba[x][y]=='@') {
          sx = x;
          sy = y;
        }
      }
    }
    cout << go(sx,sy)+1 << endl;
    
  }
}