Go: package main

import (
	"bufio"
	"encoding/json"
	"fmt"
	"os"
	"strconv"
	"strings"
)

var (
	sc = bufio.NewScanner(os.Stdin)
)

type Color int

const (
	ColorBlack Color = iota
	ColorRed
)

var strByColor = map[Color]string{
	ColorBlack: "black",
	ColorRed:   "red",
}

func (c Color) String() string {
	return strByColor[c]
}

type Node struct {
	ID      int
	X, Y    int
	Color   Color
	NextIDs map[int]struct{}
}

func (n *Node) String() string {
	d, _ := json.Marshal(n)
	return string(d)
}

type Map struct {
	Width, Height int
	Nodes         map[int]*Node
}

func (m *Map) GetByID(id int) *Node {
	return m.Nodes[id]
}

func (m *Map) Get(x, y int) *Node {
	return m.Nodes[m.getID(x, y)]
}

func (m *Map) SetColor(x, y int, c Color) {
	m.Nodes[m.getID(x, y)].Color = c
}

func (m *Map) getID(x, y int) int {
	return x + y*m.Width
}

func (m *Map) init() {
	for x := 1; x <= m.Width; x++ {
		for y := 1; y <= m.Height; y++ {
			id := m.getID(x, y)
			m.Nodes[id] = &Node{ID: id, X: x, Y: y}
		}
	}
	for x := 1; x <= m.Width; x++ {
		for y := 1; y <= m.Height; y++ {
			nextIDs := map[int]struct{}{}
			if x > 1 {
				nextIDs[m.getID(x-1, y)] = struct{}{}
			}
			if x < m.Width {
				nextIDs[m.getID(x+1, y)] = struct{}{}
			}
			if y > 1 {
				nextIDs[m.getID(x, y-1)] = struct{}{}
			}
			if y < m.Height {
				nextIDs[m.getID(x, y+1)] = struct{}{}
			}
			m.Get(x, y).NextIDs = nextIDs
		}
	}
}

func NewMap(w, h int) *Map {
	m := &Map{Width: w, Height: h, Nodes: map[int]*Node{}}
	m.init()
	return m
}

type Searcher struct {
	Map        *Map
	queue      []*Node
	touchedIDs map[int]struct{}
	visitedIDs map[int]struct{}
}

func NewSearcher(m *Map) *Searcher {
	return &Searcher{
		Map:        m,
		queue:      []*Node{},
		touchedIDs: map[int]struct{}{},
		visitedIDs: map[int]struct{}{},
	}
}

func (s *Searcher) Search(start *Node) {
	s.enqueue(start)

	for {
		if len(s.queue) == 0 {
			break
		}
		for nextID := range s.dequeue().NextIDs {
			n := s.Map.GetByID(nextID)
			if n.Color == ColorRed || s.hasTouched(n) {
				continue
			}
			s.enqueue(n)
		}
	}
}

func (s *Searcher) VisitedNodeCount() int {
	return len(s.visitedIDs)
}

func (s *Searcher) hasTouched(n *Node) bool {
	_, ok := s.touchedIDs[n.ID]
	return ok
}

func (s *Searcher) enqueue(n *Node) {
	s.queue = append(s.queue, n)
	s.touchedIDs[n.ID] = struct{}{}
}

func (s *Searcher) dequeue() *Node {
	n := s.queue[0]
	s.queue = s.queue[1:]
	s.visitedIDs[n.ID] = struct{}{}
	return n
}

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])

		m := NewMap(w, h)

		// Set colors
		var start *Node
		for y := 1; y <= h; y++ {
			sc.Scan()
			for i, c := range sc.Text() {
				switch c {
				case '.', '@':
					m.SetColor(i+1, y, ColorBlack)
					if c == '@' {
						start = m.Get(i+1, y)
					}
				case '#':
					m.SetColor(i+1, y, ColorRed)
				}
			}
		}

		// Search
		s := NewSearcher(m)
		s.Search(start)
		fmt.Println(s.VisitedNodeCount())
	}
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;
#ifndef FAST_IO
 #define IOS ios_base::sync_with_stdio(false);cin.tie(nullptr);
#endif

#define int long long
#define FILE_IO freopen("input.txt","r",stdin)/#define int long long
#define f first
#define s second
#define endl '\n'
#define all(c) c.begin(),c.end()
#define bp __builtin_popcount
#define debug(x) cerr << #x << " = " << x << endl;

