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<cmath>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<cstdio>
#include<map>
#include<utility>
#include<list>
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define fori(i,k,n) for(int i=k;i<(int)n;i++)
using namespace std;
const int dx[4] = { 0, 1, 0, -1 }, dy[4] = { 1, 0, -1, 0 };
const int dX[8] = { 0, 1, 1, 1, 0, -1, -1, -1 }, dY[8] = { 1, 1, 0, -1, -1, -1, 0, 1 };
struct XY
{
	int x;
	int y;
};


/*
(char)'1' - (int)48 = (int)1;


*/
int main()
{
	int h, w;
	cin >> w>> h;
	while (w != 0 || h != 0)
	{
		queue<XY> m;
		XY fis;
		int a[200][200] = {};
		for (int i = 0; i < h; i++)
		{
			for (int l = 0; l < w; l++)
			{
				char r;
				cin >> r;
				if (r == '.')
					a[i + 1][l + 1] = 1;
				else if (r == '@')
				{
					fis.x = l + 1;
					fis.y = i + 1;
				}

			}
		}


		int ans = 1;
		m.push(fis);
		while (!m.empty())
		{
			XY gg;
			int x, y;
			x = m.front().x;
			y = m.front().y;
			m.pop();

			for (int i = 0; i < 4; i++)
			{
				if (a[y + dy[i]][x + dx[i]] == 1)
				{
					ans++;
					a[y + dy[i]][x + dx[i]] = 0;
					gg.x = x + dx[i];
					gg.y = y + dy[i];
					m.push(gg);
				}
			}

		}

		cout << ans << endl;
		cin >> w >> h;
	}
}

C++: #include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <vector>
#include <string>
#include <sstream>
#include <cstring>

#define rep(i, j) for(int i = 0; i < j; i++)
// sp_rep
#define sp_rep(i, j) for(int i = 1; i <= j; i++)
#define all(i) i.begin(), i.end()
#define ll long long

using namespace std;

string Num2Str(int num)
{
	stringstream ss;
	ss << num;
	return ss.str();
}

int w, h, cnt;
char maze[20][20];
bool reached[20][20];

void serch(int x, int y) {
	if (x < 0 || w <= x || y < 0 || h <= y || maze[y][x] == '#') return;
	if (reached[y][x])return;
	reached[y][x] = true;
	cnt++;

	serch(x + 1, y);
	serch(x - 1, y);
	serch(x, y + 1);
	serch(x, y - 1);
}

int main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);

	// code
	while (true) {
		cin >> w >> h;
		if (w == 0 && h == 0)break;
		cnt = 0;
		memset(reached, false, sizeof(reached));
		int sx, sy;
		rep(y, h) {
			rep(x, w) {
				cin >> maze[y][x];
				if (maze[y][x] == '@') {
					sx = x;
					sy = y;
				}
			}
		}
		serch(sx, sy);
		cout << cnt << endl;
	}

	return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<cctype>
#include<utility>
#include<string>
#include<cmath>
#include<cstring>
#include<queue>
#include<map>
#include<queue>

#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 1000000007
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;
typedef long long int ll;

#define ARRAY_MAX 25

int h,w;
char mp[ARRAY_MAX][ARRAY_MAX];
int sx,sy;
int dx[4] = {1,0,0,-1};
int dy[4] = {0,1,-1,0};
int ans = 1;

void dfs(int sy,int sx){

    mp[sy][sx] = '$';//今いるところを塗りつぶす
    
    for(int i = 0;i < 4;i++){
        int nx = sx + dx[i];
        int ny = sy + dy[i];
        if(nx >= 0 && nx < w && ny >= 0 && ny < h && mp[ny][nx] == '.'){
            //mpの範囲内で.があれば調べる
            ans++;
            dfs(ny,nx);
        }
    }
}

int main(){

    while(cin >> w >> h,w){

        REP(i,h){
            REP(j,w){
                cin >> mp[i][j];
                if(mp[i][j] == '@'){
                    sy = i;
                    sx = j;//スタート一を保存
                }
            }
        }
        dfs(sy,sx);
        cout << ans << endl;
        ans = 1;
    }



    return 0;
}

C++: #include <iostream>
using namespace std;
int ch[23][23],cnt;
void dfs(int,int);
int main(){
  int w,h,i,j,k,l;
  string in[21];
  while(1){
    cnt=0;
    cin >> w >> h;
    if(w==0&&h==0) break;
    for(i=0;i<h;i++){
      cin >> in[i];
    }
    for(i=0;i<h+2;i++){
      for(j=0;j<w+2;j++){
    ch[i][j]=1;
      }
    }
    for(i=0,k=1;k<h+1;i++,k++){
      for(j=0,l=1;l<w+1;l++,j++){
    if(in[i][j]=='.') ch[k][l]=0;
    else if(in[i][j]=='#') ch[k][l]=1;
    else ch[k][l]=2;
      }
    }
    for(i=1;i<=h;i++){
      for(j=1;j<=w;j++){
    if(ch[i][j]==2) dfs(i,j);
      }
    }
    cout << cnt << endl;
  }
  return 0;
}
void dfs(int i,int j){
  if(ch[i][j]==1) return ;
  ch[i][j]=1;
  cnt++;
  dfs(i+1,j);
  dfs(i-1,j);
  dfs(i,j-1);
  dfs(i,j+1);
}

C++: #include<cstdio>

#define MAX 22
#define INV (0)
#define REP(i,a,e) for(int i=(a); i<=(e); ++i)

void dfs(int map[][MAX], int i, int j, int &ans)
{
	const int di[] = {-1,0,1,0};
	const int dj[] = {0,1,0,-1};
	if( map[i][j] == INV || map[i][j] == '#' ){
		return ;
	}
	if( map[i][j] == '.' ){
		++ans;
		map[i][j] = '#';
	}
	for(int k = 0; k < 4; ++k){
		dfs( map, i + di[k], j + dj[k], ans );
	}
}

int main()
{
	while(true){
		int si, sj;
		int ans = 1;
		int map[MAX][MAX] = {{INV,},};
		int W,H;

		scanf("%d%d", &W, &H);

		if( W == 0 && H == 0 ) break;

		REP(i,1,H){
			char c;
			scanf("%c", &c);
			REP(j,1,W){
				scanf("%c", &c);
				map[i][j] = c;
				if( map[i][j] == '@' ){
					si = i;
					sj = j;
				}
			}
		}

		dfs(map,si,sj,ans);

		printf("%d\n", ans);
	}
	return 0;
}