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 9000
#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;
    //cout << isValidPoint(x+dx,y+dy) << " " << x+dx << y+dy << endl;
    if(isValidPoint(x+dx,y+dy) && step[x+dx][y+dy]==INITIAL){
      //cout << isValidPoint(x+dx,y+dy) << " " << x+dx << y+dy << endl;
      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 <vector>
using namespace std;
void move(const vector<vector<char>> &board, vector<vector<bool>> &reached, int x, int y, int w, int h) {
    if (x < 0 || w <= x || y < 0 || h <= y) {
        return;
    } else if (board[x][y] == '#') {
        return;
    } else if (reached[x][y]) {
        return;
    }
    reached[x][y] = true;
    int dxAry[] = {0, 1, 0, -1};
    int dyAry[] = {-1, 0, 1, 0};
    for (int i = 0; i < 4; ++i) {
        move(board, reached, x + dxAry[i], y + dyAry[i], w, h);
    }
}
int main(void){
    int w, h;
    while (cin >> w >> h && w && h) {
        vector<vector<char>> board(w, vector<char>(h));
        int sx, sy;
        for (int i = 0; i < h; ++i) {
            string row;
            cin >> row;
            for (int j = 0; j < row.length(); ++j) {
                board[j][i] = row[j];
                if (row[j] == '@') {
                    sx = j;
                    sy = i;
                }
            }
        }
        vector<vector<bool>> reached(w, vector<bool>(h, false));
        move(board, reached, sx, sy, w, h);
        int count = 0;
        for (int y = 0; y < h; ++y)
            for (int x = 0; x < w; ++x)
                count += reached[x][y];
        cout << count << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int w,h;
char a[100][100];
int dfs(int i,int j){
    if(i==h||j==w||i<0||j<0)return 0;
    if(a[i][j]=='#')return 0;
    int s=1;
    a[i][j]='#';
    s+=dfs(i+1,j);
    s+=dfs(i-1,j);
    s+=dfs(i,j+1);
    s+=dfs(i,j-1);
    return s;
}
int main(){
    int i,j;
    while(cin>>w>>h,w!=0&&h!=0){
    //char a[h][w];
    for(i=0;i<h;i++)cin>>a[i];
    for(i=0;i<h;i++)for(j=0;j<w;j++)if(a[i][j]=='@')cout<<dfs(i,j)<<endl;
}}

C++: // clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
// #define main int main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
#define stlice(from, to) substr(from, (to) - (from) + 1)
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
using namespace std;
using pii = pair<int, int>;
using vi = vector<int>;
using vd = vector<double>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vpii = vector<pii>;
using vvi = vector<vi>;
using vvb = vector<vb>;
using vvpii = vector<vpii>;
template<typename A> using fn = function<A>;
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = acos(-1);
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
constexpr bool odd(const int &n) { return n & 1; }
constexpr bool even(const int &n) { return !odd(n); }
void solve();
main { solve(); return 0; }
// clang-format on

void solve() {
  int w, h;
  while (cin >> w >> h, w) {
    int si, sj;
    vs m(h);
    rep(i, h) {
      cin >> m[i];
      rep(j, m[i].size()) {
        if (m[i][j] == '@') {
          si = i;
          sj = j;
        }
      }
    }
    int c = 0;
    fn<void(int, int)> f = [&](int i, int j) {
      if (i < 0 || j < 0 || i >= h || j >= w || m[i][j] == '#') return;
      c++;
      m[i][j]  = '#';
      int di[] = {1, 0, -1, 0}, dj[] = {0, 1, 0, -1};
      rep(k, 4) f(i + di[k], j + dj[k]);
    };
    f(si, sj);
    cout << c << endl;
  }
}