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>
#include<string.h>
int flag[20][20];//?????°?????????0
char map[20][21];//y:x
int count;
int H,W;
void search(int nx,int ny);
main(){
    scanf("%d%d",&W,&H);
    while(H){
        memset(flag,0,sizeof(flag));
        memset(map,0,sizeof(map));
        for(int i = 0;i < H;i++){
            scanf("%s",map[i]);
        }
        for(int i = 0;i < H;i++){
            for(int j = 0;j < W;j++){
                if(map[i][j] == '@'){
                    count = 0;
                    search(j,i);
                    printf("%d\n",count);
                }
            }
        }
        scanf("%d%d",&W,&H);
    }
}
void search(int nx,int ny){
    if(nx<0 || ny<0 || nx>W-1 || ny>H-1){
        return;
    }
    if(map[ny][nx] == '#'){
        return;
    }
    if(flag[ny][nx] == 0){
        flag[ny][nx]++;
        count++;
        search(nx+1,ny);
        search(nx-1,ny);
        search(nx,ny+1);
        search(nx,ny-1);
    }
}

C++: #include <algorithm>
#include <cmath>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <cassert>
#include <functional>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
//#define LOG(...)
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define RSORT(c) sort((c).rbegin(),(c).rend())
#define CLR(a) memset((a), 0 ,sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = { -1, 0, 1, 0 }; const int dy[] = { 0, 1, 0, -1 };

struct UnionFind {
	vector<int> v;
	UnionFind(int n) : v(n)  { for (int i = 0; i < n; i++) v[i] = i; }
	int  find(int x)         { return v[x] == x ? x : v[x] = find(v[x]); }
	void unite(int x, int y) { v[find(x)] = find(y); }
};

int main() {
	int w, h;
	while (cin >> w >> h, w){
		vector<string> cell(h);
		int sx=-1, sy;
		REP(i, h){
			cin >> cell[i];
			if (sx==-1)
			REP(j, w){
				if (cell[i][j] == '@'){
					sx = j;
					sy = i;
					cell[i][j] = '#';
				}
			}
		}
		queue<pair<int, int>> Q;
		Q.push(pair<int, int>(sx, sy));
		int ans = 0;
		while (!Q.empty()){
			ans++;
			pair<int, int> p = Q.front(); Q.pop();
			REP(i, 4){
				if (0 <= p.first + dx[i] && p.first + dx[i] < w && 0 <= p.second + dy[i] && p.second + dy[i] < h&&cell[p.second + dy[i]][p.first + dx[i]] == '.'){
					cell[p.second + dy[i]][p.first + dx[i]] = '#';
					Q.push(pair<int, int>(p.first + dx[i], p.second + dy[i]));
				}
			}
		}
		cout << ans << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define P pair<ll,ll>
#define FOR(I,A,B) for(ll I = (A); I < (B); ++I)
#define FORR(I,A,B) for(ll I = (B-1); I >= (A); --I)
#define SORT(x) (sort(x.begin(),x.end())) // 0 2 2 3 4 5 8 9
#define REV(x) (reverse(x.begin(),x.end())) //reverse
ll gcd(ll a,ll b){if(a<b)swap(a,b);if(a%b==0)return b;else return gcd(b,a%b);}
ll lcm(ll a,ll b){ll c=gcd(a,b);return ((a/c)*(b/c)*c);}//saisyo kobaisu
#define NEXTP(x) next_permutation(x.begin(),x.end())
const ll INF=1e18+7;
const ll MOD=1e9+7;

bool solve(){
	ll H,W;
	cin >> W >> H;
	ll dx[] = {0,1,0,-1};
	ll dy[] = {1,0,-1,0};
 	if(H==0&&W==0)return false;
	string tile[H];
	bool did[H][W];
	FOR(i,0,H)FOR(j,0,W)did[i][j]=false;
	FOR(i,0,H)cin >> tile[i];
	queue<P> q;
	ll ans = 0;
	FOR(i,0,H)FOR(j,0,W){
		if(tile[i][j]=='@'){
			q.push({i,j});
			ans++;
			did[i][j]=true;
		}else if(tile[i][j]=='#'){
			did[i][j]=true;
		}
	}
	while(not q.empty()){
		ll x = q.front().first;
		ll y = q.front().second;
		q.pop();
		FOR(i,0,4){
			if(x+dx[i]<H && x+dx[i]>=0 && y+dy[i]<W && y+dy[i]>=0){
				if(not did[x+dx[i]][y+dy[i]]){
					did[x+dx[i]][y+dy[i]] = true;
					ans++;
					q.push({x+dx[i],y+dy[i]});
				}
			}
		}
	}
	cout << ans << endl;
	return true;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	while(1){
		if(not solve())break;
	}
}