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>
using namespace std;
int ch[23][23],cnt;
int dfs(int,int);
int main(){
  int w,h,i,j,k,l,result;
  string in[21];
  while(1){
    cnt=0;
    cin >> w >> h;
    if(w==0&&h==0) break;
    for(i=0;i<h;i++){
      cin >> in[i];
    }
    for(i=0;i<h+2;i++){
      for(j=0;j<w+2;j++){
	ch[i][j]=1;
      }
    }
    for(i=0,k=1;k<h+1;i++,k++){
      for(j=0,l=1;l<w+1;l++,j++){
	if(in[i][j]=='.') ch[k][l]=0;
	else if(in[i][j]=='#') ch[k][l]=1;
	else ch[k][l]=2;
      }
    }
    for(i=1;i<h;i++){
      for(j=1;j<w;j++){
	if(ch[i][j]==2) result=dfs(i,j);
      }
    }
    cout << cnt << endl;
  }
  return 0;
}
int dfs(int i,int j){
  if(ch[i][j]==1) return 0;
  ch[i][j]=1;
  cnt++;
  dfs(i+1,j);
  dfs(i-1,j);
  dfs(i,j-1);
  dfs(i,j+1);
  return cnt;
}

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

using namespace std;

int w;
int h;
string map[20];
bool come[20][20];

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

int dfs(int x, int y)
{
    if(x < 0 || y < 0 || w <= x || h <= y)return 0;
    if(come[y][x])return 0;
    if(map[y][x] == '#')return 0;

    come[y][x] = true;

    int ret = 1;
    for(int i = 0; i < 4;i++)
    {
        ret += dfs(x + dx[i], y + dy[i]);
    }
    return ret;
}

int main()
{
    while( cin >> w >> h, (w | h) )
    {
        
        string gomi;
        getline(cin, gomi);//??°?????????

        int px, py;

        //input
        for (int i = 0; i < h; i++)
        {
            getline(cin, map[i]);
            for(int j = 0; j < w; j++)
            {
                come[i][j] = false;
                if(map[i][j] == '@')
                {
                    px = j;
                    py = i;
                }
            }
        }
        cout << dfs(px, py) << endl;
    }
    return 0;
}

C++: #include<cstdio>
using namespace std;
int w,h;
char m[21][21];

int dfs(int y,int x){
  if(x < 0 || x >= w)return 0;
  if(y < 0 || y >= h)return 0;
  if(m[y][x] == '#') return 0;
  int ret = 1;
  m[y][x] = '#';
  ret += dfs(y+1,x);
  ret += dfs(y-1,x);
  ret += dfs(y,x+1);
  ret += dfs(y,x-1);
  return ret;
}

int main(){
  int sx,sy;
  while(true){
  scanf("%d %d",&w,&h);
  if(w == 0 && h == 0)break;
  for(int i = 0;i < h;i++){
    scanf("%s",m[i]);
    for(int j = 0;j < w;j++){
      if(m[i][j] == '@'){
        sx=j;sy=i;
      }
    }
  }
  printf("%d\n",dfs(sy,sx));
  }
}

C++: #include "bits/stdc++.h"

using namespace std;

#define rep(a,b) for(int (a)=0;(a)<(b);(a)++)

int main(){
    int w,h;
    while(cin>>w>>h){
        if(w==0 && h==0)break;
        int sx,sy;
        int sum=0;
        int dy[]={-1,0,1,0};
        int dx[]={0,-1,0,1};
    
    
        string data[30]={};
    
        for(int i=0;i<h;i++){
            cin>>data[i];
        }
    
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(data[i][j]=='@'){
                    sx=j;sy=i;
                    break;
                }
            }
        }
    
        bool visited[30][30]={};
        rep(i,30)rep(j,30)visited[i][j]=false;
        
    
        queue<pair<int,int> > que;
    
        que.push( pair<int,int>(sy,sx) );
        visited[sy][sx]=true;
    
        while(que.size()){
            int qff=que.front().first,qfs=que.front().second;
            sum++;
            rep(i,4){
                if( qff+dy[i]<h && qff+dy[i]>=0 && qfs+dx[i]<w && qfs>=0 && visited[ qff+dy[i] ][ qfs+dx[i] ]==false && data[ qff+dy[i] ][ qfs+dx[i] ]!='#' ){
                    que.push( pair<int,int>(qff+dy[i] , qfs+dx[i]) );
                    visited[ qff+dy[i] ][ qfs+dx[i] ]=true;
                }
            }
            
            que.pop();
        }
        cout<<sum<<endl;
    }
    
    return 0;
}

C++: #include<iostream>
using namespace std;
#define MAX (256)

char field[MAX][MAX];
int W, H;
int ans = 0;
int sx, sy;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

void dfs(int x, int y){
	if(0 <= x && x < H && 0 <= y && y < W && field[x][y] == '.'){
		ans++;
		field[x][y] = '#';

		for(int i=0; i < 4; i++){
			int nx = x + dx[i]; int ny = y + dy[i];
			dfs(nx, ny);
		}
	}
}

int main(){
	while(cin >> W >> H, W || H){
		ans = 0;
		for(int i=0; i < H; i++){
			for(int j=0; j < W; j++){
				cin >> field[i][j];
				if(field[i][j] == '@'){
					sx = i;
					sy = j;
					field[i][j] = '.';
				}
			}
		}
		dfs(sx, sy);

		cout << ans << endl;
	}

}