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<bits/stdc++.h>
using namespace std;
using ll = long long;
#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
int n,m;
vector<string> area;
bool ok(int x,int y,int nx,int ny){
  bool res = 0<=nx&&nx<n&&0<=ny&&ny<m;
  if(res){
    res = (area[y][x]=='.'||area[y][x]=='@')&&(area[ny][nx]=='.'||area[ny][nx]=='@');
  }
  return res;
}
struct union_find{//rank,経路圧縮で高速化
  vector<long long> data;//そのノードがrootであれば-（集合のサイズ）を表し、そうでなければ親ノードidを示す。
  union_find(long long size):data(size,-1){}//コンストラクタ,size個のノードを作成し、-1で初期化
  inline long long root(long long a){//根を探す,経路圧縮
    return data[a] < 0 ? a : data[a]=root(data[a]);
  }
  inline bool find(long long a,long long b){//a,bが同じ集合に属するか調べる
    return root(a) == root(b);
  }
  inline bool unite(long long a,long long b){//小さい方の木の根に大きい方の根を上書きする
    a = root(a),b = root(b);
    if(a==b) return false;//根が同じなら終了,uniteが失敗したことを出力
    if(data[a]<data[b]) swap(a,b); //集合サイズの大きい方の根に低い方の根をつける
    data[b] += data[a];//集合サイズを統合
    data[a] = b;//yの親にxをつける
    return true;//unite成功を出力
  }
  inline long long size(long long a){//aが属する集合のサイズを出力
    return -data[root(a)];
  }
};
int main(){
  while(cin>>n>>m,n){
    union_find uf(n*m);
    area.resize(m);
    REP(i,m) cin >> area[i];
    int person = -1;
    REP(i,m)REP(j,n){
      if(area[i][j]=='@') person = i*n+j;
      REP(k,4){
        if(ok(j,i,j+dx[k],i+dy[k])){
          uf.unite(i*n+j,(i+dy[k])*n+j+dx[k]);
        }
      }
    }
    cout << uf.size(person) << endl;
  }
}

C++: #include <iostream>

using namespace std;

int width, height;
int ans = 0;
char map[20][20];
int checked[20][20];

void search(int x, int y);
int main(){
	while (1) {
		ans = 0;
		for (int y=0; y<20; y++) {
			for (int x=0; x<20; x++) {
				map[y][x] = '#';
				checked[y][x] = 0;
			}
		}
		cin >> width >> height;
		if (width==0 && height==0) {
			break;
		}

		int startX, startY;
		for (int y=0; y<height; ++y) {
			for (int x=0; x<width; ++x) {
				cin >> map[y][x];
				if (map[y][x] == '@') {
					map[y][x] = '.';
					startX = x;
					startY = y;
				}
			}
		}
		search(startX, startY);
		cout << ans << endl;
	}
	return 0;
}
void search(int x, int y) {
	ans++;
	static const int diffX[] = {0,-1,1,0};
	static const int diffY[] = {-1,0,0,1};
	checked[y][x] = 1;
	for (int ite=0; ite<4; ++ite) {
		int nowX = x + diffX[ite];
		int nowY = y + diffY[ite];
		if (nowX < 0 || nowX >= 20 || nowY < 0 || nowY >= 20) continue;
		if (checked[nowY][nowX] == 0 && map[nowY][nowX] == '.') {
			search(nowX, nowY);
		}
	}
}

C++: #include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<string>
#include<cmath>
//#include<>
using namespace std;

typedef long long int64;
typedef int int32;
typedef char int8;
typedef int32 int__;
typedef pair<int,int> pii;
#define rep(i,j) for(int__ i=0;i<j;i++)
#define repeat(i,j,k) for(int__ i=(j);i<(k);i++)
#define repeat_eq(i,j,k) for(int__ i=(j);i<=(k);i++)
#define all(v) (v).begin(),(v).end()

const int INF=1<<30;//=10E10

int main()
{
  int w,h;
  while(cin>>w>>h && w && h){
    char tile[20][20];
    int fx,fy;
    rep(i,h)rep(j,w){
      cin>>tile[i][j];
      if(tile[i][j]=='@'){
	fx=j;fy=i;
      }
    }
    
    bool visited[20][20]={};
    stack<pii> p;
    int ans=0;
    p.push(pii(fy,fx));
    while(!p.empty()){
      pii now=p.top();p.pop();
      int nx=now.second,ny=now.first;
      if(!visited[ny][nx]){
	visited[ny][nx]=true;
	ans++;
	if(ny>0 && tile[ny-1][nx]=='.')p.push(pii(ny-1,nx));
	if(ny+1<h && tile[ny+1][nx]=='.')p.push(pii(ny+1,nx));
	if(nx>0 && tile[ny][nx-1]=='.')p.push(pii(ny,nx-1));
	if(nx+1<w && tile[ny][nx+1]=='.')p.push(pii(ny,nx+1));
      }
    }
    cout<<ans<<endl;
  }

  return 0;
}

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;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
typedef vector<vb> vvb;
typedef vector<vpii> vvpii;
const int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
const int MOD = 1000000007;
const 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; }
bool odd(const int &n) { return n & 1; }
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) {
    vs v(h);
    int si, sj;
    rep(i, h) {
      cin >> v[i];
      rep(j, v[i].size()) if (v[i][j] == '@') {
        si = i;
        sj = j;
      }
    }
    queue<pii> q;
    q.emplace(si, sj);
    int c = 0;
    while (q.size()) {
      int i, j;
      tie(i, j) = q.front();
      q.pop();
      if (i < 0 || j < 0 || i >= h || j >= w || v[i][j] == '#') continue;
      c++;
      v[i][j] = '#';
      vi di = {0, 1, 0, -1}, dj = {1, 0, -1, 0};
      rep(k, 4) q.emplace(i + di[k], j + dj[k]);
    }
    cout << c << endl;
  }
}