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<functional>
#include<algorithm>
#include<iostream>
#include<utility>
#include<string>
#include<vector>
#include<cmath>
#include<stack>
#include<queue>
#include<deque>
#include<map>
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,x,n) for(int i=x;i<n;i++)
#define all(v) (v).begin(),(v).end()
#define pf push_front
#define pb push_back
#define int long long
using namespace std;
const int INF=1000000007;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

int w,h,sx,sy,sum;
char mp[21][21];

int dfs(int y,int x){
  if(0<=y&&y<h&&0<=x&&x<w&&(mp[y][x]=='.'||mp[y][x]=='@')){
    mp[y][x]='$';
    sum++;
    rep(i,4){
      dfs(y+dy[i],x+dx[i]);
    }
  }
  return sum;
}

signed main(){
 
  while(cin>>w>>h,w){
    rep(i,h)rep(j,w){
      cin>>mp[i][j];
      if(mp[i][j]=='@'){
        sy=i;sx=j;
      }
    }
    sum=0;
    cout<<dfs(sy,sx)<<endl;
  }
  return 0;
}

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

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

int W, H, ans;
char maze[20][20];

bool check(int x, int y){
	return x >= 0 && x < W && y >= 0 && y < H && maze[y][x] != '#';
}

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

int main() {
	while(cin >> W >> H && (W || H)){
		for(int i = 0; i < H; ++i)
			for(int j = 0; j < W; ++j)
				cin >> maze[i][j];
		
		ans = 0;
		for(int i = 0; i < H; ++i){
			for(int j = 0; j < W; ++j){
				if(maze[i][j] == '@'){
					dfs(j, i);
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <queue>
using namespace std;

#define MP make_pair
typedef pair<int,int> pii;

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

int main(){
    int W,H;
    while(cin>>W>>H){
        if(W==0&&H==0) return 0;

        vector<string> t(H);
        vector<vector<bool>> used(H,vector<bool>(W,false));
        for(int i=0;i<H;i++) cin>>t[i];

        int sx,sy;
        for(int i=0;i<H;i++) for(int j=0;j<W;j++) if(t[i][j]=='@'){
            sx = i;
            sy = j;
        }
        used[sx][sy]=true;

        queue<pii> que;
        que.push(make_pair(sx,sy));

        while(que.size()!=0){
            pii p = que.front();
            que.pop();

            int x = p.first;
            int y = p.second;

            for(int i=0;i<4;i++){
                int nx = x + dx[i];
                int ny = y + dy[i];
                if(nx>=0 && nx<H && ny>=0 && ny<W && t[nx][ny]!='#' && used[nx][ny]==false){
                    used[nx][ny] = true;
                    que.push(MP(nx,ny));
                }
            }
        }

        int cnt = 0;
        for(int i=0;i<H;i++) for(int j=0;j<W;j++) if(used[i][j]==true) cnt++;
        cout << cnt << endl;
    }
}

C++: #include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <numeric>
#include <complex>
#include <sstream>
#include <fstream>
#include <functional>
#include <iomanip>
#include <cassert>
#include <iostream>
#include <iterator>
#include <algorithm>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vec;
typedef pair<int, int> P;
#define rep(n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define out(a) cout << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
#define INF 100000000

int w, h, res = 1;
char f[25][25];
int dy[] = { 1,0,-1,0 }, dx[] = { 0,1,0,-1 };
int used[25][25] = { 0 };
void dfs(int y, int x) {
	for (int i = 0; i < 4; i++) {
		int ny = y + dy[i];
		int nx = x + dx[i];
		if (0 <= ny&&ny < h && 0 <= nx&&nx < w&&f[ny][nx] == '.'&&used[ny][nx] == 0) {
			used[ny][nx] = 1;
			res++;
			dfs(ny, nx);
		}
	}
	return;
}

int main() {
	while (true) {
		cin >> w >> h;
		res = 1;
		if (w == 0 && h == 0) return 0;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				cin >> f[i][j];
				used[i][j] = 0;
			}
		}
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (f[i][j] == '@') {
					used[i][j] = 1;
					dfs(i, j);
				}
			}
		}
		cout << res << endl;
	}
	return 0;
}

C++: #include<cstdio>
#include<iostream>

using namespace std;

#define HMAX 20
#define WMAX 20

char room[HMAX+5][WMAX+5];
int W,H;

typedef struct{
	int x;
	int y;
}map;

int solve(int x,int y){
	int dx[4]={ 0, 0, 1,-1};
	int dy[4]={ 1,-1, 0, 0};
	int cnt=0;

	room[y][x]='#';
	
	for(int i=0;i<4;i++){
		if(x+dx[i]>=0&&x+dx[i]<W&&y+dy[i]>=0&&y+dy[i]<H){
			if(room[y+dy[i]][x+dx[i]]=='.'){
				cnt+=solve(x+dx[i],y+dy[i]);
			}
		}
	}

	return cnt+1;
}

int main(void){

	map start;

	while(1){
		cin>>W>>H;
		if(W==0&&H==0) break;
		for(int i=0;i<H;i++){
			for(int j=0;j<W;j++){
				cin>>room[i][j];
				if(room[i][j]=='@') start.x=j,start.y=i;
			}
		}
		cout<<solve(start.x,start.y)<<endl;
	}
	
	return 0;
}