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 <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <cmath>
#include <string>
#include <sstream>
#include <iomanip>
#include <complex>
using namespace std;
 
#define ll long long
#define vvi vector< vector<int> >
#define vi vector<int>
#define All(X) X.begin(),X.end()
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define pb push_back 
#define pii pair<int,int>
#define mp make_pair
#define pi 3.14159265359
#define shosu(X) fixed << setprecision(X)
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}

char grid[100][100];
int w,h;
int ans=0;
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
bool searched[100][100];
int a=0,b=0;

bool inrange(int x,int y){
  if(x<0||y<0||x>h||y>w) return false;
  else return true;
}

void search(int x,int y){
  if((grid[x][y]=='.'||grid[x][y]=='@')&&searched[x][y]==false&&inrange(x,y)){
	searched[x][y]=true;
	ans++;
	REP(i,4) {
	  search(x+dx[i],y+dy[i]);
	}
  }
}

int main(){
  while(1){
	cin >> w >> h;
	if(w==0&&h==0) break;
	ans = 0;
	REP(i,h){
	  REP(j,w){
		cin >> grid[i][j];
		searched[i][j]=false;
		if(grid[i][j]=='@'){
		  a=i;
		  b=j;
		}
	  }
	}
	search(a,b);

	cout<<ans<<endl;
  }
}

C++: #include <iostream>
#include <queue>
#include <map>

using namespace std;

typedef pair<int, int> P;

int main() {
    int w, h;
    int dx[] = {1, 0, -1, 0};
    int dy[] = {0, 1, 0, -1};
    char map[20][20];
    while (true) {
        int count = 1;
        queue<P> q;
        cin >> w >> h;
        if (w == 0) {
            return 0;
        }
        for (int y=0; y<h; y++) {
            for (int x=0; x<w; x++) {
                cin >> map[x][y];
                if (map[x][y] == '@') {
                    q.push(P(x, y));
                }
            }
        }
        while (!q.empty()) {
            int x = q.front().first;
            int y = q.front().second;
            q.pop();
            for (int i=0; i<4; i++) {
                int nx = x+dx[i];
                int ny = y+dy[i];
                if (nx < 0 || w <= nx || ny < 0 || h <= ny) {
                    continue;
                }
                if (map[nx][ny] != '.') {
                    continue;
                }
                map[nx][ny] = '#';
                q.push(P(nx, ny));
                count++;
            }
        }
        cout << count << endl;
    }
}

C++: #include<iostream>
#include<vector>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;

int w=1;
int h=1;
char map_data[21][21];
bool reacher[21][21];
int count=0;
char a;
int sx,sy;

void searth(int x,int y){
//cout<<w<<","<<y<<endl;
if(x<0 or x>w-1 or y<0 or y>h-1 or map_data[y][x]=='#' ){return;}


if (reacher[y][x]){return;}
//cout<<count;
reacher[y][x]=true;
count++;


searth(x+1,y);
searth(x,y+1);
searth(x-1,y);
searth(x,y-1);
//cout<<count;



}



int main(){

   

    while(w>0 and h>0){
        cin>>w>>h;
        if(w==0 and h==0){
            break;

        }
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                cin>>a;
                map_data[i][j]=a;
                
                reacher[i][j]=0;

                if(a=='@'){
                 
                    sx=j;
                    sy=i;
                } 
    
            }
        }
        //cout<<"sx"<<sx<<"sy"<<sy<<endl;
        //rep(i,h)rep(j,w)cout<<map_data[i][j];
        //rep(i,h)rep(j,w)cout<<reacher[i][j];
        searth(sx,sy);
        cout<<count<<endl;
        count=0;
        //rep(i,h)rep(j,w)cout<<map_data[i][j];
    }

   


    return 0;
}

C++: #include <iostream>
#include <math.h>
#include <algorithm>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;

int main(){
	int w,h;
	vector<string> f(30);
	vector<vector<int>> g(30,vector<int>(30,0));
	int count = 0;
	int count2 = 0;

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

		for(int i = 0;i < h; i++){
			cin >> f[i];
		}



		for(int i = 0;i < h; i++){
			for(int j = 0;j < w;j++){
				g[i][j] = 0;
			}
		}
		count = 0;
		count2 = 0;
		while(1){
			for(int i = 0; i < h; i++){
				for(int j = 0; j < w;j++){
					if(f[i][j] == '@' && count== 0){
						g[i][j] = 1;
						count2++;
					}else if(g[i][j] == 1){
						if(i != 0){
							if(f[i-1][j] == '.'){
								if(g[i-1][j] == 0){
									count2++;
								}
								g[i-1][j] = 1;

							}
						}
						if(i != h-1){
							if(f[i+1][j] == '.'){
								if(g[i+1][j] == 0){
									count2++;
								}
								g[i+1][j] = 1;
							}
						}
						if(j != 0){
							if(f[i][j-1] == '.'){
								if(g[i][j-1] == 0){
									count2++;
								}
								g[i][j-1] = 1;
							}
						}
						if(j != w-1){
							if(f[i][j+1] == '.'){
								if(g[i][j+1] == 0){
									count2++;
								}
								g[i][j+1] = 1;
							}
						}
					}else{}
				}
			}

			if(count == count2){
				cout << count << endl;
				break;
			}
			count = count2;
		}

	}
}