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 <algorithm>
#include <cstring>
#include <cstdio>

using namespace std;

int W, H;
string m[30];
bool visit[30][30];
const int di[4] = {0,0,-1,1};
const int dj[4] = {1,-1,0,0};

int dfs(const int ci, const int cj) {
  int ret = 1;
  for (int i = 0; i < 4; ++i) {
    int ni = ci + di[i], nj = cj + dj[i];
    if (ni < 0 || nj < 0 || H <= ni || W <= nj) continue;
    if (m[ni][nj] == '#' || visit[ni][nj]) continue;
    visit[ni][nj] = true;
    ret += dfs(ni, nj);
  }
  return ret;
}

int solve() {
  memset(visit, 0, sizeof visit);
  int ret = 0;
  for (int i = 0; i < H; ++i)
    for (int j = 0; j < W; ++j)
      if (m[i][j] == '@')
        visit[i][j] = true , ret = dfs(i, j);
  return ret;
}

int main() {
  ios_base::sync_with_stdio(false);
  while (cin >> W >> H, W && H) {
    for (int i = 0; i < H; ++i)
      cin >> m[i];
    cout << solve() << endl;
  }
  return 0;
}

C++: #include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <fstream>
using namespace std;
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; }
template<class T> inline string toStr(T x) { ostringstream sout; sout << x; return sout.str(); }
typedef vector<int> vi;
typedef vector<vi>  vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n) FOR(i,0,(n)-1)
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = INT_MAX / 10;

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

int main() {
	int W, H;
	while (cin >> W >> H, W) {
		vs field(H);
		pii s;
		REP(i, H) {
			cin >> field[i];
			REP(j, W) {
				if (field[i][j] == '@') {
					s = pii(j, i);
				}
			}
		}

		int ans = 0;
		queue<pii> Q;
		Q.push(s);
		while (!Q.empty()) {
			pii p = Q.front();
			Q.pop();
			ans++;
			
			REP(d, 4) {
				int nx = p.first + dx[d], ny = p.second + dy[d];
				if (nx < 0 || W <= nx || ny < 0 || H <= ny) {
					continue;
				}
				if (field[ny][nx] == '.') {
					field[ny][nx] = '#';
					Q.push(pii(nx, ny));
				}
			}
		}

		cout << ans << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<string>
using namespace std;

int w,h;
string data[50];
int dx[] = {0,0,1,-1};
int dy[] = {1,-1,0,0};
int ans;

void solve(int y, int x){

  if(y < 0 || x < 0 || y == h || x == w) return;
  if(data[y][x] == '#') return;

  data[y][x] = '#';
  ans++;

  for(int i=0;i<4;i++) solve(y+dy[i], x+dx[i]);
}

int main(){

  while(true){
    cin >> w >> h;
    if(!w && !h) break;

    for(int i=0;i<h;i++) cin >> data[i];

    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	if(data[i][j] == '@') ans = 0, solve(i, j); //y,x
      }
    }

    cout << ans << endl;

  }
}

C++: #include <iostream>
#include <cstring>
#include <deque>
using namespace std;

//x, yðüêé
typedef pair<int, int> P;

int w, h;
char map[22][22];

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

int main() {
	while (1) {
		cin >> w >> h;
		if (w == 0 && h == 0) break;		
		P start;
		for (int i = 0; i < h; i++) {
			cin >> map[i];
			char* at = strchr(map[i], '@');
			if (at != NULL) {
				start.first = at - map[i];
				start.second = i;
			}
		}
		//Ç¤\¦³êéH
		/*for (int i = 0; i < h; i++) {
			cout << map[i] << endl;
		}*/
		//[³DæTõ
		deque<P> deq;	//deque<P<int, int>>Æ·éÆ>>ZqÆÔá¦Äðß³êé
		int count = 0;
		deq.push_back(start);
		while (!deq.empty()) {
			P current = deq.back();
			deq.pop_back();
			count++;
			for (int i = 0; i < 4; i++) {
				int nextX = dx[i] + current.first;
				int nextY = dy[i] + current.second;
				if (0 <= nextX && nextX < w && 0 <= nextY && nextY < h
					&& map[nextY][nextX] == '.') {
					deq.push_back(P(nextX, nextY));
					map[nextY][nextX] = '#';//ßçÈ¢æ¤Éßé
				}
			}
		}
		cout << count << endl;
	}
	return 0;
}