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 <algorithm>
#include <cmath>
#include <string>
#include <queue>
#include <utility>

int dir[] = {0, -1, 0, 1, 0};

using namespace std;
int main()
{
    while (true) {
        int w, h;
        cin >> w >> h;
        if (w == 0 and h == 0) {
            break;
        }
        vector<vector<int>> tile(h, vector<int>(w));
        vector<vector<int>> check(h, vector<int>(w, 0));
        int starti = 0;
        int startj = 0;
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                char c;
                cin >> c;
                if (c == '.') {
                    tile[i][j] = 0;

                } else if (c == '#') {
                    tile[i][j] = -1;
                } else {
                    tile[i][j] = 0;
                    starti = i;
                    startj = j;
                }
            }
        }

        queue<pair<int, int>> q;
        q.push(make_pair(starti, startj));
        while (not q.empty()) {
            const int pi = q.front().first;
            const int pj = q.front().second;
            check[pi][pj] = 1;
            q.pop();
            for (int i = 0; i < 4; i++) {
                const int i_ = pi + dir[i];
                const int j_ = pj + dir[i + 1];
                if (i_ >= 0 and i_ < h and j_ >= 0 and j_ < w and tile[i_][j_] == 0 and check[i_][j_] == 0) {
                    q.push(make_pair(i_, j_));
                }
            }
        }
        int cnt = 0;
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (check[i][j] == 1) {
                    cnt++;
                }
            }
        }
        cout << cnt << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#define INF 999999
#define fst first
#define snd second
#define MAX 32
using namespace std;
typedef pair<int,int> P;
typedef unsigned long long ll;

int w,h,d[MAX][MAX],dx[4]={1,0,-1,0},dy[4]={0,1,0,-1},sx,sy;
char maze[MAX][MAX];
	
void bfs(){
	int i,j;
	queue<P> que;
	for(i=0;i<h;i++)for(j=0;j<w;j++)d[i][j]=0;
	que.push(P(sx,sy));
	while(que.size()){
		P p=que.front();
		que.pop();
		if(maze[p.fst][p.snd]=='#'||d[p.fst][p.snd])continue;
		d[p.fst][p.snd]=1;
		for(i=0;i<4;i++){
			int nx=p.fst+dx[i],ny=p.snd+dy[i];
			if(0<=nx&&nx<h&&0<=ny&&ny<w){
				que.push(P(nx,ny));
			}
		}
	}
}
	
int main(){
	int i,j,t;
	while(1){
		scanf("%d %d",&w,&h);
		getchar();
		if(w==0&&h==0)break;
		t=0;
		for(i=0;i<MAX;i++)for(j=0;j<MAX;j++)maze[i][j]='\0';
		for(i=0;i<h;i++){
			for(j=0;j<w;j++){
				scanf("%c",&maze[i][j]);
				if(maze[i][j]=='@'){
					sx=i;
					sy=j;
				}
			}
			getchar();
		}
		bfs();
		for(i=0;i<h;i++)for(j=0;j<w;j++)t+=d[i][j];
		printf("%d\n",t);
	}
}

C++: #define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <math.h>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <math.h>
#include <iostream>
#include <random>
#include<map>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <cassert>
#include<fstream>
#include <unordered_map>
#include <cstdlib>
#include <complex>
using namespace std;
#define Ma_PI 3.141592653589793
#define eps 0.00000001
#define LONG_INF 1e9
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007
#define MOD 998244353
#define REP(i,n) for(long long i = 0;i < n;++i)    
#define seg_size 524288
long long gcd(long long a, long long b) {
	if (b == 0) return a;
	return gcd(b, a % b);
}
int main() {
	while (true) {
		int w, h;
		cin >> w >> h;
		if (w == 0) return 0;
		vector<string> s;
		int xe[4] = { 1,-1,0,0 };
		int ye[4] = { 0,0,1,-1 };
		queue<pair<int, int>> next;
		int visited[21][21] = {};
		REP(i, h) {
			string a;
			cin >> a;
			s.push_back(a);
			REP(q, a.length()) {
				if (a[q] == '@') {
					next.push(make_pair(i, q));
					visited[i][q] = 1;
				}
			}
		}
		int ans = 1;
		while (next.empty() == false) {
			pair<int, int> now = next.front();
			next.pop();
			REP(q, 4) {
				int now_x = now.first + xe[q];
				int now_y = now.second + ye[q];
				if (now_x >= 0 && now_x < h && now_y >= 0 && now_y < w) {
					if (visited[now_x][now_y] == 0 && s[now_x][now_y] != '#') {
						visited[now_x][now_y] = 1;
						next.push(make_pair(now_x, now_y));
						ans++;
					}
				}
			}
		}
		cout << ans << endl;
	}
}

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

int moved = 0;
int w, h;
char myMap[20][20];

bool safe(int x, int y){
    return x < h && y < w;
}

void move(int x, int y){
    myMap[x][y] = '#';
    moved++;
    if (myMap[x+1][y] == '.' && safe(x+1, y)) move(x+1, y);
    if (myMap[x-1][y] == '.' && safe(x-1, y)) move(x-1, y);
    if (myMap[x][y+1] == '.' && safe(x, y+1)) move(x, y+1);
    if (myMap[x][y-1] == '.' && safe(x, y-1)) move(x, y-1);
    return;
}

int main(){
    while (1){
	cin >> w >> h;
	if (w == 0 && h == 0) break;

	int initX, initY;
	
	for (int i = 0; i < h; i++){
	    for (int j = 0; j < w; j++){
		cin >> myMap[i][j];
		if (myMap[i][j] == '@'){
		    initX = i; initY = j;
		}
	    }
	}
	
	move(initX, initY);
	
	cout << moved << endl;
    }
}