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++: #include <cstdio>
#include <utility>
#include <queue>

using namespace std;

bool input();
int bfs();

int W, H;
char tile[20][20];		//添え字は[縦][横]
const int INF = 1000;
typedef pair<int, int> P;
int sw, sh;
int d[20][20];		//各点への最短距離の配列
int dw[4] = {1, 0, -1, 0};
int dh[4] = {0, 1, 0, -1};		//左から順に　→↓←↑　を表す
int count=0;		//発見した黒いタイルの総数を記憶


int main()
{
	for (;;)
	{
		if (!input()) return 0;
		count = 0;
		int ans = bfs();
		printf("%d\n", ans);
	}
}


int bfs()
{
	// printf("bfs()\n");
	queue<P> que;
	for (int i=0; i<20; i++)
		for (int i2=0; i2<20; i2++)
			d[i][i2] = INF;
	que.push(P(sh, sw));
	d[sh][sw] = 0;
	count++;
	
	while (que.size())
	{
		P p = que.front();
		que.pop();
		
		for (int i=0; i<4; i++)
		{
			int nw = p.second + dw[i];
			int nh = p.first + dh[i];
			
			// printf("nw=%d nh=%d\n", nw, nh);
			// printf("tile[%d][%d]=%c\n", nh, nw, tile[nh][nw]);
			// printf("d[%d][%d]=%d\n", nh, nw, d[nh][nw]);
			
			if (0<=nw && nw < W && 0<=nh && nh<H && tile[nh][nw]!='#' && d[nh][nw]==INF)
			{
				que.push(P(nh, nw));
				d[nh][nw] = d[p.first][p.second] + 1;
				count++;
			}
		}
	}
	return count;
}




//１つのデータセットに対して１回呼ばれる
bool input()
{
	scanf("%d %d", &W, &H);
	// printf("W=%d H=%d\n", W, H);
	if (W==0) return false;

	for (int i=0; i<H; i++)
	{
		scanf("%s", tile[i]);
		for (int i2=0; i2<W; i2++)
		{
			if (tile[i][i2]=='@')
			{
				sw = i2;
				sh = i;
			}
		}
		// printf("tile[%d]=%s\n", i, tile[i]);
		// printf("%c\n",tile[i][5]);
	}
	
	return true;
}

C++: #include "bits/stdc++.h"

#define REP(i,n) for(int i=0;i<n;++i)
#define RREP(i,n) for(int i=n-1;i>=0;--i)
#define FOR(i,m,n) for(int i=m;i<n;++i)
#define RFOR(i,m,n) for(int i=m-1;i>=n;--i)
#define ALL(v) v.begin(),v.end()
#define PB(a) push_back(a)
#define INF 1000000001
#define MOD 1000000007

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

using namespace std;

void dfs(const vector<string> &v, vector<vector<bool>> &b, int x, int y, int& cnt) {
	b[y][x] = true;
	cnt++;
	REP(i, 4) {
		int nx = x + dx[i], ny = y + dy[i];
		if (nx<v[y].size() && nx >= 0 && ny >= 0 && ny<v.size() &&v[ny][nx] == '.' && !b[ny][nx])
			dfs(v, b, nx, ny,cnt);
	}
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int h, w;
	while (cin >> w >> h&&h) {
		vector<string> v(h);
		REP(i, h) {
			cin >> v[i];
		}
		vector<bool> b(w, false);
		vector<vector<bool> > visited(h, b);
		int y=0, x=0;
		REP(i, h) {
			if (v[i].find('@')!=size_t(-1)) {
				y = i;
				x = v[i].find('@');
			}
		}
		int cnt = 0;
		dfs(v, visited, x, y, cnt);
		cout << cnt << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<list>
using namespace std;
int sum = 0;
char a[20][20];
void solve(int h, int w,  int i,int  j) {
	if (i + 1 < h&&a[i + 1][j] == '.') {
		sum++;
		a[i + 1][j] = '#';
		solve(h, w,  i+1, j);
	}
	if (i - 1 >= 0&&a[i - 1][j] == '.') {
		sum++;
		a[i - 1][j] = '#';
		solve(h, w,  i-1, j);
	}
	if (j + 1 < w&&a[i][j+1] == '.') {
		sum++;
		a[i][j+1] = '#';
		solve(h, w,  i, j+1);
	}
	if (j - 1 >= 0&&a[i][j-1] == '.') {
		sum++;
		a[i][j-1] = '#';
		solve(h, w,  i, j-1);
	}
}
int main() {
	int h, w;
	while (cin >> w >> h&&w != 0 && h != 0) {
		string b;
		int h2, w2;
		for (int i = 0; i < h; i++) {
			cin >> b;
			for (int j = 0; j < w; j++) {
				a[i][j] = b[j];
				if (a[i][j] == '@') {
					h2 = i;
					w2 = j;
				}
			}
		}
		sum = 0;
		solve(h, w,  h2, w2);
		cout << ++sum << endl;
	}
}

C++: #include<iostream>
#include<algorithm>
#include<stack>
#include<queue>
#include<string>
#include<string.h>
#include<set>
#include<functional>
using namespace std;

#define INF 1<<21
#define MOD 1000000007

char field[25][25];

int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,1,0,-1 };
int W, H;
int ans;

void dfs(int x, int y) {
  field[y][x] = '#';
  ans++;
  for (int i = 0; i < 4; i++) {
	int nx = x + dx[i], ny = y + dy[i];
	if (1 <= nx&&nx <= W && 1 <= ny&&ny <= H&&field[ny][nx] == '.')dfs(nx, ny);
  }
  return;
}

int main() {
  while (1) {
	cin >> W >> H;
	if (H == 0 && W == 0)return 0;
	int sx, sy;
	memset(field, 0, sizeof(field));
	ans = 0;
	for (int i = 1; i <= H; i++) {
	  for (int j = 1; j <= W; j++) {
		cin >> field[i][j];
		if (field[i][j] == '@') {
		  sy = i;
		  sx = j;
		}
	  }
	}
	dfs(sx, sy);
	cout << ans << endl;
  }

}