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<map>
#include<set>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<complex>
#include<numeric>
#include<bitset>
#define INF 1001001001
#define EPS 0.000000001
#define x first
#define y second

using namespace std;
typedef vector<int> vint;
typedef vector<vint> vvint;
typedef pair<int,int> pint;

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

int main(){
	int i,j,fx,fy;
	int w,h;
	char m[30][30];
	while(1){
		cin >> w >> h;
		if(w==0 && h==0) break;
		for(i=0;i<h;i++){
			for(j=0;j<w;j++){
				cin >> m[i][j];
				if(m[i][j]=='@'){
					fx=i;
					fy=j;
				}
			}
		}
		
		int ans=1;
		queue<pint> q;
		q.push(pint(fx,fy));
		while(!q.empty()){
			pint c = q.front(); q.pop();
			for(i=0;i<4;i++){
				int nx = c.x + dx[i];
				int ny = c.y + dy[i];
				if(0 <= nx && nx < h && 0 <= ny && ny < w && m[nx][ny]=='.'){
					m[nx][ny] = '@';
					q.push(pint(nx,ny));
					ans++;
				}
			}
		}
		cout << ans << endl;
	}
}

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

int h,w;
string str[30];

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

int dfs(int y,int x){
    str[y][x] = '#';
    int res = 0;
    for(int i=0;i<4;i++){
        if(y+dy[i]<0 || y+dy[i]>=h || x+dx[i]<0 || x+dx[i]>=w || str[y+dy[i]][x+dx[i]]!='.') continue;
        res += dfs(y+dy[i],x+dx[i])+1;
    }
    return res;
}

int main()
{
    
    while(cin >> w >> h,w!=0&&h!=0){
        for(int i=0;i<h;i++) cin >> str[i];
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(str[i][j] == '@'){
                    cout << dfs(i,j)+1 << endl;
                }
            }
        }
    }

    return(0);
}

C++: #include<bits/stdc++.h>
#define rep(i,n) for(int i=0; i<n; i++)
using namespace std;
char field[21][21];
char visit[21][21];
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
int W,H;
void dfs(int y, int x){
  visit[y][x] = 1;
  rep(i,4){
    int ny = y+dy[i];
    int nx = x+dx[i];
    if(field[ny][nx] == '.'
       && visit[ny][nx] == 0
       && nx >= 0
       && nx < W
       && ny >= 0
       && ny < H){
          dfs(ny,nx);
        }
  }
}
int main(){
  while(cin >> W >> H && W){
    int y,x;
    rep(i,H){
      rep(j,W){
        cin >> field[i][j];
        if(field[i][j] == '@') y=i,x=j;
      }
    }
    memset(visit,0,sizeof(visit));
    dfs(y,x);
    int ans = 0;
    rep(i,H){
      rep(j,W){
        ans += visit[i][j];
      }
    }
    cout << ans << endl;
  }
  return 0;
}

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

#define fs first
#define sc second
#define pb emplace_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

using pii = pair<int, int>;
using vi = vector<int>;
using lint = long long;

const int inf = 1001001001;
const lint linf = 1001001001001001001ll;
const int mod = 1e9 + 7;
const int dx[]{0, 1, 0, -1, -1, -1, 1, 1}, dy[]{1, 0, -1, 0, -1, 1, -1, 1};

template<typename T> inline bool chmin(T &a, T b) { if (a > b) { a = b; } return a > b; }
template<typename T> inline bool chmax(T &a, T b) { if (a < b) { a = b; } return a < b; }
template<typename T> inline void print(const T &x, string s = "\n") { cout << x << s; }
template<typename T> inline void print(const vector<T> &v, string s = " ")
{ if (!v.size()) puts(""); rep(i, v.size()) cout << v[i] << (i + 1 == v.size() ? "\n" : s); }
inline bool inside(int y, int x, int H, int W) { return 0 <= y && y < H && 0 <= x && x < W; }
inline lint in() { lint x; std::cin>>x; return x; }

struct UnionFind {
	vector<int> p, r;

	UnionFind() {}
	UnionFind(int size) { init(size); }

	void init(int size) {
		p.resize(size);
		r.resize(size);
		for (int i = 0; i < size; ++i) {
			p[i] = i; r[i] = 1;
		}
	}

	int root(int x) {
		if (p[x] == x) return x;
		else return p[x] = root(p[x]);
	}

	void unite(int x, int y) {
		x = root(x); y = root(y);

		if (x == y) return;

		if (r[x] > r[y]) swap(x, y);
		r[x] += r[y];
		p[y] = x;
	}

	bool is_root(int x) {
		return root(x) == x;
	}

	bool same(int x, int y) {
		return root(x) == root(y);
	}

	int element_count(int x) {
		x = root(x);
		return r[x];
	}
};

signed main() {
  int h, w;
  while (cin >> w >> h, w) {
    char c[30][30];
    rep(i, 30) rep(j, 30) c[i][j] = '#';

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

    UnionFind uf(h * w + 200);

    int s = -1;

    for (int i = 1; i <= h; ++i) {
      for (int j = 1; j <= w; ++j) {
        if (c[i][j] == '#') continue;
        if (c[i][j] == '@') {
          s = i * w + j;
        }

        int v = i * w + j;

        rep(k, 4) {
          int y = i + dy[k];
          int x = j + dx[k];
          if (c[y][x] == '#') continue;

          int u = y * w + x;

          uf.unite(u, v);
        }
      }
    }

    cout << uf.element_count(s) << endl;
  }
}

C++: #include<iostream>
using namespace std;
bool b[21][21];
int w,h,count=0;

void dfs(int x, int y){
b[y][x]=1;count++;
int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
int nx,ny;
for(int i=0;i<4;i++){
 nx=dx[i]+x;
 ny=dy[i]+y;
if(0<=nx&&nx<w&&0<=ny&&ny<h)
if(b[ny][nx]==0)dfs(nx,ny);
}
}

int main(){
char a[21][21];
int sx,sy;
cin>>w>>h;

for(int i=0;i<h;i++)
for(int j=0;j<w;j++){
cin>>a[i][j];
if(a[i][j]=='#')b[i][j]=1;
if(a[i][j]=='@'){sx=j;sy=i;}
}
dfs(sx,sy);
cout<<count<<endl;
 return 0;
}