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 <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <functional>
using namespace std;

#define Rep(b, e, i) for(int i = b; i <= e; i++)
#define Repr(e, b, i) for(int i = e; i >= b; i--)
#define rep(n, i) Rep(0, n-1, i)
#define repr(n, i) Repr(n-1, 0, i)
#define all(v) (v).begin(), (v).end()
#define pb(x) push_back(x)
#define uniq(x) (x).erase(unique(all(x)),(x).end())

typedef long long ll;
typedef pair<ll, ll> llP;
typedef pair<int, int> intP;
typedef std::priority_queue<int> IntPrioQueue; //Z->A
typedef std::priority_queue<int, std::vector<int>, std::greater<int> > IntReversePrioQueue; //A->Z

const int MAX = 32;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = 1<<29;
const int MOD = 1000000007;

int w, h, sx, sy, cnt;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int maze[MAX][MAX];

bool range(int x, int y) {
    return (0 <= x && x < w && 0 <= y && y < h);
}

void dfs(int x, int y){
    maze[y][x] = 0;
    cnt++;
    rep(4, d) {
        if (range(x+dx[d], y+dy[d]) && maze[y+dy[d]][x+dx[d]]) {
            dfs(x+dx[d], y+dy[d]);
        }
    }
    return;
}

void solve(void){
    while (1) {
        cin >> w >> h;
        if (w == 0 && h == 0) break;
        cnt = 0;
        rep(h, y) rep(w, x) {
            char c; cin >> c;
            if (c == '#') maze[y][x] = 0;
            else maze[y][x] = 1;
            if (c == '@') {
                sx = x; sy = y;
            }
        }
        dfs(sx, sy);
        printf("%d\n", cnt);
    }
}

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

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

int dfs(char tebl[][20],int y,int x,int h,int w,int ans){
	tebl[y][x]='$';
	if(y>0&&tebl[y-1][x]=='.'){//up
		ans=max(ans,dfs(tebl,y-1,x,h,w,ans+1));
	}
	if(y<h&&tebl[y+1][x]=='.'){//down
		ans=max(ans,dfs(tebl,y+1,x,h,w,ans+1));
	}
	if(x>0&&tebl[y][x-1]=='.'){//left
		ans=max(ans,dfs(tebl,y,x-1,h,w,ans+1));
	}
	if(x<w&&tebl[y][x+1]=='.'){//right
		ans=max(ans,dfs(tebl,y,x+1,h,w,ans+1));
	}
	return ans;
}
	




int main(){
	while(true){
		char tebl[20][20]={};
		int w,h,sy,sx;
		cin>>w>>h;
		if(w==0||h==0){
			break;
		}
		for(int n=0;n<h;n++){
			for(int m=0;m<w;m++){
				cin>>tebl[n][m];
				if(tebl[n][m]=='@'){
					sy=n;
					sx=m;
				}
			}
		}
		cout<<dfs(tebl,sy,sx,h-1,w-1,1)<<endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<P,P> PP;
const ll MOD = 1000000007;
const int IINF = INT_MAX;
const ll LLINF = LLONG_MAX;
const int MAX_N = int(1e5 + 5);
const double EPS = 1e-10;
const int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
#define REP(i, n) for (int i = 0; i < n; i++)

struct UnionFindTree {
    vector<int> par;
    vector<int> rank;
    vector<int> siz;

    void init(int n) {
        par.resize(n);
        rank.resize(n);
        siz.resize(n);
        for (int i = 0; i < n; i++) {
            par[i] = i;
            rank[i] = 0;
            siz[i] = 1;
        }
    }
    int find(int x) {
        if (par[x] == x) {
            return x;
        } else {
            return par[x] = find(par[x]);
        }
    }
    void unite(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) return;
        if (rank[x] < rank[y]) swap(x, y);
        if (rank[x] == rank[y]) rank[x]++;
        par[y] = x;
        siz[x] += siz[y];
    }
    bool is_same(int x, int y) {
        return find(x) == find(y);
    }
    int size(int x) {
        x = find(x);
        return siz[x];
    }
};

int main() {
    int h, w;
    while(cin >> w >> h, h){
        string s[25];
        int si, sj;
        UnionFindTree g;
        g.init(h*w);
        REP(i,h) cin >> s[i];
        REP(i,h){
            REP(j,w){
                if(s[i][j]=='#')continue;
                if(s[i][j]=='@'){
                    si = i;
                    sj = j;
                }
                REP(k,4){
                    int ni = i+di[k], nj = j+dj[k];
                    if(ni < 0 || ni >= h) continue;
                    if(nj < 0 || nj >= w) continue;
                    if(s[ni][nj]=='#') continue;
                    g.unite(i*w+j,ni*w+nj);
                }
            }
        }
        cout << g.size(si*w+sj) << endl;
    }
    return 0;
}