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>
#include <iterator>
using namespace std;

struct Fast {Fast(){std::cin.tie(0);ios::sync_with_stdio(false);}} fast;

/* cpp template {{{ */

/* short */
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) begin(v), end(v)
#define RALL(v) (v).rbegin(), (v).rend()
#define X real()
#define Y imag()

/* REPmacro */
#define REPS(i, a, n) for (ll i = (a); i < (ll)(n); ++i)
#define rep(i, n) REPS(i, 0, n)
#define REP1(i,n) for (ll i = 1; i <= (ll)(n); ++i)
#define RREP(i, n) REPS(i, 1, n + 1)
#define DEPS(i, a, n) for (ll i = (a); i >= (ll)(n); --i)
#define DEP(i, n) DEPS(i, n, 0)
#define EACH(i, n) for (auto&& i : n)

/* debug */
#define debug(x) cerr << x << " " << "(L:" << __LINE__ << ")" << '\n';

/* alias */
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<int, int>;
using D = double;
using P = complex<D>;
using vs = vector<string>;
template <typename T> using PQ = priority_queue<T>;
template <typename T> using minPQ = priority_queue<T, vector<T>, greater<T>>;

/* const */
const int INF = 1001001001;
const ll LINF = 1001001001001001001ll;
const int MOD = 1e9 + 7;
const D EPS = 1e-9;
const int dx[] = {0, 1, 0, -1, 1, -1, 1, -1}, dy[] = {1, 0, -1, 0, 1, -1, -1, 1};

/* func */
inline bool inside(int y, int x, int H, int W) {return y >= 0 && x >= 0 && y < H && x < W;}
inline int in() {int x; cin >> x; return x;}
inline ll IN() {ll x; cin >> x; return x;}
inline vs split(const string& t, char c) {vs v; stringstream s(t); string b; while(getline(s, b, c)) v.eb(b); return v;}
template <typename T> inline bool chmin(T& a, const T& b) {if (a > b) a = b; return a > b;}
template <typename T> inline bool chmax(T& a, const T& b) {if (a < b) a = b; return a < b;}
template <typename T, typename S> inline void print(const pair<T, S>& p) {cout << p.first << " " << p.second << endl;}
template <typename T> inline void print(const T& x) {cout << x << '\n';}
template <typename T, typename S> inline void print(const vector<pair<T, S>>& v) {for (auto&& p : v) print(p);}
template <typename T> inline void print(const vector<T>& v, string s = " ") {rep(i, v.size()) cout << v[i] << (i != (ll)v.size() - 1 ? s : "\n");}

vs field;
int w,h;
int dfs(int y, int x)
{
  int cnt=1;
  field[y][x]='#';
  rep(i,4)
  {
    int nx=x+dx[i], ny=y+dy[i];
    if(nx<w&&0<=nx&&ny<h&&0<=ny&&field[ny][nx]=='.')
    {
      cnt+=dfs(ny,nx);
    }
  }
  return cnt;
}

signed main() 
{
  while(cin>>w>>h, w||h)
  {
    field.clear(); //初期化！！
    string s;
    rep(i,h) 
    {
      cin>>s;
      field.pb(s);
    }
    int x,y;
    rep(i,h) rep(j,w)
    {
      if(field[i][j]=='@')
      {
        y=i,x=j;
      }
    }
    print(dfs(y,x));
  }

  return 0;
}

C++: #include <iostream>
#include <vector>

#define BLACK   ('.')
#define RED     ('#')
#define MAN     ('@')
#define VISITED ('X')

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

int dfs(std::vector<std::vector<char> >& tiles, const int width, const int height, const int x, const int y) {
  if (tiles[y][x] == VISITED)
    return 0;
  tiles[y][x] = VISITED;
  int retval = 1;
  for (int i = 0; i < 4; ++i) {
    int cx = x + dx[i], cy = y + dy[i];
    if (cx < 0 || cy < 0 || width <= cx || height <= cy || tiles[cy][cx] == VISITED || tiles[cy][cx] == RED)
      continue;
    retval += dfs(tiles, width, height, cx, cy);
  }
  return retval;
}

int main(int argc, char const* argv[])
{
  while (true) {
    int W, H;
    std::cin >> W >> H;
    if (W == 0 && H == 0)
      break;
    int ix, iy;
    std::vector<std::vector<char> > tiles(H, std::vector<char>(W, '\0'));
    for (int y = 0; y < H; ++y) {
      for (int x = 0; x < W; ++x) {
        std::cin >> tiles[y][x];
        if (tiles[y][x] == MAN) {
          ix = x;
          iy = y;
        }
      }
    }
    std::cout << dfs(tiles, W, H, ix, iy) << std::endl;
  }
  return 0;
}

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

int dfs(int h, int w, vector<vector<int>> &v, vector<vector<int>> &field) {
    v[h][w] = 1;
    int dh[] = {0, 1, 0, -1};
    int dw[] = {-1, 0, 1, 0};
    
    for (int i = 0; i < 4; i++) {
        int nh = h + dh[i];
        int nw = w + dw[i];
        if (field[nh][nw] == 1 && v[nh][nw] == 0) {
            dfs(nh, nw, v, field);
        }
    }
    return 0;
}

int solve(int H, int W) {
    vector<vector<int>> field(H+2, vector<int>(W+2, 0));
    
    int startH;
    int startW;
    for (int h = 1; h <= H; h++) {
        for (int w = 1; w <= W; w++) {
            char c; cin >> c;
            if (c == '.') {
                field[h][w] = 1;
            } else if (c == '@') {
                field[h][w] = 1;
                startH = h;
                startW = w;
            }
        }
    }
    
    int ret = 0;
    vector<vector<int>> v(H+2, vector<int>(W+2, 0));
    dfs(startH, startW, v, field);
    for (int h = 1; h <= H; h++) {
        for (int w = 1; w <= W; w++) {
            if (v[h][w] == 1) {
                ret++;
            }
        }
    }
    return ret;
}

int main(void){
    // Your code here!
    int W, H;
    vector<int> ans;
    while (1) {
        cin >> W >> H;
        if (!W && !H) break;
        ans.push_back(solve(H, W));
    }
    for (auto &i : ans) {
        cout << i << endl;
    }
}