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++: //
//  main.cpp
//  AOJ-1130-2
//
//  Created by Kenta Kodera on 2017/06/18.
//  Copyright ?? 2017??´ Kenta Kodera. All rights reserved.
//

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <numeric>
#include <string>
#include <string.h>
#include <map>
#include <set>
#include <functional>
#include <complex>
#include <stack>
#include <tuple>
#include <array>
#include <ctype.h> // isdigit
#include <cassert>
using namespace std;

int W, H;

int dfs(vector<string> &room, int h, int w){
  
  int dh[4] = {1, 0, -1, 0};
  int dw[4] = {0, 1, 0, -1};
  room[h][w] = '#';
  
  int ans = 1;
  for(int i=0; i<4; i++){
    int nh = h + dh[i];
    int nw = w + dw[i];
    if(0<=nh && nh<H && 0<=nw && nw<W && room[nh][nw] == '.'){
      ans += dfs(room, nh, nw);
    }
  }
  return ans;
}

int main(){
  
  while(cin >> W >> H, W|H){
    
    vector<string> room(H);
    for(int i=0; i<H; i++){
      cin >> room[i];
    }
    
    for(int h=0; h<H; h++){
      for(int w=0; w<W; w++){
        if(room[h][w] == '@'){
          cout << dfs(room, h, w) << endl;
        }
      }
    }
  }
  return 0;
}

C++: #include <bits/stdc++.h>

#define reps(v, f, l) for (int v = (f), v##_ = (l); v < v##_; ++v)
#define rep(v, n) reps(v, 0, n)
#define lep(v, n) reps(v, 1, n + 1)

using namespace std;

typedef long long int lint;

static inline int in(){ int x; scanf("%d", &x); return x; }
static inline lint inl(){ lint x; scanf("%lld", &x); return x; }
template<typename T> void show(T& a, char del='\n', char last='\n'){ rep(i, a.size() - 1) cout << a[i] << del; cout << a[a.size() - 1] << last; }

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

struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) {}
  bool merge(int x, int y){
    x = root(x), y = root(y);
    if (x != y){
      if (data[x] > data[y]) swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  bool isSame(int x, int y){
    return root(x) != root(y);
  }
  int size(int x){
    return -data[root(x)];
  }
  int root(int x){
    return data[x] < 0 ? x : data[x] = root(data[x]);
  }
};

int main()
{
  int w, h;
  while (w = in(), h = in(), w){
    UnionFind uf(w * h);
    vector<string> field;
    rep(i, h){
      string s;
      cin >> s;
      field.push_back(s);
    }
    int sx, sy;
    rep(y, h){
      rep(x, w){
        char cell = field[y][x];
        if (cell == '#') continue;
        if (cell == '@') {
          sx = x;
          sy = y;
        }
        rep(d, 4){
          int nx = x + dx[d];
          int ny = y + dy[d];
          if (nx < 0 || nx >= w || ny < 0 || ny >= h) continue;
          if (field[ny][nx] != '#') uf.merge(y * w + x, ny * w + nx);
        }
      }
    }
    printf("%d\n", uf.size(sy * w + sx));
  }
  return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<string>
#include<cstdlib>
#include<map>
#include<iomanip>
#include<sstream>
#include<vector>
#include<stack>
#include<math.h>
#include<queue>
#include<complex>
#include<random>
#include<ctime>
#include<set>
using namespace std;


const long long int mod=1000000007;
const long long int INF=99999999999999999;


long long int h,w;
string str[25];
int main() {
	cout << fixed << setprecision(18);
	cin>>w>>h;
	while(w!=0){
		for(int i=0;i<h;i++){
			cin>>str[i];
		}
		long long int start_h,start_w;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(str[i][j]=='@'){
					start_h=i;
					start_w=j;
				}
			}
		}
		long long int num=1;
		queue<pair<long long int,long long int> >task;
		task.push({start_h,start_w});
		bool used[25][25]={};
		used[start_h][start_w]=true;
		while(!task.empty()){
			long long int hnow=task.front().first;
			long long int wnow=task.front().second;
			if(hnow!=0&&str[hnow-1][wnow]=='.'&&!used[hnow-1][wnow]){
				used[hnow-1][wnow]=true;
				num++;
				task.push({hnow-1,wnow});
			}
			if(hnow!=h-1&&str[hnow+1][wnow]=='.'&&!used[hnow+1][wnow]){
				used[hnow+1][wnow]=true;
				num++;
				task.push({hnow+1,wnow});
			}
			if(wnow!=0&&str[hnow][wnow-1]=='.'&&!used[hnow][wnow-1]){
				used[hnow][wnow-1]=true;
				num++;
				task.push({hnow,wnow-1});
			}
			if(wnow!=w-1&&str[hnow][wnow+1]=='.'&&!used[hnow][wnow+1]){
				used[hnow][wnow+1]=true;
				num++;
				task.push({hnow,wnow+1});
			}
			//cout<<"DEBUG"<<hnow<<" "<<wnow<<endl;
			task.pop();
		}
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				//cout<<used[i][j]<<" ";
			}
			//cout<<endl;
		}
		cout<<num<<endl;
		cin>>w>>h;
	}
}

C++: #include<iostream>
using namespace std;
#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n)   REP(i,0,n)

const int N = 500;
bool vis[N][N];
char m[N][N];
const int dx[]={0,0,1,-1};
const int dy[]={1,-1,0,0};
int cnt;

void dfs(int r,int c,int y,int x){
  if (vis[y][x])return;
  vis[y][x]=true;
  cnt++;
  rep(i,4){
    int nex=x+dx[i],ney=y+dy[i];
    if (nex == -1 || ney == -1 || nex == c || ney == r ||
	m[ney][nex] == '#')continue;
    dfs(r,c,ney,nex);
  }


}

main(){
  int c,r;
  while(cin>>c>>r){
    if (c == 0 && r == 0)break;
    int sy=0,sx=0;
    cnt=0;
    rep(i,r)rep(j,c)vis[i][j]=false;
    rep(i,r){
      cin>>m[i];
      rep(j,c)if (m[i][j] == '@')sy=i,sx=j;
    }
    dfs(r,c,sy,sx);
    cout << cnt << endl;
  }
  return false;
}