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>
//#define int long long
//#define _DEBUG
#include <queue>
#include <vector>
#include <string>
#include <utility>
using namespace std;
typedef pair<int,int> pos;
int const infty = -1, none = -9, dx[] = {1,-1,0,0}, dy[]={0,0,1,-1};
int W, H;
string Map[23]; //Map[i][j]:i行j列目
bool reached[23][23];
pos firstPos;
queue<pos> Q;
int count_reached(){
    int r = 0;
    for(int i=0; i<H; i++){
        for(int j=0; j<W; j++){
            if(reached[i][j]) r++;
        }
    }
    return r;
}
bool valid(int x, int y){
    return x>=0 && x<W && y>=0 && y<H && Map[y][x]!='#';
}
int solve(){
    Q.push(firstPos);
    reached[firstPos.second][firstPos.first] = true;
    while(true){
        auto top = Q.front(); Q.pop();
        int x = top.first, y = top.second;
        for(int d=0; d<4; d++){
            if(valid(x+dx[d], y+dy[d]) && !reached[y+dy[d]][x+dx[d]]){
                Q.push(make_pair(x+dx[d], y+dy[d]));
                reached[y+dy[d]][x+dx[d]] = true;
            }
        }
        if(Q.empty()) break;
        //cout << x<<","<<y<<", "<< Q.size()<<endl;
    }
    return count_reached();
}
signed main(){    
    while(cin >> W >> H && W > 0){
        firstPos = make_pair(none, none);
        for(int i=0; i<H; i++){
            cin >> Map[i];
            for(int j=0; j<W; j++){
                if(Map[i][j] == '@') firstPos = make_pair(j, i);
                reached[i][j] = false;
            }
        }
        cout << solve() << endl;
#ifdef _DEBUG
        cout << firstPos.first<<firstPos.second<< endl;
        for(int i=0; i<H; i++){
            for(int j=0; j<W; j++)cout << Map[i][j];
            cout << endl;
        }
        for(int i=0; i<H; i++){
            for(int j=0; j<W; j++){
                cout<<reached[i][j];
            }
            cout << endl;
        }
#endif
    }
}

C++: #include <cstdio>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

#define TEMP_T template<typename T>
TEMP_T void sort(T& v){ sort(v.begin(), v.end()); }
TEMP_T void revs(T& v){ reverse(v.begin(), v.end()); }
TEMP_T void uniq(T& v){ sort(v); v.erase(unique(v.begin(), v.end())); }
TEMP_T T    cums(T& v){ T r; r.push_back(v[0]); for(int i=1; i<v.size(); i++) r.push_back(r[i-1] + v[i]); return r; }
TEMP_T void show(T& v, char delim=' ', char end='\n'){ for(int i=0; i<v.size()-1; i++) cout << v[i] << delim; cout << v[v.size()-1] << end; }

static inline int in(){ int x; scanf("%d", &x); return x; }

struct UF {
  vector<int> data;
  UF(int size) : data(size, -1) {}
  void merge(int x, int y){
    x = root(x), y = root(y);
    if (x != y){
      if (data[x] > data[y]) swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
  }
  int root(int x){
    return data[x] < 0 ? x : data[x] = root(data[x]);
  }
  int size(int x){
    return -data[root(x)];
  }
};

struct Solver {
  int w, h;
  vector<string> field;
  Solver(int w, int h) : w(w), h(h) {}
  int read_input(){
    for (int i = 0; i < h; i++){
      string s;
      cin >> s;
      field.push_back(s);
    }
    return (0);
  }

  int solve(){
    UF uf(w * h);
    int pos;
    for (int y = 0; y < h; y++){
      for (int x = 0; x < w; x++){
        if (field[y][x] == '#') continue;
        for (int d = 0; d < 4; d++){
          static const int dx[] = {0, 0, 1, -1};
          static const int dy[] = {1, -1, 0, 0};
          int nx = x + dx[d];
          int ny = y + dy[d];
          if (nx < 0 || nx >= w || ny < 0 || ny >= h) continue;
          if (field[ny][nx] != '#') uf.merge(y * w + x, ny * w + nx);
        }
        if (field[y][x] == '@') pos = y * w + x;
      }
    }
    return uf.size(pos);
  }
};

int main()
{
  int w, h;
  while (w = in(), h = in()){
    Solver s(w, h);
    s.read_input();
    printf("%d\n", s.solve());
  }

  return (0);
}

C++: #include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <fstream>
#include <queue>

#pragma warning( disable: 4996 )

using namespace std;

typedef long long ll;

#define INF 100000000
#define EPS 1e-9

#define MAX_N 50
#define MAX_R 50

#define MAX_W 20
#define MAX_H 20

int W, H;
int ans;
char maze[MAX_W][MAX_H];

void dfs(int sx, int sy){
	
	maze[sy][sx] = '#';

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

	for (int i = 0; i < 4; i++){
		int nx = sx + dx[i];
		int ny = sy + dy[i];
		if (nx >= 0 && nx < W && ny >= 0 && ny < H && maze[ny][nx] != '#'){
			ans++;
			dfs(nx, ny);
		}
	}

}

int main(){

	while (true){
		cin >> W >> H;
		if (W == 0 && H == 0){
			break;
		}
		int sx;
		int sy;
		for (int i = 0; i < H; i++){
			for (int j = 0; j < W; j++){
				cin >> maze[i][j];
				if (maze[i][j] == '@'){
					sx = j;
					sy = i;
				}
			}
		}

		ans = 1;
		dfs(sx,sy);
		cout << ans << endl;

	}


	return 0;

}