Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

var (
	sc = bufio.NewScanner(os.Stdin)
)

type Color int

const (
	ColorBlack Color = iota
	ColorRed
)

type Node struct {
	ID    int
	X, Y  int
	Color Color
	Nexts []*Node
}

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])

		// Initialize map
		m := map[int]map[int]*Node{}
		for x := 1; x <= w; x++ {
			m[x] = map[int]*Node{}
			for y := 1; y <= h; y++ {
				m[x][y] = &Node{ID: x + y*w, X: x, Y: y}
			}
		}
		for x := 1; x <= w; x++ {
			for y := 1; y <= h; y++ {
				nexts := []*Node{}
				if n, ok := m[x-1][y]; ok {
					nexts = append(nexts, n)
				}
				if n, ok := m[x][y-1]; ok {
					nexts = append(nexts, n)
				}
				if n, ok := m[x+1][y]; ok {
					nexts = append(nexts, n)
				}
				if n, ok := m[x][y+1]; ok {
					nexts = append(nexts, n)
				}
				m[x][y].Nexts = nexts
			}
		}

		// Set colors
		queue := []*Node{}
		touchedIDs := map[int]struct{}{}
		for y := 1; y <= h; y++ {
			sc.Scan()
			for i, c := range sc.Text() {
				switch c {
				case '.', '@':
					n := m[i+1][y]
					n.Color = ColorBlack
					if c == '@' {
						queue = append(queue, n)
						touchedIDs[n.ID] = struct{}{}
					}
				case '#':
					m[i+1][y].Color = ColorRed
				}
			}
		}

		// Search
		visitedIDs := map[int]struct{}{}
		for {
			if len(queue) == 0 {
				break
			}
			n := queue[0]
			queue = queue[1:]
			visitedIDs[n.ID] = struct{}{}
			for _, next := range n.Nexts {
				if next.Color == ColorRed {
					continue
				}
				if _, ok := touchedIDs[next.ID]; !ok {
					queue = append(queue, next)
					touchedIDs[n.ID] = struct{}{}
				}
			}
		}
		fmt.Println(len(visitedIDs))
	}
}

C++: #include <iostream>
#include <vector>
#include <list>
#include <set>
#include <string>
#include <cmath>
#include <algorithm>
#include <map>

using namespace std;
using ll=long long;
using vi=vector<int>;
using vll=vector<ll>;
using pii=pair<int,int>;
using vs=vector<string>;
using vpii=vector<pii>;
using si=set<int>;

#define REP(i,a) for(int i=0;i<a;i++)
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define rREP(i,a) for(int i=(a)-1;i>=0;i--)
#define rFOR(i,a,b) for(int i=(a)-1;i>=(b);i--)
#define out(S) cout<<(S)<<endl;
#define beginend(v) (v).begin(),(v).end()
#define IfOut(condition,text) if(condition){out(text);return 0;}
#define IfOutElse(condition,if_text,else_text) if(condition){out(if_text);return 0;}else{out(else_text);return 0;}
#define mod(s) ((s)%(ll)(1e9+7))
ll modpow(ll i, ll j) { ll tmp = 1; while (j) { if (j % 2)tmp = mod(tmp*i); i = mod(i*i); j /= 2; }return tmp; }
#define divmod(a,b) (mod(a*modpow((ll)b,(ll)(1e9+5))))
#define pb(i) push_back(i)
#define fst first
#define snd second
#define Foreach(item,collection) for(auto item:collection)
#define INF (int)1e10
#define removeAll(vec,deleg) auto itr=remove_if(beginend(vec),deleg);vec.erase(itr,vec.end());
void removeAt(string& s, int index) { s.erase(index, 1); }
template<typename T> void removeAt(vector<T>& v, int index) { v.erase(v.begin() + index); }
ll manhattanDistance(ll x1, ll y1, ll x2, ll y2) { return (abs(x2 - x1) + abs(y2 - y1)); }
vector<ll> sieveOfEratosthenes(int max) {ll prime;double sqrtmax = sqrt(max);vector<ll> primeVec, searchVec;FOR(i, 2, max + 1) searchVec.push_back(i);do{prime = searchVec[0];primeVec.push_back(prime);auto itr = remove_if(beginend(searchVec), [=](ll x)->bool {return x%prime == 0; });searchVec.erase(itr, searchVec.end());} while (prime < sqrtmax);primeVec.reserve(primeVec.size() + searchVec.size());primeVec.insert(primeVec.end(), beginend(searchVec));return primeVec;}
ll sum(vi v) {ll sum=0;Foreach(i,v){sum+=i;}return sum;}
#define ShowAll(v) Foreach(i,v) out(i);


