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++: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define EPS (1e-7)
#define INF (1e9)
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
const double PI = acos(-1);
const ll MOD = 1000000007;
template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}
 
template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}
///////////////////////////////////////////////////////////////

int H,W;
vector<vector<char>> board;
vector<vector<bool>> check;

const vector<int> dy = {-1,0,0,1};
const vector<int> dx = {0,-1,1,0};

void dfs(int y, int x) {
    check[y][x] = true;
    rep(i,4) {
        int ny = y + dy[i];
        int nx = x + dx[i];
        if (board[ny][nx] != '#' && !check[ny][nx]) dfs(ny,nx);
    }
    return;
}

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    while(true) {
        cin >> W >> H;
        if (W == 0 && H == 0) break;
        board.assign(H+2, vector<char>(W+2,'#'));
        rep(i,H) rep(j,W) cin >> board[i+1][j+1];
      
        int sy,sx;
        rep(i,H) {
            rep(j,W) {
                if (board[i+1][j+1] == '@') {
                    sy = i+1;
                    sx = j+1;
                }
            }
        }

        check.assign(H+2, vector<bool>(W+2,false));
        dfs(sy,sx);

        int count = 0;
        rep(i,H) rep (j,W) if (check[i+1][j+1]) count++;
        cout << count << endl;
    }
}

C++: #include <iostream>
#include <cstdio>
//#include <cstdlib>
#include <cmath>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
//#include <stack>
//#include <limits>
#include <sstream>
//#include <functional>
using namespace std;

#define len(array)  (sizeof (array) / sizeof *(array))
#define rep(i, s, e) for(int i = s;i < e;i++)
#define rrep(i, e, s) for(int i = e;s <= i;i--)
#define vrange(v) v.begin(), v.end()
#define vrrange(v) v.rbegin(), v.rend()
#define vsort(v) sort(vrange(v))
#define vrsort(v) sort(vrrange(v))
#define arange(a) a, a + len(a)
#define asort(a) sort(arange(a))
#define arsort(a, t) sort(arange(a), greater<t>())
#define afill(a, v) fill(arange(a), v)
#define afill2(a, v, t) fill((t *)a, (t *)(a + len(a)), v)
#define fmax(a, b) (a < b? b : a)
#define fmin(a, b) (a > b? b : a)
#define fabs(a) (a < 0? -a : a)
typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
const int INF = (int)1e9;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-10;
const int dx[] = {1, -1, 0, 0, 1, -1, -1, 1};
const int dy[] = {0, 0, 1, -1, -1, -1, 1, 1};
//const int weight[] = {0,1,10,100,1000,10000,100000,1000000,10000000};
//priority_queue< int, vector<int>, greater<int> > q;

void doIt(){
	int w, h, sx, sy;
	string f[21];
	bool visited[21][21];
	queue<P> q;
	while(true){
		cin >> w >> h;
		if(w + h == 0) break;
		rep(i, 0, h){
			cin >> f[i];
		}
		afill2(visited, false, bool);
		rep(i, 0, h){
			rep(j, 0, w){
				if(f[i][j] == '@'){
					sx = j;
					sy = i;
					break;
				}
			}
		}
		q.push(P(sy, sx));
		while(!q.empty()){
			P e = q.front();
			int yy = e.first, xx = e.second;
			// cout << xx << ", " << yy << endl;
			q.pop();
			if(0 <= yy && yy < h && 0 <= xx && xx < w && visited[yy][xx] == false){
				visited[yy][xx] = true;
				// cout << xx << ", " << yy << endl;
				if(f[yy][xx] == '@' || f[yy][xx] == '.' ){
					rep(i, 0, 4){
						q.push(P(yy + dy[i], xx + dx[i]));
					}
				}
			}
		}
		int ans = 0;
		rep(i, 0, h){
			rep(j, 0, w){
				if(visited[i][j] && f[i][j] == '.'){
					ans++;
				}
			}
		}
		cout << ans + 1 << endl;

	}	

}

int main() {
	doIt();
	return 0;
}

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

class DoubleArray {
    int X,Y;
    int *top;
    public:
    DoubleArray(int *p,int x,int y);
    int *operator[](int i);
    int getx();
    int gety();
};

DoubleArray::DoubleArray(int *p,int x,int y){
    top = p;
    X = x;
    Y = y;
}

int *DoubleArray::operator[](int i){
    return top + Y*i;
}
int DoubleArray::getx(){
    return X;
}
int DoubleArray::gety(){
    return Y;
}

int calc(DoubleArray &map){
    int i,j,a=0;
    int x = map.getx();
    int y = map.gety();
    queue<int> q;
    for(i=0;i<x;i++){
        for(j=0;j<y;j++){
            if(map[i][j]==2){
                q.push(i);
                q.push(j);
                a=1;
                map[i][j]=0;
            }
        }
    }
    while(!q.empty()){
        i=q.front();
        q.pop();
        j=q.front();
        q.pop();
        if(i>0) if(map[i-1][j]==1){
            q.push(i-1);
            q.push(j);
            a++;
            map[i-1][j]=0;
        }
        if(j>0) if(map[i][j-1]==1){
            q.push(i);
            q.push(j-1);
            a++;
            map[i][j-1]=0;
        }
        if(i<x-1) if(map[i+1][j]==1){
            q.push(i+1);
            q.push(j);
            a++;
            map[i+1][j]=0;
        }
        if(j<y-1) if(map[i][j+1]==1){
            q.push(i);
            q.push(j+1);
            a++;
            map[i][j+1]=0;
        }
    }
    return a;
}

int main(){
    int i,j,x,y,a[10000],b = 0;
    string c;
    while(114514){
        cin >> x;
        cin >> y;
        if(x==0&&y==0) break;
        if(x<1||y<1) continue;
        int m[x][y];
        DoubleArray map(&m[0][0],x,y);
        for(j=0;j<y;j++){
            for(i=0;i<x;i++){
                cin >> c;
                if(c=="#") map[i][j]=0;
                if(c==".") map[i][j]=1;
                if(c=="@") map[i][j]=2;
            }
        }
        a[b++] = calc(map);
    }
    for(i=0;i<b;i++){
        cout << a[i] << endl;
    }
    return 0;
}