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;

void search(int a, int b);

char wh[22][22];
bool judge[22][22];

int ans = 0;

int main(void){

while(1) {
	int x;
	int y;
	cin >> x >> y;
	if(x == 0 && y == 0) return 0;

	int i;
	int j;

	// 初期化
	for(i = 0; i < 22; i++){
		for(j = 0; j < 22; j++){
			wh[j][i] = '#';
			judge[j][i] = false;
		}
	}

// 入力
	int xs, ys;
	for(i = 1; i < y+1; i++){
		for(j = 1; j < x+1; j++){
			cin >> wh[j][i];
			if(wh[j][i] == '@') {
				xs = j;
			 	ys = i;
			}
		}
	}


	// for(i = 0; i < 22; i++){
	// 	for(j = 0; j < 22; j++){
	// 		printf("%c",wh[j][i]);
	// 	}
	// 	printf("\n");
	// }

	search(xs, ys);

	cout << ans << endl;
	ans = 0;
}

	return 0;
}

void search(int a,int b) {
	if(wh[a][b] == '#' || judge[a][b] == true) return;

	ans++;
	judge[a][b] = true;
	// 左
	search(a-1,b);
	// 右
	search(a+1,b);
	// 上
	search(a,b+1);
	// 下
	search(a,b-1);
}

C++: #include <iostream>
using namespace std;
int a=0;
int box[20][20];
int  kazoe(int p,int P,int H,int W){
  int i,I;
  a++;
  for(i=0;i<H;i++){
    for(I=0;I<W;I++){
      cout<<box[i][I];
    }
    cout<<endl;
  }
  box[p][P]=0;
  if(box[p][P+1]==1) kazoe(p,P+1,H,W);
  if(box[p][P-1]==1) kazoe(p,P-1,H,W);
  if(box[p-1][P]==1) kazoe(p-1,P,H,W);
  if(box[p+1][P]==1) kazoe(p+1,P,H,W);
  else {
  return a;
  }
}
int main(){
  int i,I,H,W,p,P;
  
  char v;
  while(1){
  cin>>W>>H;
  if(W==0&&H==0) break;
  for(i=0;i<H;i++){
    for(I=0;I<W;I++){
      cin>>v;
      if(v=='.') box[i][I]=1;
      if(v=='#') box[i][I]=2;
      if(v=='@') {
	box[i][I]=0;
	p=i;
	P=I;
      }
    }
  }
  cout<<kazoe(p,P,H,W)<<endl;
  a=0;
  for(i=0;i<H;i++){
    for(I=0;I<W;I++){
      box[i][I]=0;
    }
  }
  
 
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int ans;
bool dfs(int y,int x,vector<vector<bool> >& canGo){
canGo[x][y]=false;
ans+=1;
if(canGo[y+1][x])dfs(y+1,x,canGo);
if(canGo[y-1][x])dfs(y-1,x,canGo);
if(canGo[y][x+1])dfs(y,x+1,canGo);
if(canGo[y][x-1])dfs(y,x-1,canGo);
return false;
}

int main(void){
int h,w;
int i,j,k;
int x,y;
string s;
vector<vector<bool> >canGo;

while(true){
cin>>h>>w;
if(h==0)return 0;
canGo=vector<vector<bool> >(h+2,vector<bool>(w+2,false));
ans=0;
for(i=0;i<h;i++){
	cin>>s;
	for(j=0;j<w;j++){
		if(s[j]=='.')canGo[i+1][j+1]=true;
		else if(s[j]=='@'){y=i+1;x=j+1;canGo[i+1][j+1]=true;}
	}
}
dfs(y,x,canGo);
cout<<ans<<endl;
}
return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <map>
#include <set>
#include <cstdio>
#include <cmath>
#define rep(i,l,n) for(lint i=l;i<n;i++)
#define rer(i,l,n) for(lint i=l;i<=n;i++)
#define all(a) a.begin(),a.end()
#define o(a) cout<<a<<endl
#define pb(a) push_back(a)
#define mk(a,b) make_pair(a,b)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef vector<int> vi;
typedef vector<lint> vli;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

int h, w, ans, sy, sx;
char s[21][21] = {};
int d[21][21] = {};
int dy[4] = { 1, -1, 0, 0 };
int dx[4] = { 0, 0, 1, -1 };

void dfs(int y, int x){
	if (y < 0 || h <= y || x < 0 || w <= x || d[y][x]||s[y][x]=='#') return;
	ans++;
	d[y][x] = 1;
	rep(i, 0, 4){
		int ny = y + dy[i], nx = x + dx[i];
		dfs(ny, nx);
	}
	return;
}

int main(){
	while (1){
		cin >> w >> h;
		rep(i, 0, 21){
			rep(j, 0, 21){
				d[i][j] = 0;
			}
		}
		if (h == 0 && w == 0) break;
		rep(i, 0, h){
			rep(j, 0, w){
				cin >> s[i][j];
				if (s[i][j] == '@'){
					sy = i;
					sx = j;
				}
			}
		}
		ans = 0;
		dfs(sy, sx);
		o(ans);
	}

}