int x,y;
vs box;
int dx[4]{0,1,0,-1};
int dy[4]{1,0,-1,0};
ll s=0;
void dfs(int px,int py){
    REP(i,4){
        int nx=px+dx[i];
        int ny=py+dy[i];
        if(nx>=0&&nx<x&&ny>=0&&ny<y&&box[ny][nx]=='.'){
            box[ny][nx]='d';
            s++;
            dfs(nx,ny);
        }
    }
    return;
}

int main(){
    vi ans;
    while (cin>>x>>y,(x||y))
    {
        box.resize(y);
        int sx=-1,sy;
        REP(i,y){cin>>box[i];if(sx==-1&&(sx=box[i].find('@'))!=-1){sy=i;}}
        s=1;
        dfs(sx,sy);
        ans.pb(s);
    }
    
    ShowAll(ans);
    return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;
typedef pair<int, int> P;

int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};
int h, w;
int sy, sx;
char field[22][22];

int main(){
 
  while(cin >> w >> h && (h || w)){
    fill(&field[0][0], &field[21][21] + 1, '#');
    for(int i = 1; i <= h; i++){
      for(int j = 1 ; j <= w; j++){
	cin >> field[i][j];
	if(field[i][j] == '@'){
	  sy = i;
	  sx = j;
	  field[i][j] = '#';
	}
      }
    }
    queue<P> que;
    que.push(P(sy, sx));
    int res = 1;
    while(!que.empty()){
      P p = que.front();
      que.pop();
      int y = p.first;
      int x = p.second;
      for(int i = 0; i < 4; i++){
	int y2 = y + dy[i];
	int x2 = x + dx[i];
	if(field[y2][x2] == '.'){
	  res++;
	  field[y2][x2] = '#';
	  que.push(P(y2, x2));
	}
      }
    }
    cout << res << endl;
  }
  return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <fstream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <assert.h>

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)  FOR(i,0,n)
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define ALL(s) (s).begin(),(s).end()
#define ITE(c) typeof((c).begin())

#define clr(a) memset((a),0,sizeof(a))
#define nclr(a) memset((a),-1,sizeof(a))
#define pb push_back
#define INRANGE(x,s,e) ((s)<=(x) && (x)<(e))
#define MP(a,b) make_pair((a),(b))

#define MAX 20

using namespace std;

int Map[MAX][MAX];

int w, h;
int sx, sy;

int cou;
int d_x[4]={-1,0,1,0};
int d_y[4]={0,-1,0,1};
void dfs(int x,int y){
	Map[x][y]=0;
	for(int i=0;i<4;i++){
		int nx=x+d_x[i];
		int ny=y+d_y[i];
		if(nx>=0&&nx<w&&ny>=0&&ny<h&&Map[nx][ny]){
			dfs(nx,ny);
			cou++;
		}
	}
}		

void setMap(int i,int j,char s){
	switch(s){
		case '.':Map[i][j]=1;break;
		case '#':Map[i][j]=0;break;
		case '@':sx=i;sy=j;Map[i][j]=0;break;
	}
}


char getMap(int i,int j){
	if(Map[i][j]){
		return '.';
	}else{
		return '#';
	}
}


int main(){
	char s;
	while(cin>>w>>h&&w||h){
		rep(i,h){
			rep(j,w){
				cin>>s;
				setMap(j,i,s);
			}
		}
		cou=1;
		dfs(sx,sy);
		cout<<cou<<endl;
	}
	return 0;
}