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<cstdio>
#include<algorithm>
#include<cmath>
#include<string>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cctype>
#include<climits>
#include<bitset>
#define ld long double
#define ll long long int
#define ull unsigned long long int
#define rep(i,n) for(i=0;i<n;i++)
#define fin(ans) cout<<(ans)<<endl
#define INF INT_MAX
#define vi vector<int>
#define vc vector<char>
#define vs vector<string>
#define vpii vector<pair<int,int> >
#define vvi vector<vector<int> >
#define vvc vector<vector<char> >
#define vvs vector<vector<string> >
#define P 1000000007
using namespace std;
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 long long LLINF=1LL<<60;
//set<int>::iterator it;
//|\_
//g++ -std=c++1z temp.cpp
//./a.out

int ans=0;
vvc v(30,vector<char> (30,'a'));
int dfs(int x,int y,int h,int w){
    // cout<<x<<" "<<y<<" "<<ans<<endl;
    if(v[y][x]=='%')return 0;
    if(v[y][x]=='@'||v[y][x]=='.'){
        ans++;
        v[y][x]='%';
        if(x+1<w&&v[y][x+1]=='.')dfs(x+1,y,h,w);
        if(x-1>=0&&v[y][x-1]=='.')dfs(x-1,y,h,w);
        if(y+1<h&&v[y+1][x]=='.')dfs(x,y+1,h,w);
        if(y-1>=0&&v[y-1][x]=='.')dfs(x,y-1,h,w);

    }
    else return 0;
    return 0;
}

int main(void){ios::sync_with_stdio(false);cin.tie(0);
//////////////////////////////////////////////////////
    
    
    int h,w,i,j,x,y;
    while(1){
        cin>>w>>h;
        ans=0;
        if(h==0&&w==0)break;
        rep(i,30)rep(j,30)v[i][j]='%';
        rep(i,h){
            rep(j,w){
                cin>>v[i][j];
                if(v[i][j]=='@'){
                    // cout<<i<<" "<<j<<endl;
                    y=i;
                    x=j;
                }
            }
        }
        dfs(x,y,h,w);
        cout<<ans<<endl;
    }
    
    

//////////////////////////////////////////////////////
return 0;}

C++: #include<iostream>
using namespace std;
int map[20][20];


int solve(int x,int y){
	int count = 0;
	if(map[y][x] == 0){
		count++;
		map[y][x] = 1;
		if(x-1 >= 0){
			count += solve(x-1,y);
		}
		if(y-1 >= 0){
			count += solve(x,y-1);
		}
		if(20 > x+1){
			count += solve(x+1,y);
		}
		if(20 > y+1){
			count += solve(x,y+1);
		}
	}
	return count;
}

int main(){
	int w,h;
	int startX,startY;

	while(true){
		cin >> w >> h;

		if(w == 0 && h == 0){
			break;
		}

		for(int i=0;i<20;i++){
			for(int j=0;j<20;j++){
				map[i][j] = 1;
			}
		}

		char c;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				cin >> c;
				switch(c){
				case '.':
					map[i][j] = 0;
					break;
				case '#':
					map[i][j] = 1;
					break;
				case '@':
					map[i][j] = 0;
					startX = j;
					startY = i;
					break;
				}
			}
		}

		cout << solve(startX,startY) << endl;
	}
}

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

using namespace std;

#define F first
#define S second
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define INT(a) int((a)+1e-9)
#define SUPMaxX 22
#define SUPMaxY 22

typedef pair<int,int> Pii;

int DX[4]={1,0,-1,0};
int DY[4]={0,1,0,-1};

int maxX,maxY;
char tizu[SUPMaxX][SUPMaxY];
int re;
int initX,initY;

int dfs(int px, int py){
	re++;
	FOR(i,0,4){int qx=px+DX[i]; int qy=py+DY[i];
		if(tizu[qx][qy]=='.'){
			tizu[qx][qy]='@';
			dfs(qx,qy);
		}
	}
}

int main(){
	while(true){
		cin >> maxY >> maxX;			if(!maxX) return 0;
		FOR(x,0,maxX+2) FOR(y,0,maxY+2) tizu[x][y]='#';
		FOR(x,1,maxX+1) FOR(y,1,maxY+1){
			cin>>tizu[x][y];
			if(tizu[x][y]=='@'){initX=x; initY=y;}
		}
		re=0;
		dfs(initX,initY);
		cout<<re<<endl;
	}
}

C++: #include <iostream>
using namespace std;
int w,h;
char a[21][21];
int dfs(int i,int j){
    if(i==h||j==w||i<0||j<0)return 0;
    if(a[i][j]=='#')return 0;
    int s=1;
    a[i][j]='#';
    s+=dfs(i+1,j);
    s+=dfs(i-1,j);
    s+=dfs(i,j+1);
    s+=dfs(i,j-1);
    return s;
}
int main(){
    int i,j;
    while(cin>>w>>h,w!=0&&h!=0){
    for(i=0;i<h;i++)cin>>a[i];
    for(i=0;i<h;i++)for(j=0;j<w;j++)if(a[i][j]=='@')cout<<dfs(i,j)<<endl;
}}

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

int W, H;
char b[20][20];
int ans;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

void dfs(int x, int y) {
	ans++;
	b[y][x] = '#';
	for (int i = 0; i < 4; i++) {
		int nx = x + dx[i], ny = y + dy[i];
		if (0 <= nx && nx < W && 0 <= ny && ny < H && b[ny][nx] == '.') {
			dfs(nx, ny);
		}
	}
}

int main() {
	while (cin >> W >> H , W) {
		for (int y = 0; y < H; y++) {
			for (int x = 0; x < W; x++) {
				cin >> b[y][x];
			}
		}

		ans = 0;

		for (int y = 0; y < H; y++) {
			for (int x = 0; x < W; x++) {
				if (b[y][x] == '@') {
					dfs(x, y);
				}
			}
		}
		cout << ans << endl;
	}
}

C++: #include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
#include<climits>
#include<string>
#include<set>
#include<numeric>
#include<map>
#include<iostream>
using namespace std;
#define rep(i,n) for(int i = 0;i<((int)(n));i++)
#define reg(i,a,b) for(int i = ((int)(a));i<=((int)(b));i++)
#define irep(i,n) for(int i = ((int)(n)-1);i>=0;i--)
#define ireg(i,a,b) for(int i = ((int)(b));i>=((int)(a));i--)
typedef long long ll;
typedef pair<ll, ll> mp;

int W,H,f[20][20],ans=0,sx,sy;//finded
string m[20],t,s;

void find(int x,int y){
	if(x>=W || x<0 || y>=H || y<0)return;
	t = m[x][y];
	if(t=="#")return;
	if(f[x][y]!=0)return;
	f[x][y] = 1;
	ans++;
	find(x+1,y);
	find(x-1,y);
	find(x,y+1);
	find(x,y-1);
}

int main(void){
	while(1){
		cin>>W>>H;
		if(W==0 && H==0)break;
		rep(i,H){
			cin>>s;
			rep(j,W){
				m[j][i]=s[j];
				t=m[j][i];
				if(t=="@"){
					sx=j;
					sy=i;
				}
			}
		}
		find(sx,sy);
		cout<<ans<<endl;
	}
	return 0;
}