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<algorithm>
#include<cstdio>
#include<cmath>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define MOD 1000000007
#define rep(i,n) for(i=0;i<n;i++)
#define loop(i,a,n) for(i=a;i<n;i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

typedef vector<int> vi;
typedef pair<int,int> pii;

int main(void) {
  int i,j;
  int h,w;
  while(1){
    cin>>w>>h;
    if(w+h==0)break;
    cin.ignore();
    vector<string> s(h);
    rep(i,h)cin>>s[i];
    queue<pii> q;
    vector<vi> used(h,vi(w,0));
    rep(i,h)rep(j,w)if(s[i][j]=='@'){
      q.push(pii(i,j));
      used[i][j]++;
    }
    int d[5]={0,1,0,-1,0};
    while(q.size()){
      rep(i,4){
	int x=q.front().first+d[i];
	int y=q.front().second+d[i+1];
	if(x>=0 && x<h && y>=0 && y<w && 
	   s[x][y]=='.' && used[x][y]==0){
	     used[x][y]++;
	     q.push(pii(x,y));
	}
      }
      q.pop();
    }
    int ans=0;
    rep(i,h)rep(j,w)if(used[i][j])ans++;
    cout<<ans<<endl;
  }
}

C++: #include<stdio.h>

int m,n,ans;
char ice[22][22];

void saiki(int w,int h){
	ice[w][h]='*';
	
	if(ice[w+1][h]=='.')saiki(w+1,h);
	if(ice[w][h+1]=='.')saiki(w,h+1);
	if(w!=0)if(ice[w-1][h]=='.')saiki(w-1,h);
	if(h!=0)if(ice[w][h-1]=='.')saiki(w,h-1);
}

int main(void){

	for(;;){
		scanf("%d %d",&m,&n);
		if(!m||!n)break;
		
		ans=0;
		
		for(int i=0;i<n;i++){
			scanf("%s",&ice[i]);
		}
		
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				if(ice[i][j]=='@')saiki(i,j);
			}
		}
		//puts("");
		for(int i=0;i<n;i++){
			//printf("%s\n",ice[i+1]);
			for(int j=0;j<m;j++){
				if(ice[i][j]=='*')ans++;
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <tuple>

using namespace std;

void dfs( int W, int H, int w_index, int h_index, vector< vector<char> > &V, int &ans );

int main(int argc, char const *argv[]) {
    vector< vector<char> > vec;

    int w, h, w_index, h_index, ans;
    vector<int> sums;

    while (1) {
        ans = 1;
        cin >> w >> h;

        if( w==0 || h==0 ) break;

        vec.resize(h);
        for (int i = 0; i < h; i++) {
            vec[i].resize(w);
        }

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

        for (auto it = vec.begin(); it != vec.end(); it++ ) {
            auto iter = find(it->begin(), it->end(), '@');

            if(iter!=it->end()){
                h_index = distance( vec.begin(), it );
                w_index = distance( it->begin(), iter );
            }
        }

        dfs( w, h, w_index, h_index, vec, ans );

        sums.push_back( ans );
    }

    for( auto it = sums.begin(); it != sums.end(); it++ ){
        cout << *it << endl;
    }

    return 0;
}

void dfs( int W, int H, int w_index, int h_index, vector< vector<char> > &v , int &ans ){
    int dx[4] = { 1, 0, -1, 0 };
    int dy[4] = { 0, 1, 0, -1 };
    v[h_index][w_index] = '#';

    for( int i = 0; i < 4; i++ ){
        int nx = w_index + dx[i];
        int ny = h_index + dy[i];

        if( 0 <= nx && nx < W && 0 <= ny && ny < H && v[ny][nx] != '#' ){
            ans++;
            dfs( W, H, nx, ny, v, ans );
        }
    }
    return;
}

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

int w, h;
char data[99][99]; //y, x
int dx[] = {0,0,1,-1};
int dy[] = {1,-1,0,0};

int solve(int x, int y){

  if(x < 0 || y < 0 || x == w || y == h) return 0;
  if(data[y][x] == '#') return 0;

  data[y][x] = '#';

  int ret = 0;
  for(int i=0;i<4;i++) ret += solve(x+dx[i], y+dy[i])+1;

  return ret;
}

int main(){

  while(true){
    cin >> w >> h;
    if(!w && !h) break;

    int sx, sy;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	cin >> data[i][j];
	if(data[i][j] == '@') sx = j, sy = i;
      }
    }

    cout << solve(sx, sy)/4 << endl; //for()で４回+1しているため

  }
}

C++: #pragma GCC optimize(2)
#include <iostream>
#include <queue>
#include <vector>
#include <string>
using namespace std;
const int INF = 1e9;
const int MXN = 0;
const int MXV = 0;
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define FOR(i, L, R) for (int i = L; i != (int)R; ++i)
#define FORD(i, L, R) for (int i = L; i != (int)R; --i)
#define IOS                                                                    \
    cin.tie(NULL);                                                             \
    cout.tie(NULL);                                                            \
    ios_base::sync_with_stdio(false);
vector<string> room;
int n, m;
int ans;
int d[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

void dfs(int x, int y)
{
    // cout << x << ' ' << y << '\n';
    FOR(i, 0, 4)
    {
        int _x = x + d[i][0];
        int _y = y + d[i][1];
        //     cout << _x << '-' << _y << '\n';
        // if (_x >= 0 && _x < n && _y >= 0 && _y < m)
        // {
        //     cout << room[_x][_y] << '\n';
        // }
        if (_x >= 0 && _x < m && _y >= 0 && _y < n && room[_x][_y] == '.')
        {
            room[_x][_y] = '@';
            ++ans;
            dfs(_x, _y);
        }
    }
}

int main()
{
    IOS;
    while (cin >> n >> m, n || m)
    {
        room.resize(m);
        FOR(i, 0, m) { cin >> room[i]; }
        int x = 0, y = 0;
        FOR(i, 0, m) FOR(j, 0, n)
        {
            if (room[i][j] == '@')
            {
                x = i;
                y = j;
            }
        }
        ans = 1;
        // cout << x << ' ' << y << '\n';
        dfs(x, y);
        cout << ans << '\n';
    }
}