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.h>
#include <queue>

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

char t[20][20];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int ans,h,w;
queue<P> q;

int bfs(int x, int y){

  q.push( P(x, y) );
  
  while( !q.empty() ){
    P now = q.front();
    q.pop();
    for( int i = 0 ; i < 4 ; i++ ){
      int nx = now.first + dx[i];
      int ny = now.second + dy[i];
      if( 0 <= nx && nx < w && 0 <= ny && ny <= h 
	  && t[ny][nx] == '.' ){
	t[ny][nx] = '#';
	q.push( P(nx, ny) );
	ans++;
      }
    }
  }
  return ans;
}

int main(void){

  int x,y;
  while( cin >> w >> h, h + w ){

    memset( t, 0, sizeof(t) );
    ans = 1;

    for( int i = 0 ; i < h ; i++ ){
      for( int j = 0 ; j < w ; j++ ){
	cin >> t[i][j];
	if( t[i][j] == '@' ){
	  x = j;
	  y = i;
	}
      }
    }
    cout << bfs(x,y) << endl;
  }
  return 0;
}

C++: #include<stdio.h>
#include<string.h>

char map[20][20]={0};
int number=1;
int h,w;

void debug(void) {
	for(int i=0;i<h;i++) { for(int j=0;j<w;j++) putchar(map[i][j]); putchar('\n'); }
}

void trace(int x,int y) {
	if(x-1 >= 0 && map[y][x-1]=='.') {
		map[y][x-1] = '@';
		trace(x-1,y);

		number++;
	}

	if(x+1 < w && map[y][x+1]=='.') {
		map[y][x+1] = '@';
		trace(x+1,y);
		number++;
	}
	if(y-1 >= 0 && map[y-1][x]=='.') {
		map[y-1][x] = '@';
		trace(x,y-1);
		number++;
	}

	if(y+1 < h && map[y+1][x]=='.') {
		map[y+1][x] = '@';
		trace(x,y+1);
		number++;
	}
}

int main(void) {
	char buf[21];

	int x,y;


	for(;;) {

		number=1;
		scanf("%d %d\n",&w,&h);

		if(w==0 || h==0) break;

		for(int i=0;i<h;i++) {
			fgets(buf,1024,stdin);

			memcpy(&map[i],buf,w);
		}


		for(int j=0;j<h;j++) {
			for(int i=0;i<w;i++) {
				if(map[j][i] == '@'){
					x=i;y=j;
				}
			}
		}


		trace(x,y);

		printf("%d\n",number);

	}

	//debug();

	return 0;
}

C++: // ??±???????????¢?´¢(DFS:Depth First Search)
// ICPC Domestic 2004
//
#include <iostream>
using namespace std;

#define LIM 20
int w,h,sx,sy,ans;
bool field[LIM][LIM];
char buff[LIM];
bool flg=true;

void input(void);
void dump(void);
void dfs(int, int);
void solve();


int main(void)
{
    while(true){
        input();
        if(!flg)break; // ????????????????????????????¨??????§????????°??????????????????
        //dump();
        dfs(sx,sy);
        solve();
    }
    return 0;
}

void input(void){
    cin >> w >> h;
    if(w==0 && h==0){
        flg=false;
        return;
    }
    for(int i=0;i<h;++i){
        cin >> buff;
        for(int j=0;j<w;++j){
            if(buff[j]=='#')field[i][j]=false; // ????????????
            else field[i][j]=true; // ?????????
            if(buff[j]=='@'){
                sy=i;
                sx=j;
            }
        }
    }
    ans=0; //?§£????????????????????????????????¨????????????
    return;
}

void dump(void){
    cout << "sx:" << sx << " sy:" << sy << endl;
    for(int i=0;i<h;++i){
        for(int j=0;j<w;++j){
            cout << field[i][j];
        }
        cout << endl;
    }
}

void dfs(int x,int y){
    if(!field[y][x] || x<0 || y<0 || x>=w || y>=h) //????????¢?????????????????´????????´???
        return;
    else{
        field[y][x]=false;
        ++ans;
        dfs(x-1,y); //???
        dfs(x,y-1); //???
        dfs(x+1,y); //???
        dfs(x,y+1); //???
    }
    return;
}

void solve(){
    cout << ans << endl;
}

C++: #include <bits/stdc++.h>

using namespace std;

#define all(c) (c).begin(), (c).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

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

int W, H;
string s[30];
using P = pair<int, int>;
void solve() {
    P init;
    rep(i, H) rep(j, W) if (s[i][j] == '@') init = P(i, j);
    set<P> visited;
    queue<P> que;
    que.push(init);
    visited.insert(init);

    while (que.size()) {
        auto p = que.front();
        que.pop();
        rep(k, 4) {
            auto np = p;
            np.first += dy[k];
            np.second += dx[k];
            if (np.first < 0 || np.second < 0) continue;
            if (H <= np.first || W <= np.second) continue;
            if (visited.count(np)) continue;
            if (s[np.first][np.second] == '#') continue;

            que.push(np);
            visited.insert(np);
        }
    }
    cout << visited.size() << endl;
}
int main() {
    while (cin >> W >> H, W) {
        rep(i, H) cin >> s[i];
        solve();
    }

    return 0;
}