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;

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

int func(int x, int y, vector<vector<int>> &map){

    if(map[y][x] == -1)
        return 0;

    if(map[y][x] == 1)
        return 0;

    map[y][x] = 1;

    int ret = 0;

    for(int i = 0; i < 4; i++){
        ret += func(x + dx[i], y + dy[i], map);
    }

    return ret + 1;

}

int main(){

    cin.tie(0);
    ios::sync_with_stdio(false);

    while( true ){

        int w, h;
        cin >> w >> h;
        
        if(w == 0 && h == 0)
            break;

        int x,y;
        vector<vector<int>> map(h + 2, vector<int>(w + 2, -1));
        for(int i = 0; i < h; i++){
            string s;
            cin >> s;
            for(int j = 0; j < w ; j++){
                if(s[j] == '.')
                    map[i + 1][j + 1] = 0;
                else if(s[j] == '#')
                    map[i + 1][j + 1] = -1;
                else {
                    map[i + 1][j + 1] = 0;
                    x = j + 1;
                    y = i + 1;
                }
            }
        }

        cout << func(x, y, map) << endl;
        /*
        for(auto &x:map){
            for(auto &y: x)
                cout << y << ' ';
            cout << endl;
        }
        */

    }

    return 0;

}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <ctime>
#define INF 999999999
#define mod 1000000007

#define rep(i,n) for(int i=0;i<int(n);i++)
#define rrep(i,n) for(int i=(n);i>=0;i--)
#define REP(i,a,b) for(int i=(a);i<int(b);i++)
#define all(x) (x).begin(),x.end()
#define pb push_back
#define MOD(x) (x%(mod))
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int,int> pi;

int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
bool debug=false;
/*---------------------------------------------------*/

char maze[22][22];
int sx,sy;

void make_maze(int w,int h){
  rep(i,22)rep(j,22)maze[i][j]='#';
  rep(i,h)rep(j,w){
    cin>>maze[i][j];
    if(maze[i][j]=='@'){sy=i;sx=j;}
  }
  //rep(i,22){rep(j,22)cout<<maze[i][j];cout<<endl;}
}

void solve(int w,int h){
  queue<int> que;
  int cnt=0;
  que.push(sx);que.push(sy);
  while(que.size()){
    int x=que.front();que.pop();
    int y=que.front();que.pop();
    rep(i,4){
      if(maze[y+dy[i]][x+dx[i]]=='.'&&0<=x+dx[i]&&x+dx[i]<w&&0<=y+dy[i]&&y+dy[i]<h){
	cnt++;
	que.push(x+dx[i]);que.push(y+dy[i]);
	maze[y+dy[i]][x+dx[i]]='#';
      }
    }
  }
  cout<<cnt+1<<endl;
}

int main(){
  int w,h;
  while(cin>>w>>h){
    if(w==0&&h==0)break;
    make_maze(w,h);
    solve(w,h);
  }
}

C++: #include<bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
int A[30][30];
int x,y;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int dfs(int x,int y){
    A[x][y]=1;
    int ans=0;
    REP(i,4){
        int xx=dx[i]+x;
        int yy=dy[i]+y;
        if(A[xx][yy]==0){
           ans+=dfs(xx,yy);
        }
    }
    return ans+1;
}

int main(){
    int W,H;
    while(cin>>W>>H,W+H){
        REP(i,30){
            REP(j,30){
                A[i][j]=1;
            }
        }
        REP(i,H){
            string s;
            cin>>s;
            REP(j,W){
                if(s[j]=='.')A[i+1][j+1]=0;
                if(s[j]=='@'){
                   A[i+1][j+1]=0;
                   x=i+1;
                   y=j+1;
                }
            }
        }
        cout<<dfs(x,y)<<endl;
    }
    return 0;
}

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

int h,w;
char mp[111][111];
int xx[4]={0,1,0,-1};
int yy[4]={-1,0,1,0};
int sx,sy;

int tile(int y,int x){
    if(y<h&&x<w&&y>=0&&x>=0&&mp[y][x]!='#'){
        mp[y][x]='#';
        int t=0;
        for(int i=0;i<4;i++){
            t+=tile(y+yy[i],x+xx[i]);
        }
        return t+1;
    }
return 0;
}

int main(){
    
    while(1){
    
        scanf(" %d %d",&w,&h);
        
        if(w==0&&h==0)break;
        
        for(int i=0;i<h;i++){
            scanf(" %s",mp[i]);
            for(int j=0;j<w;j++){
                if(mp[i][j]=='@'){
                    sx=j;
                    sy=i;
                }
            }
        }
        
        cout << tile(sy,sx) << endl;
        
    }
    
    return 0;
    
}

C++: #include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;

struct UF {
  vector<int> p;
  UF(int N):p(N,-1) {}
  int rt(int x){return p[x]<0?x:p[x]=rt(p[x]);}
  int s(int x){return -p[rt(x)];}
  void cat(int x,int y) {
    if((x=rt(x))==(y=rt(y))) return;
    if(p[x]<p[y])swap(x,y);
    p[y]+=p[x];
    p[x]=y;
  }
};

int main() {
  for(int tci = 0; ; tci++) {
    int W, H; scanf("%d%d", &W, &H);
    if(W==0) break;
    static char tbl[30][30];
    for(int y = 0; y < H; y++) scanf(" %s", tbl[y]);
    UF uf(W*H);
    int k = 0;
    for(int y = 0; y < H; y++) {
      for(int x = 0; x < W; x++) {
        if(tbl[y][x]=='#') continue;
        if(y>0 && tbl[y-1][x]!='#') uf.cat(y*W+x,(y-1)*W+x);
        if(x>0 && tbl[y][x-1]!='#') uf.cat(y*W+x,y*W+(x-1));
        if(tbl[y][x]=='@')k=y*W+x;
      }
    }
    printf("%d\n", uf.s(k));
  }
  return 0;
}