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;
void dfs(int,int);
int main(){
  int w,h,i,j,k,l;
  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) dfs(i,j);
      }
    }
    cout << cnt << endl;
  }
  return 0;
}
void dfs(int i,int j){
  if(ch[i][j]==1) return ;
  ch[i][j]=1;
  cnt++;
  dfs(i+1,j);
  dfs(i-1,j);
  dfs(i,j-1);
  dfs(i,j+1);
}

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

void bfs(std::vector<std::vector<char> > &map, std::pair<int, int> start) {
    int w = map.size();
    int h = map[0].size();
    int dy[4] = {0, 0, 1, -1};
    int dx[4] = {1, -1, 0, 0};
    std::queue<std::pair<int, int> > que; // coord
    int cost = 0;
    std::pair<int, int> coord = start;
    cost++;
    que.push(std::make_pair(coord.first, coord.second));
    map[coord.first][coord.second] = '#';

    while(! que.empty()) {
        coord = que.front(); 
        que.pop();
        for (int i = 0; i < 4; i++) {
            if (map[coord.first + dy[i]][coord.second + dx[i]] == '.') {
                cost++;
                que.push(std::make_pair(coord.first + dy[i], coord.second + dx[i]));
                map[coord.first + dy[i]][coord.second + dx[i]] = '#';
            }
        }
    }
    std::cout << cost << std::endl;
    return;
}

int main(void) {
    int w;
    int h;
    std::pair<int, int> start;

    while (1) {
        std::cin >> w >> h;
        if (w == 0 && h == 0) {
            break;
        }
        std::vector<std::vector<char> > map(h + 2, std::vector<char>(w + 2, '#'));
        for (int i = 1; i <= h; i++) {
            for (int j = 1; j <= w; j++) {
                std::cin >> map[i][j];
                if (map[i][j] == '@') {
                    start.first = i;
                    start.second = j;
                }
            }
        }
        bfs(map, start);
    }
    return 0;
}

C++: #include <iostream>
using namespace std;
int W,H;
int x[4]={0,0,1,-1};
int y[4]={1,-1,0,0};
int i,j;
int count=0;
char room[100][100];

int dfs(int, int);

int main(){
  while(1){
    int sy,sx;
    scanf("%d %d",&W,&H);
    if(W==0&&H==0)break;
    for(i = 0 ; i < H ; i++){
      scanf("%c",&room[i][0]);
      for(j = 0 ; j < W ; j++){
	scanf("%c",&room[i][j]);
	if(room[i][j] == '@'){
	  sy = i;
	  sx = j; 
	}
      }
    }
    printf("%d\n", dfs(sx,sy));
  }
  return 0;
}

int dfs(int ax,int ay){
  if(ax<0||ay<0)return 0;
  if(ax>=W||ay>=H)return 0;
  if(room[ay][ax]=='#')return 0;
  room[ay][ax]='#';
  int cnt=1;
  int i;
  for(i=0;i<4;i++){
    int nx=ax+x[i];
    int ny=ay+y[i];
    cnt+=dfs(nx,ny);
  }
  return cnt;
}

C++: #include<iostream>
#include <cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<utility>
#include<queue>

#define REP(i,n) for(int i = 0;i < n ;i++)
#define pa pair<int,int>
#define pq queue< pair<int,int> >
	

using namespace std;

int w,h;
char mp[22][22];
bool dp[22][22];
int dx[]={1,0,-1,0},dy[] = {0,1,0,-1};
int sx,sy;
int cou;

int main()
{
	for(;;)
	{
		cin >> w >>h;
		if(w == 0 && h == 0) break;
		cou = 1;sx = 0;sy = 0;
		REP(i,22)REP(j,22)dp[i][j] = 0;
		
		REP(i,h)REP(j,w)
		{
			cin >> mp[i][j];
			if(mp[i][j] == '@'){sx = i;sy = j;}
		}
		
		pq p;
		p.push(make_pair(sx,sy));
		dp[sx][sy] = 1;
//		cout << sx << ' '<<sy<<endl;
		while(!p.empty())
		{
			pa t = p.front();p.pop();
			REP(i,4)
			{
				int cx = t.first+dx[i],cy = t.second+dy[i];
				if(cx >= 0 && cx < h && cy >= 0 && cy < w)
				if( dp[cx][cy] == 0&& mp[cx][cy] != '#')
				{
//					cout << cx << ' ' <<cy << endl;
					p.push(make_pair(cx,cy));
					dp[cx][cy] = 1;
					cou++;
				}
			}
		}
		
			
		
		cout << cou << endl;
	}
	return 0;
}