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

int masu[22][22];
int ans=0;
int i,j;

void saiki(int x,int y){
	/*
	for(i=0;i<22;i++){
		for(j=0;j<22;j++){
			if(masu[i][j]==0)printf(" ");
			if(masu[i][j]==1)printf("¡");
		}
		puts("");
	}
	printf("%d",ans);
	Sleep(100);
	system("cls");
	*/
	masu[x][y]=1;
	ans++;
	if(masu[x][y-1]==0)saiki(x,y-1);
	if(masu[x+1][y]==0)saiki(x+1,y);
	if(masu[x][y+1]==0)saiki(x,y+1);
	if(masu[x-1][y]==0)saiki(x-1,y);
}

int main(){
	for(;;){
		ans = 0;
	int W,H;
	int x,y;
	char str[22];
	
	for(i=0;i<22;i++){
		for(j=0;j<22;j++){
			masu[i][j]=1;
		}
	}
	
	scanf("%d%d", &W, &H);
	if(W == 0){
		break;
	}
	for(i=0;i<H;i++){
		scanf("%s",str);
		for(j=0;j<W;j++){
			if(str[j]=='.')masu[i+1][j+1]=0;
			if(str[j]=='@'){
				x=i+1;
				y=j+1;
				masu[i+1][j+1]=0;
			}
		}
	}
	/*
	for(i=0;i<22;i++){
		for(j=0;j<22;j++){
			if(masu[i][j]==0)printf(" ");
			if(masu[i][j]==1)printf("¡");
		}
		puts("");
	}
	printf("%d\n",ans);
	system("cls");
	*/
	saiki(x,y);
	/*
	for(i=0;i<22;i++){
		for(j=0;j<22;j++){
			if(masu[i][j]==0)printf(" ");
			if(masu[i][j]==1)printf("¡");
		}
		puts("");
	}
	*/
	printf("%d\n",ans);
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <queue>
#include <set>
#include <stdio.h>
using namespace std;
#define ll long long
typedef pair<int, int> P;
char maze[30][30];
int  w, h;
int sx, sy;
int ans = 0;

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

void bfs() {
	queue<P> que;
	que.push(P(sy, sx));

	while (que.size()) {
		P p = que.front();
		que.pop();
		for (int i = 0; i < 4; i++) {
			int nx = p.second + dx[i], ny = p.first + dy[i];


			if (0 <= nx && nx < w && 0 <= ny && ny < h && maze[ny][nx] != '#') {
				que.push(P(ny, nx));
				maze[ny][nx] = '#';
				ans++;
			}
		}
	}
	return;
}

int main() {
	while (1) {
		ans = 0;
		cin >> w >> h;
		if (w == 0 && h == 0) break;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				cin >> maze[i][j];
				if (maze[i][j] == '@') {
					maze[i][j] = '#';
					sx = j;
					sy = i;
					ans++;
				}
			}
		}
		bfs();
		cout << ans << endl;
	}

	return 0;
}

C++: /**
 *    author:  otera    
**/
#include<iostream>
#include<string> 
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm> 
#include<functional>
#include<iomanip>
#include<queue>
#include<deque>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
using namespace std;

//#define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef long double ld;
const int inf=1e9+7;
const ll INF=1LL<<60 ;
const ll mod=1e9+7 ;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<int, int> P;
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
#define pb push_back
#define debug(x)  cerr << #x << " = " << (x) << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

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

int w, h;
vector<vector<char>> c;
int ans;

void dfs(int i, int j) {
    c[i][j] = '#';
    ++ ans;
    rep(k, 4) {
        int ni = i + dx[k], nj = j + dy[k];
        if(0 <= ni and ni < h and 0 <= nj and nj < w and c[ni][nj] == '.') {
            dfs(ni, nj);
        }
    }
}

void solve() {
	while(cin >> w >> h) {
        if(w == 0 and h == 0) break;
        c.assign(h, vector<char>(w));
        int si, sj;
        rep(i, h) {
            rep(j, w) {
                cin >> c[i][j];
                if(c[i][j] == '@') {
                    c[i][j] = '.';
                    si = i, sj = j;
                }
            }
        }
        ans = 0;
        dfs(si, sj);
        cout << ans << endl;
    }
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	//int t; cin >> t; rep(i, t)solve();
	solve();
    return 0;
}