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>
#include <utility>
#include <algorithm>
#include <map>

using namespace std;

typedef pair<int, int> P;

const int W = 20, H = 20;

int w, h;
char board[H + 2][W + 2];

int search(int x, int y){
	int c = 0, nx, ny;
	int dx[] = {0, -1, 1, 0}, dy[] = {-1, 0, 0, 1};
	queue<P> q;
	map<P, bool> v;
	q.push(make_pair(x, y));
	while(!q.empty()){
		P p = q.front();
		q.pop();
		v[p] = true;
		board[p.second][p.first] = '#';
		++c;
		for(int i = 0;i < 4;++i){
			nx = p.first + dx[i];
			ny = p.second + dy[i];
			if(board[ny][nx] == '.' and !v[make_pair(nx, ny)]){
				q.push(make_pair(nx, ny));
				v[make_pair(nx, ny)] = true;
			}
		}
	}
	return c;
}

int main(){
	int sx, sy;
	while(cin >> w >> h, w){
		for(int i = 0;i < h + 1;++i){
			fill(board[i], board[i] + W + 2, 0);
		}
		for(int i = 0;i < h;++i){
			for(int j = 0;j < w;++j){
				cin >> board[i + 1][j + 1];
				if(board[i + 1][j + 1] == '@'){
					sx = j + 1;
					sy = i + 1;
				}
			}
		}
		cout << search(sx, sy) << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
//const ll mod = 1000000007;
int dh[4] = {1, 0, -1, 0};
int dw[4] = {0, 1, 0, -1};
int main() {
    //cout.precision(10);
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(true) {
        int H, W;
        cin >> W >> H;
        if(H == 0 && W == 0) break;
        string field[21];
        i_i start;
        for(int i = 1; i <= H; i++) {
            cin >> field[i];
            field[i] = "&" + field[i];
            for(int w = 1; w <= W; w++) {
                if(field[i][w] == '@') start = {i, w};
            }
        }
        queue<i_i> que;
        que.push(start);
        int ans = 0;
        while(!que.empty()) {
            i_i now = que.front();
            que.pop();
            int h = now.first;
            int w = now.second;
            //cerr << h << " " << w << endl;
            if(field[h][w] == '#') continue;
            ans++;
            field[h][w] = '#';
            for(int k = 0; k < 4; k++) {
                int newh = h + dh[k];
                int neww = w + dw[k];
                if(neww <= 0 || neww > W || newh > H || newh <= 0) continue;
                if(field[newh][neww] == '#') continue;
                que.push({newh, neww});
            }
        }
        cout << ans << endl;
    }
    return 0;
}

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

int main(void){
    int h, w;
    while (cin >> w >> h) {
        if (h * w == 0) return 0;
        
        int on_h, on_c;
        bool check[30][30] = {};
        char s[30][30];
        for (int i = 0; i < h; i++) {
            cin >> s[i];
            for (int j = 0; j < w; j++) {
                if (s[i][j] == '@') {
                    on_h = i;  on_c = j;
                }
                else if (s[i][j] == '.') check[i][j] = true;
            }
        }
        
        int ans = 1, dh[4] = {1, 0, -1, 0}, dc[4] = {0, 1, 0, -1};
        queue<int> que_h, que_c;
        que_h.push(on_h);
        que_c.push(on_c);
        
        while (!que_h.empty()) {
            int qh = que_h.front(), qc = que_c.front();
            for (int i = 0; i < 4; i++) {
                if (qh + dh[i] >= 0 && qh + dh[i] < h && qc + dc[i] >= 0 && qc + dc[i] < w 
                && check[qh + dh[i]][qc + dc[i]]) {
                    ans++;
                    check[qh + dh[i]][qc + dc[i]] = false;
                    que_h.push(qh + dh[i]);
                    que_c.push(qc + dc[i]);
                }
            }
            que_h.pop();
            que_c.pop();
        }
        
        cout << ans << endl;
    }
}

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

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

void dfs(int y,int x)
{
	if(c[y][x]=='#') return;
	c[y][x]='#';
	++ans;
	for (int i = 0; i < 4; ++i)
	{
		int ny=y+dy[i],nx=x+dx[i];
		if(0<=ny&&ny<h&&0<=nx&&nx<w) dfs(ny,nx);
	}
	return;
}

int main(int argc, char const *argv[])
{
	while(cin>>w>>h,w)
	{
		ans=0;
		int x,y;
		for (int i = 0; i < h; ++i)
		{
			for (int j = 0; j < w; ++j)
			{
				cin>>c[i][j];
				if(c[i][j]=='@') y=i,x=j,c[i][j]='.';
			}
		}
		dfs(y,x);
		cout<<ans<<endl;
	}
	return 0;
}