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 <cmath>
#include <string>
#include <iomanip>
#define GROUND 0
#define WALL 900
#define INITIAL -1

int count();
void init();
void map();
void mark_n(int,int,int,int,int);
void mark_n_all(int);
bool isValidPoint(int,int);

using namespace std;
const int MAX_W = 20;
const int MAX_H = 20;
char floor_[MAX_W][MAX_H];
//floorツつセツづ算ツ術ツ甘鳴青板づ債ャツつエツづゥ
int step[MAX_W][MAX_H];
int width,height;

int main(){
  //  char floor[MAX_W][MAX_H];
  //int step[MAX_W][MAX_H];
  
  while(cin >> width >> height && width > 0){
    string line;
    int ix,iy;
    //cout << width << " " << height << endl;
    init();
    for(int i = 0; i < height; ++i){
      cin >> line;
      for(int j=0; j < width ; ++j){
	floor_[j][i] = line[j];
	if(line[j]=='@'){
	  step[j][i]=GROUND;
	  ix = j; iy = i;
	}else if(line[j]=='#'){
	  step[j][i]=WALL;
	}
	//cout << step[j][i];
      }
      //cout << endl;
    }
    //cout << count('.') << count('#') << count('@') << endl;
    for(int k = 0; k < 400; k++){
      //map();
      mark_n_all(k);
      //mark_n(k+1,ix,iy,1,0);
      //cout << endl;
      //map();
      
      //cout << endl;
    }
    cout << count() << endl;
  }
  
}

int count(){
  int result =0;
  for(int i=0; i<height;++i){
    for(int j=0; j< width; ++j){
      if(step[j][i] >= 0 && step[j][i] < WALL){
	result++;
      }
    }
  }
  return result;
}

void init(){
  for(int i=0; i<height;++i){
    for(int j=0; j< width; ++j){
      step[j][i]=INITIAL;
    }   
  }
}

void map(){
  for(int i = 0; i < height; ++i){
    for(int j=0; j < width ; ++j){
      cout << setw(3)<< step[j][i] << " ";
    }
    cout << endl;
  }
}

void mark_n(int n,int x,int y,int dx,int dy){
  int temp;
  for(int i =0; i< 4;i++){
    temp = dx;
    dx = dy;
    dy = -temp;
    if(isValidPoint(x+dx,y+dy) && step[x+dx][y+dy]==INITIAL){
      step[x+dx][y+dy] = n;
    }
  }
}


void mark_n_all(int n){
  for(int i = 0; i < height; i++){
    for(int j = 0; j < width ; j++){
      if(step[j][i]==n){
	//	cout << j <<" " << i << "jツづツづ個値"<< endl;
	mark_n(n+1,j,i,1,0);
      }
    }
  }
}



bool isValidPoint(int x,int y){
  if(x < 0 || x >  width || y < 0 || y > height){
    return false;
  }else{
    return true;
  }
}

C++: #include<iostream>
#include<string>
#include<vector>
using namespace std;
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
int x[4]={1,0,-1,0};
int y[4]={0,1,0,-1};
int func(int i,int j,int n,int m,vector<string>& in,int& co){
  co++;
  //cout<<co<<endl;
  in[i][j]='!';
  rep(k,4){
    int nx=i+x[k];
    int ny=j+y[k];
    if(nx>=0 && nx<=n-1 && ny>=0 && ny<=m-1 && in[nx][ny]!='!' && in[nx][ny]=='.')func(nx,ny,n,m,in,co);
  }
  return co;
}
int main(){
  int n,m;
  while(cin>>m>>n,n||m){
    vector<string>in(n);
    int co=0;
    rep(i,n)cin>>in[i];
    rep(i,n)rep(j,m)if(in[i][j]=='@')co=func(i,j,n,m,in,co);
    cout<<co<<endl;
  }
}

C++: #include<iostream>
using namespace std;
char map[114][514];
int w, h, ans;
void dfs(int x, int y) {
	map[x][y] = '#';
	ans++;
	if (map[x - 1][y] == '.')dfs(x - 1, y);
	if (map[x + 1][y] == '.')dfs(x + 1, y);
	if (map[x][y - 1] == '.')dfs(x, y - 1);
	if (map[x][y + 1] == '.')dfs(x, y + 1);
	return;
}
int main() {
	while (cin >> w >> h) {
		if (!w && !h)return 0;
		int x, y;
		ans = 0;
		for (int i = 1; i <= h; i++) {
			for (int j = 1; j <= w; j++) {
				cin >> map[i][j];
				if (map[i][j] == '@') {
					x = i;
					y = j;
				}
			}
		}
		dfs(x, y);
		cout << ans << endl;
	}
}

C++: #include <iostream>
#include <queue>
//#include <pair>
#include <tuple>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x)  cerr << #x << " = " << (x) << endl;
using namespace std;

template <typename X, typename T>
auto vectors(X x, T a) {
	return vector<T>(x, a);
}

template <typename X, typename Y, typename Z, typename... Zs>
auto vectors(X x, Y y, Z z, Zs... zs) {
	auto cont = vectors(y, z, zs...);
	return vector<decltype(cont)>(x, cont);
}

template<typename T>
ostream& operator << (ostream& os, vector<T>& v){
	rep(i,v.size()){ os << v[i] << (i == v.size() - 1 ? "" : " "); } return os;
}
template<typename T>
istream& operator >> (istream& is, vector<T>& v){
	for(T& x: v){ is >> x; } return is;
}

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

int main(){
	int h, w;
	while(cin >> w >> h,h||w){
		vector<vector<char>> m(h, vector<char>(w));
		queue<pair<int, int>> q;
		rep(i,h){
			rep(j,w){
				cin >> m[i][j];
				if(m[i][j] == '@') q.emplace(i,j);
			}
		}

		bool u[20][20] = {};
		while(not q.empty()){
			int y, x;
			tie(y, x) = q.front(); q.pop();
			if(u[y][x]) continue;
			u[y][x] = true;
			rep(d,4){
				int ny = y + dy[d];
				int nx = x + dx[d];
				if(ny < 0 or ny >= h or nx < 0 or nx >= w) continue;
				if(m[ny][nx] == '#') continue;
				q.emplace(ny,nx);
			}
		}

		int cnt = 0;
		rep(i,h){
			rep(j,w){
				cnt += u[i][j] == true;
			}
		}
		cout << cnt << endl;
	}
}