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;

#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define INF 114514810
#define ll long long
#define ALL(a) (a).begin(),(a).end()
#define SORT(v) sort(ALL(v))
//#define scanf scanf_s
typedef pair<int, int> P;

int w, h;
char fld[30][30];

int di[] = { -1, 0, 0, 1 }; int dj[] = { 0, -1, 1, 0 };
int main()
{
	while (cin >> w >> h, w)
	{
		queue<P> que;
		REP(i, h)REP(j, w)
		{
			cin >> fld[i][j];
			if (fld[i][j] == '@') que.push(P(i, j));
		}
		int cnt = 1;
		while (que.size())
		{
			int x = que.front().first; int y = que.front().second;
			que.pop();
			REP(k, 4)
			{
				int nx = x + di[k]; int ny = y + dj[k];
				if (nx >= 0 && nx < h&&ny >= 0 && ny < w&&fld[nx][ny] == '.')
				{
					que.push(P(nx, ny));
					cnt++;
					fld[nx][ny] = '@';
				}
			}
		}
		cout << cnt << endl;
	}
	return 0;
}

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

int w, h;
int vx[4] = { 1, 0, -1, 0 };
int vy[4] = { 0, 1, 0, -1 };
bool used[25][25];
char c[25][25];

int dfs(int x, int y){
    int ret = 1;
    for (int i = 0; i < 4; i++){
        int nextx = x + vx[i];
        int nexty = y + vy[i];

        if (0 < nextx && nextx < w + 1 && 0 < nexty && nexty < h + 1 && c[nextx][nexty] == '.' && !used[nextx][nexty]){
            used[nextx][nexty] = true;
            ret += dfs(nextx, nexty);
        }
    }

    return ret;
    
}

int main(void){

    while (true){

        cin >> w >> h;

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

        for (int i = 0; i <= 20; i++){
            for (int j = 0; j <= 20; j++){
                used[i][j] = false;
            }
        }

        int sx, sy;

        for (int i = 1; i <= h; i++){
            for (int j = 1; j <= w; j++){
                cin >> c[j][i];
                if (c[j][i] == '@'){
                    sx = j; sy = i;
                }
            }
        }

        cout << dfs(sx, sy) << endl;
    }
 

}

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

char A[20][20];
bool V[20][20];
int count,H,W;
void check(int x,int y){
    if(0<=x-1){
        if(A[x-1][y]=='.'&&!V[x-1][y]){
            count++;
            V[x-1][y]=true;
            check(x-1,y);
        }
    }
    if(x+1<H){
        if(A[x+1][y]=='.'&&!V[x+1][y]){
            count++;
            V[x+1][y]=true;
            check(x+1,y);
        }
    }
   if(0<=y-1){
        if(A[x][y-1]=='.'&&!V[x][y-1]){
            count++;
            V[x][y-1]=true;
            check(x,y-1);
        }
    }   
    if(y+1<W){
        if(A[x][y+1]=='.'&&!V[x][y+1]){
            count++;
            V[x][y+1]=true;
            check(x,y+1);
        }
    }
    return;
    }
int main(){
    int x,y;
    count=1;
    while(true){
        cin>>W>>H;
        if(W==0&&H==0){
            break;
        }else{
            for(int i=0;i<H;i++){
                for(int j=0;j<W;j++){
                    cin>>A[i][j];
                    if(A[i][j]=='@'){
                        x=i;
                        y=j;
                    }
                }
            }
            for(int i=0;i<H;i++){
                for(int j=0;j<W;j++){
                    V[i][j]=false;
                    }
                }
            }
        check(x,y);
        cout<<count<<endl;
        count=1;
    }
    return 0;
}

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

const int INF = 10000009;
char maze[20][20];
int W, H, sx, sy;
int ans = 1;
int d[20][20];
int dx[4] = { 1, 0, -1, 0 }, dy[4] = { 0, 1, 0, -1 };

void bfs() {
	queue< pair<int, int> > que;
	for (int i = 0; i < H; i++)for (int j = 0; j < W; j++) d[i][j] = INF;
	que.push(make_pair(sx, sy));
	d[sy][sx] = 0;
	while (que.size()) {
		pair<int, int> p = que.front(); que.pop();
		for (int i = 0; i < 4; i++) {
			int nx = p.first + dx[i], ny = p.second + dy[i];
			if (nx >= 0 && nx < W && ny >= 0 && ny < H && maze[ny][nx] != '#') {
				if (d[ny][nx] == INF) {
					que.push(make_pair(nx, ny));
					d[ny][nx] = 0;
					ans++;
				}
			}
		}
	}
}
int main() {
	while (cin >> W >> H && W + H) {
		ans = 1;
		for (int i = 0; i < H; i++) {
			for (int j = 0; j < W; j++) {
				cin >> maze[i][j];
				if (maze[i][j] == '@') {
					sy = i; sx = j;
				}
			}
		}
		bfs();
		cout << ans << endl;
	}
}