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 <bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef vector<int>    VI;
typedef vector<bool>   VB;
typedef vector<string> VS;

#define MP make_pair
#define PB push_back
#define ALL(x)  (x).begin(),(x).end()
#define rep(i, N)  for (int i = 0; i < (int)(N); i++)
#define REP(i, init, N)  for (int i = (init); i < (int)(N); i++)
#define DUMP( x ) cerr << #x << " = " << ( x ) << endl
#define RANGE(x, min, max) min <= x && x <= max

int w, h;
VS tbl;

const int DX[] = {-1, 0, 1, 0}, DY[] = {0, -1, 0, 1};

int dfs(int y, int x) {
	int res = 1;
	tbl[y][x] = '#';
	rep (i, 4) {
		int ny = y + DY[i], nx = x + DX[i];
		if (RANGE(ny, 0, h - 1) && RANGE(nx, 0, w - 1) && tbl[ny][nx] == '.') {
			res += dfs(ny, nx);
		}
	}
	return res;
}

int main(void) {
	while (1) {
		tbl.clear();
		cin >> w >> h;
		if (w == 0 && h == 0) break;
		rep (i, h) {
			string s;
			cin >> s;
			tbl.PB(s);
		}
		int sy, sx;
		rep (i, h) rep (j, w) {
			if (tbl[i][j] == '@') {
				sy = i;
				sx = j;
				break;
			}
		}
		cout << dfs(sy, sx) << endl;	
	}
    return 0;
}

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

bool used[25][25];
int s;
int n,m;

void check(int x,int y){
	if(x<0 || n<=x || y<0 || m<=y)return;
	if(used[x][y])return;
	used[x][y]=true,s++;
	check(x+1,y);
	check(x-1,y);
	check(x,y+1);
	check(x,y-1);
}

int main(){
	int i,j,k;
	int a,b,c;
	int x,y;
	char ca[26];
	while(cin>>m>>n){
		if(!n)break;
		memset(used,false,sizeof(used));
		for(i=0;i<n;i++){
			cin>>ca;
			for(j=0;j<m;j++){
				if(ca[j]=='#')used[i][j]=true;
				if(ca[j]=='@')x=i,y=j;
			}
		}
		s=0;
		check(x,y);
		cout<<s<<endl;
	}
}

C++: #include<bits/stdc++.h>
using namespace std;

struct UnionFind {
  vector< int > data;
  UnionFind(int sz) {
    data.assign(sz, -1);
  }
  int Size(int x) {
    return(-data[Find(x)]);
  }
  int Find(int k) {
    return(data[k] < 0 ? k : data[k] = Find(data[k]));
  }
  void Unite(int a, int b) {
    a = Find(a), b = Find(b);
    if(a == b) return;
    data[a] += data[b];
    data[b] = a;
  }
};

int main() {
  int W, H;
  while(cin >> W >> H, W) {
    string mas[50];
    for(int i = 0; i < H; i++) cin >> mas[i];
    UnionFind uf(W * H);
    for(int i = 0; i < H; i++) {
      for(int j = 1; j < W; j++) {
        if(mas[i][j - 1] != '#' && mas[i][j] != '#') uf.Unite(i * W + j - 1, i * W + j);
      }
    }
    for(int i = 1; i < H; i++) {
      for(int j = 0; j < W; j++) {
        if(mas[i - 1][j] != '#' && mas[i][j] != '#') uf.Unite(i * W - W + j, i * W + j);
      }
    }
    for(int i = 0; i < H; i++) {
      for(int j = 0; j < W; j++) {
        if(mas[i][j] == '@') cout << uf.Size(i * W + j) << endl;
      }
    }
  }
}

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

int w, h;
char tile[20][20];
int ans = 0;
int dx[4] = { 0, 0, 1, -1 };
int dy[4] = { 1, -1, 0, 0 };

void solve(int x, int y)
{
	ans++;
	tile[y][x] = '#';
	for (int i = 0; i < 4; i++)
	{
		int X = x + dx[i];
		int Y = y + dy[i];
		if (tile[Y][X] == '.' && 0 <= X && X < w && 0 <= Y && Y < h)
		{
			solve(X, Y);
		}
	}
}

int main()
{

	while (cin >> w >> h, w + h != 0){
		
		ans = 0;

		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				cin >> tile[i][j];
			}
		}
		
		for (int i = 0; i < h; i++)
		{
			for (int j = 0; j < w; j++)
			{
				if (tile[i][j] == '@') solve(j, i);
			}
		}

		cout << ans << endl;
	}

	return 0;
}

C++: #include<iostream>
#include<climits>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<functional>
#include<stack>
#include<list>
#include<string>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) for(int i=0;i<(n);i++)
#define INF INT_MAX/3
#define ALL(a) (a).begin(),(a).end()
#define PII pair<int,int>
#define PCC pair<char,char>
#define PCI pair<char,int>
#define PIC pair<int,char>
#define MP make_pair
#define DEBUG(x) cout<<#x<<": "<<x<<endl;
char MAP[20][20];
int ans;
int dx[4] = { 1, 0, -1, 0 };
int dy[4] = { 0, 1, 0, -1 };
void dfs(int x, int y,int w,int h) 
{
	if (x < 0 || x>=w || y < 0 || y>=h)return;
	else if (MAP[y][x] == '#')return;
	else
	{
		MAP[y][x] = '#';
		::ans++;
		for (int r = 0; r < 4; r++)
		{
			int nx = x + dx[r];
			int ny = y + dy[r];
			dfs(nx, ny,w,h);
		}
	}
}
int main()
{
	while (1)
	{
		int w, h;
		::ans = 0;
		cin >> w >> h;
		if (w == 0)break;
		PII self;
		REP(y, h)
		{
			REP(x, w)
			{
				cin >> MAP[y][x];
				if (MAP[y][x] == '@')self = MP(y, x);
			}
		}
		dfs(self.second, self.first,w,h);
		cout << ::ans << endl;
	}
	return 0;
}