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 <stdio.h>

int cnt;

void dp(char fld[20][20], int y, int x)	{
	
	fld[y][x] = '#';
	cnt++;
	///
	//printf("%d %d\n", x, y);
	///
	
	for (int dy = -1; dy <= 1; dy++)	{
		for (int dx = -1; dx <= 1; dx++)	{
			if (dy != 0 && dx != 0)	continue;
			if (y + dy < 0 || y + dy >= 20 || x + dx < 0 || x + dx >= 20)	continue;
			
			if (fld[y+dy][x+dx] == '.')	{
				dp(fld, y+dy, x+dx);
			}
		}
	}	
}

int main()	{
	
	int w, h, cy, cx;
	char temp;
	char fld[20][20];
	
	while (true)	{
		
		scanf("%d %d%*c", &w, &h);
		if (w == 0)	break;
		
		// initialize
		for (int y = 0; y < 20; y++)	{
			for (int x = 0; x < 20; x++)	{
				fld[y][x] = '#';
			}
		}
		
		for (int i = 0; i < h; i++)	{
			///
			//printf("%d列目\n", i);
			///
			for (int j = 0; j < w; j++)	{
				scanf("%c", &temp);
				fld[i][j] = temp;
				
				if (temp == '@')	{
					cy = i;
					cx = j;
				}
			}
			scanf("%*c");
		}
		
		/*
		for (int y = 0; y < 20; y++)	{
			printf("%2d: ", y);
			for (int x = 0; x < 20; x++)	{
				printf("%c", fld[y][x]);
			}
			printf("\n");
		}
		*/
		
		cnt = 0;
		
		dp(fld, cy, cx);
		
		printf("%d\n", cnt);
	}
	
	return 0;
}

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

static const int WHITE = 0;
static const int ABLE  = 1;
static const int N = 20;

char a[N][N];
int judge[N][N];

void search(int x, int y, int w, int h) {
	queue<int> qx, qy;
	qx.push(x);
	qy.push(y);
	judge[y][x] = ABLE;
	
	int tx, ty;
	while (!qx.empty() && !qy.empty()) {
		tx = qx.front();
		qx.pop();
		ty = qy.front();
		qy.pop();
		
		if (ty + 1 < h && a[ty+1][tx] != '#' && judge[ty+1][tx] == WHITE) {
			judge[ty+1][tx] = ABLE;
			qx.push(tx);
			qy.push(ty+1);
		}
		if (ty - 1 >= 0 && a[ty-1][tx] != '#' && judge[ty-1][tx] == WHITE) {
			judge[ty-1][tx] = ABLE;
			qx.push(tx);
			qy.push(ty-1);
		}
		if (tx + 1 < w && a[ty][tx+1] != '#' && judge[ty][tx+1] == WHITE) {
			judge[ty][tx+1] = ABLE;
			qx.push(tx+1);
			qy.push(ty);
		}
		if (tx - 1 >= 0 && a[ty][tx-1] != '#' && judge[ty][tx-1] == WHITE) {
			judge[ty][tx-1] = ABLE;
			qx.push(tx-1);
			qy.push(ty);
		}
	}
}

int main() {
	while (1) {
		int w, h;
		cin >> w >> h;
		if (h == 0) break;
		
		int startx, starty;
		for (int i = 0; i < h; i++)
			for (int j = 0; j < w; j++)
				cin >> a[i][j];
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (a[i][j] == '.' || a[i][j] == '#') judge[i][j] = WHITE;
				else {
					startx = j;
					starty = i;
					judge[i][j] = WHITE;
				}
			}
		}
		
		search(startx, starty, w, h);
		
		int count = 0;
		for (int i = 0; i < h; i++)
			for (int j = 0; j < w; j++)
				if (judge[i][j] == ABLE) count++;
			
		cout << count << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <numeric>
#include <string>
#include <string.h>
#include <map>
#include <set>
#include <functional>
#include <complex>

using namespace std;

const int INF = (1<<30) - 1;

int dw[] = {0, 0, 1, -1};
int dh[] = {1, -1, 0, 0};

int dfs(vector<string>& field, int h, int w){
    int H = field.size();
    int W = field[0].size();
    
    field[h][w] = 'a';
    int res = 1;
    
    for(int i=0; i<4; i++){
        int nh = h + dh[i];
        int nw = w + dw[i];
        if(nh<0 || H<=nh || nw<0 || W<=nw || field[nh][nw] != '.')
            continue;
        res += dfs(field, nh, nw);
    }
    return res;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int W, H;
    while(cin>>W>>H, W|H){
        vector<string> field(H);
        for(int i=0; i<H; i++)
            cin >> field[i];
        
        for(int i=0; i<H; i++){
            for(int j=0; j<W; j++){
                if(field[i][j] == '@'){
                    cout << dfs(field, i, j) << endl;
                    break;
                }
            }
        }
    }
    
    return 0;
}

C++: #include<iostream>
#include<string>
using namespace std;
int x[21][21],ans=0,w,h;
string d[21];
void f(int i,int j){
	ans++;
	x[i][j]=1;
	if(i+1<h){
		if(x[i+1][j]==0 && d[i+1][j]=='.')
		f(i+1,j);
	}
	if(i-1>=0){
		if(x[i-1][j]==0 && d[i-1][j]=='.')
		f(i-1,j);
	}
	if(j+1<w){
		if(x[i][j+1]==0 && d[i][j+1]=='.')
		f(i,j+1);
	}
	if(j-1>=0){
		if(x[i][j-1]==0 && d[i][j-1]=='.')
		f(i,j-1);
	}
}
int main(){
	while(cin>>w>>h,w){
		int a,b;
		ans=0;
		for(int i=0;i<21;i++){
			for(int j=0;j<21;j++){
				x[i][j]=0;
			}
		}
		for(int i=0;i<h;i++){
			cin>>d[i];
			for(int j=0;j<w;j++){
				if(d[i][j]=='@'){
					a=j;
					b=i;
				}
			}
		}
		f(b,a);
		cout<<ans<<endl;
	}
	return 0;
}