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 <bits/stdc++.h>

using namespace std;

#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define INF 114514810
#define ll long long
#define ALL(a) (a).begin(),(a).end()
#define SORT(v) sort(ALL(v))
//#define scanf scanf_s
typedef pair<int, int> P;

int w, h;
char fld[30][30];

int di[] = { -1, 0, 0, 1 }; int dj[] = { 0, -1, 1, 0 };
int main()
{
	while (cin >> w >> h, w)
	{
		queue<P> que;
		REP(i, h)REP(j, w)
		{
			cin >> fld[i][j];
			if (fld[i][j] == '@') que.push(P(i, j));
		}
		int cnt = 1;
		while (que.size())
		{
			int x = que.front().first; int y = que.front().second;
			que.pop();
			REP(k, 4)
			{
				int nx = x + di[k]; int ny = y + dj[k];
				if (nx >= 0 && nx < h&&ny >= 0 && ny < w&&fld[nx][ny] == '.')
				{
					que.push(P(nx, ny));
					cnt++;
					fld[nx][ny] = '@';
				}
			}
		}
		cout << cnt << endl;
	}
	return 0;
}

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

int w, h;
int vx[4] = { 1, 0, -1, 0 };
int vy[4] = { 0, 1, 0, -1 };
bool used[25][25];
char c[25][25];

int dfs(int x, int y){
    int ret = 1;
    for (int i = 0; i < 4; i++){
        int nextx = x + vx[i];
        int nexty = y + vy[i];

        if (0 < nextx && nextx < w + 1 && 0 < nexty && nexty < h + 1 && c[nextx][nexty] == '.' && !used[nextx][nexty]){
            used[nextx][nexty] = true;
            ret += dfs(nextx, nexty);
        }
    }

    return ret;
    
}

int main(void){

    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++){
                used[i][j] = false;
            }
        }

        int sx, sy;

        for (int i = 1; i <= h; i++){
            for (int j = 1; j <= w; j++){
                cin >> c[j][i];
                if (c[j][i] == '@'){
                    sx = j; sy = i;
                }
            }
        }

        cout << dfs(sx, sy) << endl;
    }
 

}

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

char A[20][20];
bool V[20][20];
int count,H,W;
void check(int x,int y){
    if(0<=x-1){
        if(A[x-1][y]=='.'&&!V[x-1][y]){
            count++;
            V[x-1][y]=true;
            check(x-1,y);
        }
    }
    if(x+1<H){
        if(A[x+1][y]=='.'&&!V[x+1][y]){
            count++;
            V[x+1][y]=true;
            check(x+1,y);
        }
    }
   if(0<=y-1){
        if(A[x][y-1]=='.'&&!V[x][y-1]){
            count++;
            V[x][y-1]=true;
            check(x,y-1);
        }
    }   
    if(y+1<W){
        if(A[x][y+1]=='.'&&!V[x][y+1]){
            count++;
            V[x][y+1]=true;
            check(x,y+1);
        }
    }
    return;
    }
int main(){
    int x,y;
    count=1;
    while(true){
        cin>>W>>H;
        if(W==0&&H==0){
            break;
        }else{
            for(int i=0;i<H;i++){
                for(int j=0;j<W;j++){
                    cin>>A[i][j];
                    if(A[i][j]=='@'){
                        x=i;
                        y=j;
                    }
                }
            }
            for(int i=0;i<H;i++){
                for(int j=0;j<W;j++){
                    V[i][j]=false;
                    }
                }
            }
        check(x,y);
        cout<<count<<endl;
        count=1;
    }
    return 0;
}

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

const int INF = 10000009;
char maze[20][20];
int W, H, sx, sy;
int ans = 1;
int d[20][20];
int dx[4] = { 1, 0, -1, 0 }, dy[4] = { 0, 1, 0, -1 };

void bfs() {
	queue< pair<int, int> > que;
	for (int i = 0; i < H; i++)for (int j = 0; j < W; j++) d[i][j] = INF;
	que.push(make_pair(sx, sy));
	d[sy][sx] = 0;
	while (que.size()) {
		pair<int, int> p = que.front(); que.pop();
		for (int i = 0; i < 4; i++) {
			int nx = p.first + dx[i], ny = p.second + dy[i];
			if (nx >= 0 && nx < W && ny >= 0 && ny < H && maze[ny][nx] != '#') {
				if (d[ny][nx] == INF) {
					que.push(make_pair(nx, ny));
					d[ny][nx] = 0;
					ans++;
				}
			}
		}
	}
}
int main() {
	while (cin >> W >> H && W + H) {
		ans = 1;
		for (int i = 0; i < H; i++) {
			for (int j = 0; j < W; j++) {
				cin >> maze[i][j];
				if (maze[i][j] == '@') {
					sy = i; sx = j;
				}
			}
		}
		bfs();
		cout << ans << endl;
	}
}

C++: #include <sstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <utility>
#include <set>
#include <cctype>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
using namespace std;
#define INF 100000000
typedef long long ll;

int W, H;
char field[32][32];
bool used[32][32];

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

void dfs(int x, int y) {
    used[x][y] = true;
    for (int i = 0; i < 4; i++) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        if (0 <= nx && nx < H && 0 <= ny && ny < W && field[nx][ny] != '#' && used[nx][ny] == false) {
            dfs(nx, ny);
        }
    }
}

int main(void) {
    while (1) {
        cin >> W >> H;
        for (int i = 0; i < 32; i++)
            for (int j = 0; j < 32; j++)
                used[i][j] = false;
        if (W == 0 && H == 0) break;
        int sx, sy;
        for (int i = 0; i < H; i++) {
            for (int j = 0; j < W; j++) {
                cin >> field[i][j];
                if (field[i][j] == '@') {
                    sx = i; sy = j;
                }
            }
        }
        used[sx][sy] = true;
        dfs(sx, sy);
        int ret = 0;
        for (int i = 0; i < H; i++) {
            for (int j = 0; j < W; j++) {
                if (used[i][j]) ret++;
            }
        }
        cout << ret << endl;
    }
    return 0;
}