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;

char table[21][21];
bool reachable;
int cnt;
int w, h;

void dfs(int x, int y) {
    if (table[y][x] == '@')
        reachable = true;
    table[y][x] = '#';
    for (int dy = -1; dy <= 1; dy++) {
        for (int dx = -1; dx <= 1; dx++) {
            if (dx*dy != 0 || x+dx < 0 || x+dx >= w || y+dy < 0 || y+dy >= h)
                continue;
            if (table[y+dy][x+dx] != '#') {
                cnt++;
                dfs(x+dx, y+dy);
            }
        }
    }
}

int main()
{
    start:
    while (cin >> w >> h, w|h) {
        // input
        for (int i = 0; i < h; i++) {
            cin >> table[i];
        }
        // dfs
        for (int j = 0; j < h; j++) {
            for (int i = 0; i < w; i++) {
                if (table[j][i] != '#') {
                    reachable = false;
                    cnt = 1;
                    dfs(i, j);
                    if (reachable) {
                        cout << cnt << endl;
                        goto start;
                    }
                }
            }
        }
    }

    return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;
const int dx[] = { -1, 0, 1, 0 };
const int dy[] = { 0, -1, 0, 1 };

int main(){
	int W, H;
	while( cin >> W >> H, W, H ){
		char room[20][20];
		queue< pair<int, int> > xy;
		for( int i = 0; i < H; i++ ){
			for( int j = 0; j < W; j++ ){
				cin >> room[i][j];
				if( room[i][j] == '@' ){
					xy.push(pair<int, int>(i, j));
					room[i][j] = '#';
				}
			}
		}
		int sum = 1;
		while( !xy.empty() ){
			for( int i = 0; i < 4; i++ ){
				int x = xy.front().second + dx[i];
				int y = xy.front().first + dy[i];
				if( 0 <= x && x < W && 0 <= y && y < H && room[y][x] == '.' ){
					room[y][x] = '*';
					xy.push(pair<int, int>(y, x));
					sum++;
				}
			}
			xy.pop();
		}
		cout << sum << endl;
	}
}

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

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

typedef pair<int,int> P;

int bfs(int, int, int, int, char **);

int main()
{
	int w, h, sx, sy;
	char **map;

	while( cin >> w >> h && w > 0)
	{
		fflush(stdin);

		map = new char *[h];
		for(int f1 = 0; f1 < h; f1++)
		{
			map[f1] = new char [w+1];
			scanf("%s",map[f1]);
			for(int f2 = 0; f2 < w; f2++)
			{
				if(map[f1][f2] == '@')
				{
					sx = f2;
					sy = f1;
				}
			}
		}
		cout << bfs(w, h, sx, sy, map) << endl;
	}

	return 0;
}

int bfs(int w, int h, int sx, int sy, char **map)
{
	int px, py, cnt = 1;

	queue<P> que;
	que.push(P(sy,sx));
	while(!que.empty())
	{
		P p = que.front(); que.pop();
		for(int f1 = 0; f1 < 4; f1++)
		{
			py = p.first + dy[f1];
			px = p.second + dx[f1];
			if(px >= 0 && px < w && py >= 0 && py < h && map[py][px] == '.')
			{
				que.push(P(py,px));
				map[py][px] = '#';
				cnt++;
			}
		}
	}
	return cnt;
}

C++: #include<cstdio>

bool done[21][21];
int W,H;
char s[21][21];
bool map[21][21];

void init(){
  for(int i=0;i<21;i++){
    for(int j=0;j<21;j++){
      done[i][j]=false;
      map[i][j]=false;
      s[i][j]=0;
    }}
}

void solve(int h,int w){
  if(s[h][w]=='.' || s[h][w]=='@'){
    map[h][w]=true;
    done[h][w]=true;
    if(h-1 >=0 && done[h-1][w]==false) solve(h-1,w);
    if(h+1 < H && done[h+1][w]==false) solve(h+1,w);
    if(w-1 >=0 && done[h][w-1]==false) solve(h,w-1);
    if(w+1 <W && done[h][w+1]==false) solve(h,w+1);
  }
  else{
    map[h][w]=false;
    done[h][w]=true;
  }

  return;

}
int main(){
  while(1){
    init();
    scanf("%d%d",&W,&H);
    if(W==0 && H==0) break;
    for(int i=0;i<H;i++)
      scanf("%s",&s[i]);
    int sw,sh;
    for(int i=0;i<H;i++){
      for(int j=0;j<W;j++){
        if(s[i][j]=='@'){
          sw=j;
          sh=i;
        }}}

    solve(sh,sw);


    int ans=0;

    for(int i=0;i<H;i++){
      for(int j=0;j<W;j++){
        if(map[i][j]==true)
          ans++;
      }}

    printf("%d\n",ans);
  }
  return 0;

}