const int MOD = 1e9 + 7;
const int INF = 1e18;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T>void print(T t){for (auto &it : t) cout << it << " ";}
inline int powerexp(int a, int b, int m = MOD){ int res = 1;while(b){if(b&1)res = res * a % m;a = a * a % m,b >>= 1;}return res % m;}
inline int modinv(int a){ return powerexp(a,MOD-2,MOD);}
inline int lcm(int a, int b){ int g = __gcd(a,b);return a/g*b;}
inline int add(int x, int y){ x += y; if (x >= MOD) x -=  MOD; return x;}
inline int mul(int x, int y){ return (x * 1ll * y) % MOD;}
inline int sub(int x, int y){ x -= y; if (x<0) x += MOD; return x ;}

//-------------------< start here >-------------------------// 

const int N = 20;
int g[N][N];
bool visited[N][N];
int w, h,x ,y, cnt;

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

bool check(int x, int y){
  return (x >= 0 && y >= 0 && x < w && y < h);	
}

void dfs(int c, int r){
	visited[c][r] = true;
	cnt++;
	for (int i = 0; i < 4; i++){
	  int nx = r + dx[i];
	  int ny = c + dy[i];
	  if (check(nx, ny) && g[ny][nx] == 1 && !visited[ny][nx]){
		  dfs(ny, nx);  
		}
	}
}

void solve(){
  while(cin >> w >> h && w > 0 && h > 0){
	  cnt = 0;
	  memset(visited, false, sizeof visited);
	  for (int i = 0; i < h; i++){
		for (int j = 0; j < w; j++){
		  char input;
		  cin >> input;
		  if (input == '.') g[i][j] = 1;
		  else if (input == '#') g[i][j] = 0;
		  else if (input == '@'){
			   x = i;
			   y = j; 
			 }	
		  }  
	  }
	  dfs(x, y);
	  cout << cnt << endl;  
	}	
}

//-----------------------------
//comment from here for TOPCODER 
int32_t main(){
   IOS;
   solve();
   //find();
   #ifdef LOCAL_DEFINE
   cerr << "time elapsed " << (double)clock()/CLOCKS_PER_SEC << endl;
   #endif
}

C++: #include <iostream>

using namespace std;

const int MAX_SIZE = 21;
int w, h;

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

char field[MAX_SIZE][MAX_SIZE];

bool ok(int x, int y) {
	return (x >= 0 && x < w && y >= 0 && y < h) && field[y][x] == '.';
}

int solve(int x, int y) {
	int ret = 0;
	for (int i = 0; i < 4; ++i) {
		int curx = x + dx[i],
			cury = y + dy[i];

		if (ok(curx, cury)) {
			field[cury][curx] = '#';
			ret += solve(curx, cury) + 1;
		}
	}
	return ret;
}

int main() {
	while (true) {
		cin >> w >> h;
		if (w == 0 && h == 0) break;
		int x = -1, y = -1;
		for (int i = 0; i < h; ++i) for (int j = 0; j < w; ++j) {
			cin >> field[i][j];
			if (field[i][j] == '@') {
				field[i][j] = '#';
				x = j;
				y = i;
			}
		}

		cout << solve(x, y) + 1 << endl;

	}

	return 0;
}

C++: 
//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1130&lang=jp

#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<cmath>
#include<climits>
#include<ctime>
#include<cstring>
#include<numeric>

#define ALL(v) (v).begin(),(v).end()
#define REP(i,p,n) for(int i=p;i<(int)(n);++i)
#define rep(i,n) REP(i,0,n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list) cout << "{ "; for(auto nth : list){ cout << nth << " "; } cout << "}" << endl;

using namespace std;

int W,H;

void dfs(vector<string> &field, vector<vector<bool> > &move,int now_x, int now_y)
{
	int dx[4] = {1,0,-1,0};
	int dy[4] = {0,1,0,-1};

	if(now_x < 0 || now_x >= W || now_y < 0 || now_y >= H){ return; }
	
	if(field[now_y][now_x] == '#'){ return; }

	if(move[now_y][now_x]){ return; }

	move[now_y][now_x] = true;

	rep(i,4){
		dfs(field, move, now_x+dx[i], now_y+dy[i]);
	}

	return;
}

int main() {

	while(cin >> W >> H && W && H)
	{

		vector<string> field(H);
		vector<vector<bool> > move(H, vector<bool>(W,false));

		rep(y,H){
			cin >> field[y];
		}

		pair<int, int> start;
		rep(y,H){
			rep(x,W){
				if(field[y][x] == '@'){start = make_pair(x,y);}
			}
		}

		
		dfs(field, move, start.first, start.second);

		int count=0;
		rep(y,H){
			rep(x,W){
				if(move[y][x]){ count++; }
			}
		}

		cout << count << endl;

	}
	return 0;
}