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++: #define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <functional>
#include <utility>
#include <tuple>
#include <cctype>
#include <bitset>
#include <complex>
#include <cmath>
#include <array>
using namespace std;
#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3fLL
#define MOD 1000000007
#define mp make_pair
#define mt make_tuple
#define pb push_back
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pint;
typedef pair<ll,ll> pll;
typedef tuple<int,int,int> tint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<pint> vpint;
int dx[8]={0,0,-1,1,1,1,-1,-1};
int dy[8]={-1,1,0,0,1,-1,1,-1};
const int SIZE=100050;
//ここまでテンプレ
int main(){
	while(1){
		int W,H;
		cin>>W>>H;
		if(W==0 && H==0)
			return 0;
		int ans=1;
		vector<string> S;
		for(int i=0;i<H;i++){
			string s;
			cin>>s;
			S.pb(s);
		}
		queue<pint> Q;
		for(int i=0;i<H;i++)
			for(int j=0;j<W;j++)
				if(S[i][j]=='@')
					Q.push(mp(j,i));
		while(Q.size()){
			pint P=Q.front();
			Q.pop();
			for(int k=0;k<4;k++){
				int X=P.first+dx[k],Y=P.second+dy[k];
				if(X<0 || W<=X || Y<0 || H<=Y)
					continue;
				if(S[Y][X]=='.'){
					Q.push(mp(X,Y));
					S[Y][X]='@';
					ans++;
				}
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <math.h>
#include <algorithm>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;

int main(){
	int w,h;
	vector<string> f(30);
	vector< vector<int> > g(30,vector<int>(30,0));
	int count = 0;
	int count2 = 0;

	while(1){
		cin >> w >> h;
		if(w == 0 && h == 0){
			break;
		}

		for(int i = 0;i < h; i++){
			cin >> f[i];
		}



		for(int i = 0;i < h; i++){
			for(int j = 0;j < w;j++){
				g[i][j] = 0;
			}
		}
		count = 0;
		count2 = 0;
		while(1){
			for(int i = 0; i < h; i++){
				for(int j = 0; j < w;j++){
					if(f[i][j] == '@' && count== 0){
						g[i][j] = 1;
						count2++;
					}else if(g[i][j] == 1){
						if(i != 0){
							if(f[i-1][j] == '.'){
								if(g[i-1][j] == 0){
									count2++;
								}
								g[i-1][j] = 1;

							}
						}
						if(i != h-1){
							if(f[i+1][j] == '.'){
								if(g[i+1][j] == 0){
									count2++;
								}
								g[i+1][j] = 1;
							}
						}
						if(j != 0){
							if(f[i][j-1] == '.'){
								if(g[i][j-1] == 0){
									count2++;
								}
								g[i][j-1] = 1;
							}
						}
						if(j != w-1){
							if(f[i][j+1] == '.'){
								if(g[i][j+1] == 0){
									count2++;
								}
								g[i][j+1] = 1;
							}
						}
					}else{}
				}
			}

			if(count == count2){
				cout << count << endl;
				break;
			}
			count = count2;
		}

	}
}

C++: #include<iostream>
#include<queue>
using namespace std;
int main() {
	int w, h;
	while (cin >> w >> h) {
		if (!w && !h)return 0;
		queue<pair<int, int>> q;
		char map[114][514];
		bool b[114][514] = {};
		for (int i = 1; i <= h; i++) {
			for (int j = 1; j <= w; j++) {
				cin >> map[i][j];
				if (map[i][j] == '@')q.push(make_pair(i, j));
			}
		}
		int ans = 0;
		while (q.size()) {
			ans++;
			int x = q.front().first;
			int y = q.front().second;
			q.pop();
			map[x][y] = '#';
			b[x][y] = true;
			if (map[x - 1][y] == '.' && !b[x - 1][y]) {
				q.push(make_pair(x - 1, y));
				b[x - 1][y] = true;
			}
			if (map[x + 1][y] == '.' && !b[x + 1][y]) {
				q.push(make_pair(x + 1, y));
				b[x + 1][y] = true;
			}
			if (map[x][y - 1] == '.' && !b[x][y - 1]) {
				q.push(make_pair(x, y - 1));
				b[x][y - 1] = true;
			}
			if (map[x][y + 1] == '.' && !b[x][y + 1]) {
				b[x][y + 1] = true;
				q.push(make_pair(x, y + 1));
			}
		}
		cout << ans << endl;
	}
}

C++: #include <iostream>

using namespace std;

#define rep(i, n) for(int i = 0; i < (int)(n); ++i)

bool done[22][22];
int w, h;
bool field[22][22];
int ofs[4][2] = {
  {0,1},
  {1,0},
  {-1,0},
  {0,-1}
};
int dfs(int x, int y) {
  if( !field[y][x] ) return 0;
  if( done[y][x] ) return 0;
  done[y][x] = true;
  int cnt = 1;
  rep(i, 4) {
    int nx = x + ofs[i][0];
    int ny = y + ofs[i][1];
    cnt += dfs(nx, ny);
  }
  return cnt;
}

int main()
{
  while(cin >> w >> h, (w|h)) {
    rep(i, 21) rep(j, 21) {
      field[i][j] = done[i][j] = false;
    }
    int sx, sy;
    rep(y, h) {
      rep(x, w) {
        char c;
        cin >> c;
        field[y + 1][x + 1] = c != '#';
        if( c == '@' ) {
          sx = x + 1;
          sy = y + 1;
        }
      }
    }

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

C++: #include <cstdio>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

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

int main(){
	for(int w,h;scanf("%d%d",&w,&h),w;){
		char B[20][21];
		rep(i,h) scanf("%s",B[i]);

		int ans=1;
		rep(_,h*w){
			rep(i,h) rep(j,w) if(B[i][j]=='@') {
				rep(k,4){
					int y=i+dy[k],x=j+dx[k];
					if(0<=y && y<h && 0<=x && x<w && B[y][x]=='.') B[y][x]='@', ans++;
				}
			}
		}
		printf("%d\n",ans);
	}

	return 0;
}

C++: #define _USE_MATH_DEFINES
#include "bits/stdc++.h"
#define EPS 1e-10
using namespace std;
typedef long long llong;
vector<vector<char> >v;
int w, h;
int ans = 0;
void dfs(int x, int y) {
	if (x < 0 || h <= x)return;
	if (y < 0 || w <= y)return;

	if (v[x][y] == '#')return;

	if (v[x][y] == '.'||v[x][y]=='@') {
		ans++;
		v[x][y] = '#';
		dfs(x + 1, y);
		dfs(x - 1, y);
		dfs(x, y - 1);
		dfs(x, y + 1);
	}
	return;
}
int main() {
	while (cin >> w >> h, h) {
		ans = 0;
		int x, y;
		v = vector<vector<char> >(h, vector<char>(w));
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				cin >> v[i][j];
				if (v[i][j] == '@') {
					x = i;
					y = j;
				}
			}
		}
		dfs(x, y);
		cout << ans << endl;
	}
	return 0;
}