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<iostream>
#include<vector>
#include<algorithm>
#include<cctype>
#include<utility>
#include<string>
#include<cmath>
#include<cstring>
#include<queue>
#include<map>
#include<queue>

#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 1000000007
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;
typedef long long int ll;

#define ARRAY_MAX 25

int h,w;
char mp[ARRAY_MAX][ARRAY_MAX];
int sx,sy;
int dx[4] = {1,0,0,-1};
int dy[4] = {0,1,-1,0};
int ans = 1;

void dfs(int sy,int sx){

    mp[sy][sx] = '$';//今いるところを塗りつぶす
    
    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 && mp[ny][nx] == '.'){
            //mpの範囲内で.があれば調べる
            ans++;
            dfs(ny,nx);
        }
    }
}

int main(){

    while(cin >> w >> h,w){

        REP(i,h){
            REP(j,w){
                cin >> mp[i][j];
                if(mp[i][j] == '@'){
                    sy = i;
                    sx = j;//スタート一を保存
                }
            }
        }
        dfs(sy,sx);
        cout << ans << endl;
        ans = 1;
    }



    return 0;
}

C++: #include <iostream>
using namespace std;
int ch[23][23],cnt;
void dfs(int,int);
int main(){
  int w,h,i,j,k,l;
  string in[21];
  while(1){
    cnt=0;
    cin >> w >> h;
    if(w==0&&h==0) break;
    for(i=0;i<h;i++){
      cin >> in[i];
    }
    for(i=0;i<h+2;i++){
      for(j=0;j<w+2;j++){
    ch[i][j]=1;
      }
    }
    for(i=0,k=1;k<h+1;i++,k++){
      for(j=0,l=1;l<w+1;l++,j++){
    if(in[i][j]=='.') ch[k][l]=0;
    else if(in[i][j]=='#') ch[k][l]=1;
    else ch[k][l]=2;
      }
    }
    for(i=1;i<=h;i++){
      for(j=1;j<=w;j++){
    if(ch[i][j]==2) dfs(i,j);
      }
    }
    cout << cnt << endl;
  }
  return 0;
}
void dfs(int i,int j){
  if(ch[i][j]==1) return ;
  ch[i][j]=1;
  cnt++;
  dfs(i+1,j);
  dfs(i-1,j);
  dfs(i,j-1);
  dfs(i,j+1);
}

C++: #include<cstdio>

#define MAX 22
#define INV (0)
#define REP(i,a,e) for(int i=(a); i<=(e); ++i)

void dfs(int map[][MAX], int i, int j, int &ans)
{
	const int di[] = {-1,0,1,0};
	const int dj[] = {0,1,0,-1};
	if( map[i][j] == INV || map[i][j] == '#' ){
		return ;
	}
	if( map[i][j] == '.' ){
		++ans;
		map[i][j] = '#';
	}
	for(int k = 0; k < 4; ++k){
		dfs( map, i + di[k], j + dj[k], ans );
	}
}

int main()
{
	while(true){
		int si, sj;
		int ans = 1;
		int map[MAX][MAX] = {{INV,},};
		int W,H;

		scanf("%d%d", &W, &H);

		if( W == 0 && H == 0 ) break;

		REP(i,1,H){
			char c;
			scanf("%c", &c);
			REP(j,1,W){
				scanf("%c", &c);
				map[i][j] = c;
				if( map[i][j] == '@' ){
					si = i;
					sj = j;
				}
			}
		}

		dfs(map,si,sj,ans);

		printf("%d\n", ans);
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <string>
#include <sstream>
#include <algorithm>
#include <math.h>
//#include <map>
#include <iomanip>
#include <vector>

using namespace std;

int W, H, sw, sh, cnt;
char tiles[20][20];
int dw[4] = {-1, 0, 0, 1};
int dh[4] = {0, -1, 1, 0};


void dfs(int w, int h)
{
	tiles[h][w] = '#';
	cnt++;
	
	for (int i=0; i<4; i++)
	{
		int nw = w + dw[i];
		int nh = h + dh[i];
		
		if (0<=nw && nw<W && 0<=nh && nh<H && tiles[nh][nw]=='.')
		{
			dfs(nw, nh);
		}
	}
}


int main()
{
	while (cin >> W >> H)
	{
		if (W == 0 && H == 0) break;
		
		//入力
		for (int i=0; i<H; i++)
		{
			cin >> tiles[i];
		}
		
		//スタート地点を探す
		for (int i=0; i<H; i++)
		{
			for (int j=0; j<W; j++)
			{
				if (tiles[i][j] == '@')
				{
					sw = j;
					sh = i;
					break;
				}
			}
		}
		
		cnt = 0;
		dfs(sw, sh);
		cout << cnt << endl;
		
		//cout << "sw = " << sw << " : sh = " << sh << endl;
		
		/*
		//テスト出力
		for (int i=0; i<H; i++)
		{
			cout << tiles[i] << endl;
		}
		*/
	}
	
	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define shosu(n) setprecision(n)
using namespace std;
char s[100][100];
int H, W, stx, sty;
void dfs(int x, int y) {
  if (x < 0 || y < 0 || x >= W || y >= H)
    return;
  if (s[y][x] == '.') {
    s[y][x] = 'x';
  } else
    return;
  dfs(x + 1, y);
  dfs(x - 1, y);
  dfs(x, y + 1);
  dfs(x, y - 1);
}
int main() {
  while (cin >> W >> H) {
    int ans = 0;
    if (H == 0 && W == 0)
      break;
    rep(i, H) rep(j, W) {
      cin >> s[i][j];
      if (s[i][j] == '@') {
        stx = j;
        sty = i;
      }
    }
    s[sty][stx] = '.';
    dfs(stx, sty);
    rep(i, H) rep(j, W) {
      if (s[i][j] == 'x')
        ans++;
    }
    cout << ans << endl;
  }
  return 0;
}