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;
// define
#define endl "\n"
#ifdef LOCAL
#define lout cerr
#else
ostream devnull(0);
#define lout devnull
#endif
#define AMULET \
  cin.tie(0);  \
  ios::sync_with_stdio(false);
// https://github.com/kurokoji/.cpp-Template/blob/master/template/template2.cpp
#define GET_MACRO(_1, _2, _3, _4, NAME, ...) NAME
#define rep(...)                                 \
  GET_MACRO(__VA_ARGS__, rep4, rep3, rep2, rep1) \
  (__VA_ARGS__)
#define rep1(n) rep2(_, n)
#define rep2(i, n) rep3(i, 0, n)
#define rep3(i, a, n) rep4(i, a, n, 1)
#define rep4(i, a, n, d) rep5(i, a, n, d, <)
#define rep5(i, a, n, d, o) for (auto i = decltype(n)(a), _n = (n); i o _n; i += (d))

#define rrep(...)                                    \
  GET_MACRO(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1) \
  (__VA_ARGS__)
#define rrep1(a) rrep2(_, a)
#define rrep2(i, a) rrep3(i, a, 0)
#define rrep3(i, a, n) rrep4(i, a, n, -1)
#define rrep4(i, a, n, d) rep5(i, (a)-1, n, d, >=)

#define dump(x) lout << #x << " = " << x << endl;
// alias
typedef long long ll;
typedef long long lint;
typedef pair<int, int> pii;
typedef pair<lint, lint> pll;
// func
template <class C>
inline void vcin(C &c);
template <class C, class D>
inline void vcin(C &c, D &d);
template <class C>
inline void vcout(const C &c, const string separate = "\n");
template <class T = int>
inline T in();

int w, h;
int cnt;
char c[20][20];

void dfs(int x, int y);

void solve()
{
  while (true)
  {
    cin >> w >> h;
    if (w == 0 && h == 0)
    {
      break;
    }

    int sx, sy;

    rep(j, h) rep(i, w)
    {
      cin >> c[i][j];
      lout << i << "," << j << " ";
      dump(c[i][j]);
      if (c[i][j] == '@')
      {
        sx = i,
        sy = j;
      }
    }
    dump(sx);
    dump(sy);

    cnt = 0;

    dfs(sx, sy);

    cout << cnt << endl;
  }
}

void dfs(int x, int y)
{
  if (x < 0 || y < 0 || x >= w || y >= h || c[x][y] == '#')
    return;

  cnt++;
  dump(x);
  dump(y);

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

  dfs(x - 1, y);
  dfs(x + 1, y);
  dfs(x, y - 1);
  dfs(x, y + 1);

  return;
}

signed main()
{
  AMULET
  solve();
  return 0;
}

template <class C>
inline void vcin(C &c)
{
  for (auto &a : c)
  {
    cin >> a;
  }
}
template <class C, class D>
inline void vcin(C &c, D &d)
{
  for (auto it1 = c.begin(), it2 = d.begin(); it1 != c.end() && it2 != d.end(); it1++, it2++)
  {
    cin >> *it1 >> *it2;
  }
}
template <class C>
inline void vcout(const C &c, const string separate)
{
  for (auto it = c.begin(); it != c.end(); it++)
  {
    if (it != c.begin())
      cout << separate;
    cout << *it;
  }
}
template <class T = int>
inline T in()
{
  T x;
  cin >> x;
  return (x);
}

C++: #include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int,int> P;

int h,w;
queue<P>q;
char c[25][25];
int si,sj,ans=0;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
bool seen[25][25];

void f(){
        for(int i=0;i<h;i++){
                for(int j=0;j<w;j++){
                        seen[i][j]=false;
                }
        }
}

void bfs(int x,int y){
        q.push(P(x,y));
        ans++;
        seen[x][y]=true;
        while(q.size()){
                P p=q.front();q.pop();
                for(int i=0;i<4;i++){
                        int nx=p.first+dx[i],ny=p.second+dy[i];
                        if(0<=nx&&nx<h&&0<=ny&&ny<w&&c[nx][ny]!='#'){
                                if(!seen[nx][ny]){
                                        seen[nx][ny]=true;
                                        q.push(P(nx,ny));
                                        ans++;
                                }
                        }
                }
        }
        return;
}

int main(){
        while(cin>>w>>h||w,h){
                ans=0;
                f();
                for(int i=0;i<h;i++){
                        for(int j=0;j<w;j++){
                                cin>>c[i][j];
                                if(c[i][j]=='@')si=i,sj=j;
                        }
                }
                bfs(si,sj);
                cout<<ans<<endl;
        }
        return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
#include <queue>

#define REP(i,n) for(int i=0;i<(int)(n);i++)

using namespace std;

int main() {
  while(1){
    int w,h;
    cin>>w>>h;
    if(!w)break;
    vector<string> t(h);
    REP(i,h)cin>>t[i];
    int si,sj;
    REP(i,h)REP(j,w){
      if(t[i][j] == '@'){
        si = i;
        sj = j;
      }
    }
    vector<vector<bool>> vis(h,vector<bool>(w,false));
    vis[si][sj] = true;
    queue<pair<int,int>> q;
    q.push(make_pair(si,sj));
    while(!q.empty()){
      auto p = q.front();q.pop();
      int i,j;
      tie(i,j) = p;
      int di[] = {1,0,-1,0};
      int dj[] = {0,1,0,-1};
      REP(k,4){
        int ni=i+di[k];
        int nj=j+dj[k];
        if(ni<0||ni>=h||nj<0|nj>=w)continue;
        if(t[ni][nj] == '#')continue;
        if(vis[ni][nj]) continue;
        q.push(make_pair(ni,nj));
        vis[ni][nj] = true;
      }
    }
    int cnt = 0;
    REP(i,h)REP(j,w){
      if(vis[i][j])++cnt;
    }
    cout << cnt << endl;
  }
  return 0;
}