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 <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;
    for(b=0; b<10000; b++){
        cin >> x;
        cin >> y;
        if(x==0&&y==0) break;
        if(x>20) x=20;
        if(y>20) y=20;
        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;
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef  long long ll;
using vll=vector<ll>;
using vvll=vector<vll>;
using vi=vector<int>;
using vvi=vector<vector<int>>;
using vb=vector<bool>;
using pii=pair<int,int>;
using vpii=vector<pair<int,int>>;
using plli=pair<ll,int>;
using vplli=vector<pair<ll,int>>;
using pllll=pair<ll,ll>;
using vpllll=vector<pair<ll,ll>>;

template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b;return true;}return false;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b;return true;}return false;}

#define rep(i, begin_i, end_i) for (ll i = (ll)begin_i; i < (ll)end_i; i++)
//試験導入
#define irep(i, end_i, begin_i) for (ll i = (ll)begin_i-1; i >= (ll)end_i; i--)

long long INF = 1LL<<60;
int main( ){
    while(true){
        int H,W;
        cin>>W>>H;
        if(H==0&&W==0)
            break;
        string s[H];
        int si,sj;
        rep(i,0,H){
            cin>>s[i];
            rep(j,0,W){
                if(s[i][j]=='@')
                    si=i,sj=j;
            }
        }
        queue<pii> q;
        q.emplace(si,sj);
        vector<vb> is_v(H,vb(W,false));
        is_v[si][sj]=true;
        int cnt=1;
        while(!q.empty()){
            pii e=q.front();
            q.pop();
            int i=e.first,j=e.second;
            int di[]={1,-1,0,0};
            int dj[]={0,0,1,-1};
            rep(w,0,4){
                int ni=i+di[w],nj=j+dj[w];
                if((ni<0||ni>H-1)||(nj<0||nj>W-1))
                    continue;
                if(is_v[ni][nj])
                    continue;
                is_v[ni][nj]=true;
                if(s[ni][nj]=='.'){
                    cnt++;
                    q.emplace(ni,nj);
                }
            }
        }
        cout<<cnt<<endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define N_PRIME 1000000007
#define ll int64_t

void calc(){
    int w,h;
    int board[32][32];
    bool moved[32][32];
    int px,py;
    
    cin >> w >> h;
    if( w == 0 && h == 0)exit(0);
    for(int j = 0 ; j < h ;j++ ){
        string s;
        cin >> s;
        for(int i = 0 ; i < w ; i++ ){
            moved[i][j] = false;
            
            if( s[i] == '.' ){
                board[i][j] = 1;
            }else if(s[i] == '#' ){
                board[i][j] = 2;
            }else if(s[i] == '@' ){
                px = i;
                py = j;
                board[i][j] = 1;
            }
        }
    }
    
    int count = 0;
    queue<int> xs;
    queue<int> ys;
    xs.push(px);
    ys.push(py);
    
    while(xs.size()>0){
        px = xs.front();
        py = ys.front();
        xs.pop();
        ys.pop();
        
        if( px < 0 || w <= px || py < 0 || h <= py )
            continue;
        
        if( moved[px][py] )
            continue;
        moved[px][py] = true;
        
        if( board[px][py] == 2 )
            continue;

        xs.push(px + 1 );
        ys.push(py + 0 );
        
        
        xs.push(px + 0 );
        ys.push(py + 1 );
        
        
        xs.push(px - 1 );
        ys.push(py + 0 );
        
        
        xs.push(px + 0 );
        ys.push(py - 1 );
        count++;
    }
    
    cout << count << endl;
}

int main(void){
    while(true)calc();
    return 0;
}