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>
#include<string>
using namespace std;
int main(){
	bool dp[20][20];
	char r[20][20];
	int w,h;
	while(true){
	cin>>w>>h;
	if(w==0&&h==0)
		break;
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			cin>>r[i][j];
			dp[i][j]=false;
			if(r[i][j]=='@')
				dp[i][j]=true;
		}
	}
	int count=1,cop=1;
	while(true){
	cop=count;
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			bool t=false;
			if(i<h-1){
			if(dp[i+1][j]==true&&r[i][j]=='.')
				t=true;
			}
			if(i>0){
			if(dp[i-1][j]==true&&r[i][j]=='.')
				t=true;
			}
			if(j<w-1){
			if(dp[i][j+1]==true&&r[i][j]=='.')
				t=true;
			}
			if(j>0){
			if(dp[i][j-1]==true&&r[i][j]=='.')
				t=true;
			}
			if(t==true&&dp[i][j]==false){
				count++;
				dp[i][j]=true;
			}
		}
	}
	if(cop==count)
		break;
	}
	cout<<count<<endl;
	}
    return 0;
}

C++: #include <cstdio>

using namespace std;

int W, H;
char M[22][22];

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

int ans;

void dfs(int y, int x) {
    if (M[y][x] == '#' || M[y][x] == '@') { return; }

    ans++;
    M[y][x] = '@';
    for (int i = 0; i < 4; i++) {
        dfs(y + dy[i], x + dx[i]);
    }
}

int solve() {
    int sx, sy;
    for (sy = 1;; sy++) {
        for (sx = 1; sx <= W; sx++) {
            if (M[sy][sx] == '@') { goto found; }
        }
    }
found:

    ans = 0;
    M[sy][sx] = '.';
    dfs(sy, sx);
    return ans;
}

int main() {
    for (;;) {
        scanf("%d%d ", &W, &H);

        if (!W && !H) { break; }

        for (int i = 0; i < 22; i++) {
            for (int j = 0; j < 22; j++) {
                M[i][j] = '#';
            }
        }
        for (int i = 1; i <= H; i++) {
            for (int j = 1; j <= W; j++) {
                scanf("%c", &M[i][j]);
            }
            char d;
            scanf("%c", &d);
        }
        printf("%d\n", solve());
    }
}

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

using namespace std;

int width, depth;
int s_w, s_d;
bool is_visit[20 + 2][20 + 2];
char field[20 + 2][20 + 2];
int t_x[4] = {1, 0, -1, 0};
int t_y[4] = {0, -1, 0, 1};


int search(int w, int d)
{
  //  printf("(%d, %d)\n", w, d);
  if (is_visit[d][w]) return 0;
  if (field[d][w] == '#') return 0;
  is_visit[d][w] = true;
  int sum = 0;
  for (int i = 0; i < 4; i++) {
    sum += search(w + t_x[i], d + t_y[i]);
  }
  return sum + 1;
}

int main()
{
  while (scanf("%d %d ", &width, &depth) == 2) {
    if (width == 0 && depth == 0) {
      break;
    }
    for (int i = 0; i < 22; i++) {
      fill(is_visit[i], &is_visit[i][22], false);
      fill(field[i], &field[i][22], '#');
    }
    for (int i = 1; i <= depth; i++) {
      for (int j = 1; j <= width; j++) {
	scanf("%c ", &field[i][j]);
	if (field[i][j] == '@') {
	  s_w = j, s_d = i;
	}
	//	printf("%c", field[i][j]);
      }
      //      printf("\n");
    }
    
    printf("%d\n", search(s_w, s_d));
  }
}