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 <cstdint>
#include <array>

using namespace std;


typedef std::array<uint32_t, 2> Idx;


uint32_t checkAndSetIdx(vector<string>& mp, vector<Idx>& stack, uint32_t i, uint32_t j)
{
    if(i > mp.size()-1 || j > mp.at(0).size()-1)
        return 0;

    if(mp[i][j] == '.'){
        mp[i][j] = 'x';
        stack.push_back(Idx({i, j}));
        return 1;
    }

    return 0;
}


int main()
{
    while(1)
    {
        uint32_t h, w;
        cin >> w >> h;

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


        Idx iniPos;

        vector<string> mp;
        for(uint32_t i = 0; i < h; ++i){
            mp.push_back([](){string str; cin >> str; return str;}());

            for(uint32_t j = 0; j < w; ++j){
                if(mp[i][j] == '@'){
                    iniPos = Idx({i, j});
                    mp[i][j] = 'x';
                }
            }
        }


        vector<Idx> stack;
        stack.push_back(iniPos);
        uint32_t cnt = 1;


        while(stack.size() != 0){
            auto nowPos = stack.at(stack.size() - 1);
            stack.pop_back();
            const auto i = nowPos[0],
                       j = nowPos[1];

            cnt += checkAndSetIdx(mp, stack, i+1, j);
            cnt += checkAndSetIdx(mp, stack, i-1, j);
            cnt += checkAndSetIdx(mp, stack, i, j+1);
            cnt += checkAndSetIdx(mp, stack, i, j-1);
        }

        cout << cnt << endl;
    }

    return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;
#define ll long long

#define rep(i, a) for (int (i) = 0; (i) < (int) (a); (i)++)
#define reps(i, a, b) for (int (i) = (int) (a); (i) < (int) (b); (i)++)
#define rrep(i, a) for (int (i) = (int) a-1; (i) >= 0; (i)--)
#define rreps(i, a, b) for (int (i) = (int) (a)-1; (i) >= (int) (b); (i)--)
#define MP(a, b) make_pair((a), (b))
#define PB(a) push_back((a))
#define all(v) (v).begin(), (v).end()
#define PERM(v) next_permutation(all(v))
#define UNIQUE(v) sort(all(v));(v).erase(unique(all(v)), v.end())
#define CIN(type, x) type x;cin >> x
#define TRUE__ "Yes"
#define FALSE__ "No"
#define PRINT(f) if((f)){cout << (TRUE__) << endl;}else{cout << FALSE__ << endl;}

#ifdef LOCAL
	#define lcout(a) cout << a;
	#define lcoutln(a) cout << a << endl;
	#define lcerr(a) cerr << a;
	#define lcerrln(a) cerr << a << endl;
#else
	#define lcout(a) 
	#define lcoutln(a) 
	#define lcerr(a) 
	#define lcerrln(a) 
#endif

int W, H;
bool m[20][20];
bool hou[20][20];

int dfs(int r, int c)
{
	if (r < 0 || c < 0 || r >= H || c >= W) return 0;
	if (hou[r][c] || !m[r][c]) return 0;
	hou[r][c] = true;
	int res = 1;
	int dr[] = {-1, 0, 0, 1};
	int dc[] = { 0,-1, 1, 0};
	rep(i, 4) res += dfs(r+dr[i], c+dc[i]);
	return res;
}

signed main()
{
	cin >> W >> H;
	while (W) {
		int r=0, c=0;
		rep(i, H) rep(j, W) {
			CIN(char, t);
			if (t == '#') m[i][j] = false;
			else {
				m[i][j] = true;
				if (t == '@') {
					r = i;
					c = j;
				}
			}
		}
		cout << dfs(r, c) << endl;
		cin >> W >> H;
		rep(i, 20) rep(j, 20) hou[i][j] = false;
	}
}

C++: #include<bits/stdc++.h>
using namespace std;

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

int main(){
    int w,h;
    while(cin>>w>>h,w){
        vector<string> s(h);
        for(int i=0;i<h;i++)
            cin>>s[i];
        int x,y;
        for(int i=0;i<h;i++)
            for(int j=0;j<w;j++)
                if(s[i][j]=='@')
                    x=j,y=i;
        vector<vector<int>> able(h,vector<int>(w,0));
        able[y][x]=1;
        for(int k=0;k<w*h;k++)
            for(int i=0;i<h;i++)
                for(int j=0;j<w;j++)
                    if(able[i][j]){
                        for(int r=0;r<4;r++)
                            if(0<=i+dy[r] && i+dy[r]<h && 0<=j+dx[r] && j+dx[r]<w && s[i+dy[r]][j+dx[r]]!='#')
                            able[i+dy[r]][j+dx[r]]=1;



                    }
        int res=0;
        for(int i=0;i<h;i++)
            for(int j=0;j<w;j++)
                res+=able[i][j];
        cout<<res<<endl;


    }


    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define REP(i,n) for(int i=0;i<(n);i++)
#define REAP(i,a,n) for(int i=(a);i<(n);i++)
#define YES cout<<"Yes"<<endl
#define NO cout<<"No"<<endl
#define fr first
#define sc second
#define pb push_back
#define All(v) v.begin(),v.end()
typedef long long ll;
typedef pair<int,int> P;

int h,w;
string table[21];
int dp[21][21];
int ans;

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

void puts_max(){
    REP(i,h+1){
        REP(j,w+1){
            dp[i][j]=INT_MAX;
        }
    }
}

void solve(int y,int x){
    ans++;
    dp[y][x]=0;
    REP(i,4){
        if(y+dy[i]>=0 && y+dy[i]<h && x+dx[i]>=0 && x+dx[i]<w){
            if(dp[y+dy[i]][x+dx[i]]!=0 && table[y+dy[i]][x+dx[i]]!='#'){
                solve(y+dy[i],x+dx[i]);
            }
        }
    }
}

int main(){
    P st;
    while(cin>>w>>h,w){
        ans=0;
        REP(i,h)cin >> table[i];

        //cout << 1 << endl;
        REP(i,h){
            REP(j,w){
                if(table[i][j]=='@'){
                    st.fr=i;
                    st.sc=j;
                }
            }
        }
        puts_max();
        solve(st.fr,st.sc);
        cout << ans << endl;
    }
}