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 <queue>
using namespace std;

bool debug=true;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
char d[21][21];

void init(){
  for(int i=0;i<21;i++)for(int j=0;j<21;j++)d[i][j]='#';
}

int dfs(int sx,int sy){
  int cnt=0;
  queue<int>que;
  que.push(sx);que.push(sy);
  while(que.size()){
    int x=que.front();que.pop();
    int y=que.front();que.pop();
    for(int i=0;i<4;i++){
      if(d[y+dy[i]][x+dx[i]]=='.'){
	que.push(x+dx[i]);que.push(y+dy[i]);
	d[y+dy[i]][x+dx[i]]='#';
	cnt++;
      }
    }
  }
  return cnt+1;
}

int main(){
  int w,h;
  
  while(1){
    cin>>w>>h;
    if(w==0&&h==0)break;
    init();
    int x,y;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	cin>>d[i][j];
	if(d[i][j]=='@'){
	  x=j;y=i;
	}
      }
    }
    cout<<dfs(x,y)<<endl;
  }
  return 0;
}

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

using namespace std;

typedef complex<int> P;

int main() {
	int W, H;
	while ( cin >> W >> H, W || H ) {
		int m[20][20], sx, sy;
		for (int y = 0; y < H; ++y) {
			string str; cin >> str;
			for (int x = 0; x < W; ++x) {
				m[y][x] = str[x] == '#' ? 0 : 1;
				if (str[x] == '@') {
					sx = x, sy = y;
				}
			}
		}
		int ans = 0;
		queue<P> Q; Q.push( P(sx, sy) );
		while ( !Q.empty() ) {
			P p = Q.front(); Q.pop();
			int y = p.imag(), x = p.real();
			if (y < 0 || y >= H || x < 0 || x >= W) continue;
			if ( m[y][x] == 0 ) continue;
			m[y][x] = 0;
			++ans;
			
			Q.push( P(x+1, y) );
			Q.push( P(x-1, y) );
			Q.push( P(x, y-1) );
			Q.push( P(x, y+1) );
		}
		cout << ans << endl;
	}
}

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

#define rep(i,n) for(int i=0; i<n; i++)

int w,h;
std::string f[20];
int cnt;
int dx[4] = {1,0,-1,0};
int dy[4] = {0,-1,0,1};

void rec(int x, int y){
	int mx, my;

	f[y][x] = '#';
	cnt++;

	rep(i,4){
		mx = x + dx[i];
		my = y + dy[i];

		if(mx < 0 || mx >= w)continue;
		if(my < 0 || my >= h)continue;
		if(f[my][mx] != '.')continue;
		rec(mx, my);
	}
}




int main(){
	while(std::cin >> w >> h, (w||h)){
		rep(i,h){
			std::cin >> f[i];
		}
		cnt = 0;
		rep(i,h){
			rep(j,w){
				if(f[i][j] == '@')rec(j,i);
			}
		}
		std::cout << cnt << std::endl;
	}
	return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <climits>
#include <ctime>
#include <cassert>
using namespace std;

#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++)
#define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--)
#define all(v) begin(v), end(v)
#define pb(a) push_back(a)
#define fr first
#define sc second
#define INF 2000000000
#define int long long int

#define X real()
#define Y imag()
#define EPS (1e-10)
#define EQ(a,b) (abs((a) - (b)) < EPS)
#define EQV(a,b) ( EQ((a).X, (b).X) && EQ((a).Y, (b).Y) )
#define LE(n, m) ((n) < (m) + EPS)
#define LEQ(n, m) ((n) <= (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define GEQ(n, m) ((n) + EPS >= (m))

typedef vector<int> VI;
typedef vector<VI> MAT;
typedef pair<int, int> pii;
typedef long long ll;

typedef complex<double> P;
typedef pair<P, P> L;
typedef pair<P, double> C;

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

namespace std {
    bool operator<(const P& a, const P& b) {
        return a.X != b.X ? a.X < b.X : a.Y < b.Y;
    }
}

signed main() {
    int h, w;
    while(cin >> w >> h, h || w) {
        char board[30][30];
        bool visit[30][30] = {};
        pii st;
        rep(i,0,h) rep(j,0,w) {
            cin >> board[i][j];
            if(board[i][j] == '@') {
                st = pii(i, j);
                visit[i][j] = true;
            }
        }

        int ans = 0;
        queue<pii> q;
        q.push(st);
        while(!q.empty()) {
            pii t = q.front(); q.pop();
            ans++;
            rep(i,0,4) {
                int x = t.fr + dx[i];
                int y = t.sc + dy[i];
                if(x < 0 || x >= h || y < 0 || y >= w) continue;
                if(board[x][y] == '#') continue;
                if(visit[x][y]) continue;
                visit[x][y] = true;
                q.push(pii(x, y));
            }
        }
        cout << ans << endl;
    }
    return 0;
}