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 <vector>
#include <queue>

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

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, -1, 0, 1};
void solve()
{
	int W, H;
	while(cin >> W >> H, W || H)
	{
		vector< vector<char> > field(H);
		for(int i = 0; i < field.size(); ++i)
		{
			field[i].resize(W);
		}
		queue<P> Que;
		for(int i = 0; i < field.size(); ++i)
		{
			for(int j = 0; j < field[i].size(); ++j)
			{
				cin >> field[i][j];
				if(field[i][j] == '@')
				{
					P p;
					p.first = j;
					p.second = i;
					Que.push(p);
					field[i][j] = '#';
				}
			}
		}
		int count = 1;
		while(!Que.empty())
		{
			P p = Que.front();
			Que.pop();
			for(int i = 0; i < 4; ++i)
			{
				P np;
				np.first = p.first + dx[i];
				np.second = p.second + dy[i];
				if((0 <= np.first && np.first < W) && (0 <= np.second && np.second < H) && field[np.second][np.first] == '.')
				{
					Que.push(np);
					field[np.second][np.first] = '#';
					++count;
				}	
			}
		}
		cout << count << endl;
	}
}

int main()
{
	solve();
	return(0);
}

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

int n, m, cnt = 0;
const int MAX = 30;
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
bool seen[MAX][MAX];
char C[MAX][MAX];

void dfs(int x, int y){
    seen[x][y] = true;
    for(int i = 0; i < 4; i++){
        int nx, ny;
        nx = dx[i] + x;
        ny = dy[i] + y;
        if(nx < 0 or ny < 0 or nx >= n or ny >= m){
            continue;
        }
        if(seen[nx][ny] or C[nx][ny] == '#'){
            continue;
        }
        cnt++;
        dfs(nx, ny);
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    while(cin >> m >> n){
        if(n == 0 and m == 0) return 0;
        
        int sx, sy;
        for(int i = 0; i < n; i++){
            for(int j = 0; j < m; j++){
                seen[i][j] = false;
                cin >> C[i][j];
                if(C[i][j] == '@'){
                    sx = i;
                    sy = j;
                }
            }
        }

        cnt = 1;
        dfs(sx, sy);
        cout << cnt << endl;
    }

    return 0;
}

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

int moved = 0;
int w, h;
char myMap[20][20];

bool safe(int x, int y){
    return x < h && y < w;
}

void move(int x, int y){
    myMap[x][y] = '#';
    moved++;
    if (myMap[x+1][y] == '.' && safe(x+1, y)) move(x+1, y);
    if (myMap[x-1][y] == '.' && safe(x-1, y)) move(x-1, y);
    if (myMap[x][y+1] == '.' && safe(x, y+1)) move(x, y+1);
    if (myMap[x][y-1] == '.' && safe(x, y-1)) move(x, y-1);
    return;
}

int main(){
    while (1){
	cin >> w >> h;
	if (w == 0 && h == 0) break;
	int initX, initY;

	for (int i = 0; i < h; i++){
	    for (int j = 0; j < w; j++){
		cin >> myMap[i][j];
		if (myMap[i][j] == '@'){
		    initX = i; initY = j;
		}
	    }
	}

	move(initX, initY);

	cout << moved << endl;
    }
}

C++: #include<iostream>
#include<cstring>
typedef long long ll;
typedef long double ld;
using namespace std;
ll dx[] = {1, -1, 0, 0};
ll dy[] = { 0,0,1,-1 };
ll W, H;
ll cnt;
char field[100][100];
void dfs(ll y, ll x) {
	field[y][x] = '#';
	for (ll k = 0; k < 4; k++) {
		ll ny = y + dy[k];
		ll nx = x + dx[k];
		if (ny >= 0 && ny <H && nx >= 0 && nx < W) {
			if (field[ny][nx] == '.')
			{
				cnt++;
				dfs(ny, nx);
			}
		}
	}
	return;
}
int main() {
	while (cin >> W >> H) {
		if (W == 0 && H == 0) { break; }
		cnt = 1;
		for (ll i = 0; i < H; i++) {
			cin >> field[i];
		}
		for (ll i = 0; i < H; i++) {
			for (ll j = 0; j < W; j++) {
				if (field[i][j] == '@') {
					dfs(i, j);
				}
			}
		}
		cout << cnt << endl;
	}
}

C++: #include "bits/stdc++.h"

using namespace std;

#define rep(a,b) for(int (a)=0;(a)<(b);(a)++)

int main(){
    int w,h;
    while(cin>>w>>h){
    if(w==0 && h==0)break;
    int sx,sy;
    int sum=0;
    int dy[]={-1,0,1,0};
    int dx[]={0,-1,0,1};
    
    
    string data[30]={};
    
    for(int i=0;i<h;i++){
        cin>>data[i];
    }
    
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(data[i][j]=='@'){
                sx=j;sy=i;
                break;
            }
        }
    }
    
    bool visited[30][30]={};
    
    queue<pair<int,int> > que;
    
    que.push( pair<int,int>(sy,sx) );
    visited[sy][sx]=true;
    
    while(que.size()){
        int qff=que.front().first,qfs=que.front().second;
        sum++;
        rep(i,4){
            if( qff+dy[i]<h && qff+dy[i]>=0 && qfs+dx[i]<w && qfs>=0 && visited[ qff+dy[i] ][ qfs+dx[i] ]==false && data[ qff+dy[i] ][ qfs+dx[i] ]=='.' ){
                que.push( pair<int,int>(qff+dy[i] , qfs+dx[i]) );
                visited[ qff+dy[i] ][ qfs+dx[i] ]=true;
            }
        }
            
        que.pop();
    }
    
    cout<<sum<<endl;
    }
    
    return 0;
}