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 <cstdio>
#include <set>
#include <vector>
using namespace std;

int main(){
  int H, W;
  while(scanf("%d%d", &W, &H)){
    if ((H == 0) && (W == 0)){
      break;
    }
    vector<vector<char> > m(H, vector<char>(W, ' '));
    pair<int,int> start(0,0);
    for (int i = 0; i < H; i++){
      for (int j = 0; j < W; j++){
        scanf(" %c", &(m[i][j]));
        if (m[i][j] == '@'){
          start.first = j;
          start.second = i;
	}
      }
    }
    set<pair<int,int> > q;
    set<pair<int,int> > already;
    q.insert(start);
    while(!q.empty()){
      pair<int,int> next = *(q.begin());
      q.erase(q.begin());
      if (already.find(next) == already.end()){
        already.insert(next);
        if ((next.first > 0) && (m[next.second][next.first-1] == '.')){
          q.insert(pair<int,int>(next.first-1, next.second));
	}
        if ((next.first < W-1) && (m[next.second][next.first+1] == '.')){
          q.insert(pair<int,int>(next.first+1, next.second));
	}
        if ((next.second > 0) && (m[next.second-1][next.first] == '.')){
          q.insert(pair<int,int>(next.first, next.second-1));
	}
        if ((next.second < H-1) && (m[next.second+1][next.first] == '.')){
          q.insert(pair<int,int>(next.first, next.second+1));
	}
      }
    }
    printf("%d\n", (int)already.size());
  }
  return 0;
}

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

using namespace std;
vector<int> ans;
typedef pair <int, int> P;

int  main(){
  int W,H;
  cin >> W >> H;
  int tate[4] = {0,1,0,-1};
  int yoko[4] = {1,0,-1,0};
  while(W){
    int count = 0;
    queue < P > zahyou;
    vector< vector<int> > room(H, vector<int>(W));
    for(int i = 0 ; i < H ; i++){
      for(int j = 0 ; j < W ; j++){
        char a;
        cin >> a;
        if(a == '.')room[i][j] = 1;
        else if(a == '@'){
          zahyou.push( P (i,j) );
          room[i][j] = 1;
        }
        else room[i][j] = 0;
      }
    }
    while(zahyou.size()){
      pair<int, int> now = zahyou.front();
      zahyou.pop();
      if(room[now.first][now.second] == 1){
        count++;
        room[now.first][now.second] = 0;
        for(int i = 0; i < 4 ; i++){
          if(now.first+tate[i]>=0 && now.first+tate[i] < H && now.second+yoko[i] >= 0 && now.second+yoko[i] < W && room[now.first+tate[i]][now.second+yoko[i]] == 1)
            zahyou.push( P (now.first+tate[i], now.second+yoko[i]));
        }
      }
    }
    ans.push_back(count);
    cin >> W >> H;
  }
  for(int i = 0 ; i < ans.size(); i++)
    cout << ans[i] << endl;
}

C++: #include <iostream>
#include <queue>
#include <array>

const int kSize = 20 + 2;

std::array<int, 4> ways = {-1, -kSize, +1, +kSize};

inline int xy2pos(const int x, const int y){
    return x + y * kSize;
}

std::array<bool, kSize*kSize> Bfs(const std::array<bool, kSize*kSize> &tiles, int pos){
    std::array<bool, kSize*kSize> arrived;
    std::queue<int> q;
    q.push(pos);
    std::fill(arrived.begin(), arrived.end(), false);
    while(!q.empty()){
        pos = q.front();
        q.pop();
        arrived[pos] = true;
        for(int way : ways)
            if(tiles.at(pos + way) && !arrived.at(pos + way))
                q.push(pos + way);
    }
    return arrived;
}

int main(){
    int w,h;
    std::array<bool,kSize*kSize> tiles; ///< true if black
    while((std::cin >> w >> h) && w != 0){
        std::fill(tiles.begin(), tiles.end(), false);
        int pos;
        for(int y=1; y<=h; ++y){
            for(int x=1; x<=w; ++x){
                char c;
                std::cin >> c;
                tiles[xy2pos(x,y)] = (c == '.');
                if(c == '@'){
                    pos = xy2pos(x,y);
                    tiles[pos] = true;
                }
            }
        }
        auto arrived = Bfs(tiles, pos);
        int sum = 0;
        for(bool v : arrived)
            sum += (v) ? 1 : 0;
        std::cout << sum << std::endl;
    }
}

C++: #include<iostream>
using namespace std;
int main(){
  while(1){
    int dfs(int x,int y,int count);
    
    int w,h;
    int dx[4]={0,-1,0,1};
    int dy[4]={-1,0,1,0};
    int sum=0;
    char maps[21][21];
    
    int main(){
      int sx,sy;
      cin >> h >> w;
      if(h==0 && w==0) break; 
      for(int i=0;i<h;i++){
	for(int j=0;j<w;j++){
	  cin >> maps[i][j];
	  if(maps[i][j]=='@'){
	    sy=i;
	    sx=j;
	  }
	}
      }
      int ans = dfs(sx,sy,0);
      cout << ans << endl;
    }
    
    int dfs(int x,int y,int count){
      int nx,ny;
      for(int i=0;i<4;i++){
	nx=x+dx[i];
	ny=y+dy[i];
	maps[y][x]='#';
	if(0<=nx && nx<w && 0<=ny && ny<h && maps[ny][nx]!='#'){
	  sum++;
	  dfs(nx,ny,count+1);
	}
      }
      return sum;
    }
  }
  return(0);
}