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 "bits/stdc++.h"
using namespace std;
#define rep(i,a,n) for(ll i=a;i<n;i++)
#define ALL(s) s.begin(),s.end()
#define P pair<ll,ll>
#define vl vector<ll>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vvl vector<vector<ll>>
#define print(n) cout<<n<<endl
const int M=100010;
const int MOD=1000000007;
const int inf=1000000007;
const long long INF=1000000000000000007;
using ll=long long;
int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};

int ans=0;
int w,h;
char a[22][22];
bool f[22][22];
void dfs(int H,int W){
	if(W<0||W>=w||H<0||H>=h||a[H][W]=='#'||f[H][W]==true)return;
	if(a[H][W]=='.'&&f[H][W]==false){
		f[H][W]=true;
		ans++;
	}
	rep(i,0,4)dfs(H+dy[i],W+dx[i]);
	return;
}

int main(){
	while(1){
		ans=0;
		rep(i,0,22)rep(j,0,22)f[i][j]=false;
		cin>>w>>h;
		int start_x,start_y;
		if(w==0)break;
		rep(i,0,h)rep(j,0,w){
			cin>>a[i][j];
			if(a[i][j]=='@')start_y=i,start_x=j;
		}
		dfs(start_y,start_x);
		print(ans+1);
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

using ll = long long;
#define debug(x) cerr << #x << ": " << x << endl;
#define line() cerr << "---------------" << endl;

int H, W;
char board[30][30];
bool visited[30][30];
int dx[] = {0, 0, 1, -1};
int dy[] = {-1, 1, 0, 0};

void dfs(int x, int y) {
   for (int i = 0; i < 4; i++) {
      int nx = x + dx[i], ny = y + dy[i];
      if (nx < 0 || nx >= H || ny < 0 || ny >= W) continue;
      if (board[nx][ny] == '#') continue;
      if (visited[nx][ny]) continue; // これ必要な理由がわからない
      visited[nx][ny] = true;
      dfs(nx, ny);
   }
}

int main() {
   while (cin >> W >> H, W && H) {
      memset(visited, false, sizeof(visited));

      int sx = 0, sy = 0;
      for (int i = 0; i < H; i++) {
         for (int j = 0; j < W; j++) {
            cin >> board[i][j];
            if (board[i][j] == '@') {
               sx = i, sy = j;
            }
         }
      }

      visited[sx][sy] = true;
      dfs(sx, sy);

      int cnt = 0;
      for (int i = 0; i < H; i++) {
         for (int j = 0; j < W; j++) {
            if (visited[i][j]) cnt++;
         }
      }
      cout << cnt << endl;

   }

   return 0;
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
typedef unsigned int ui;
int main(){
	int w, h;
	string s;
	int i, j;
	while (cin >> w >> h) {
		if (w == 0)break;
		char tile[22][22]; int used[22][22];
		for (i = 0; i < 22; i++) {
			for (j = 0; j < 22; j++) {
				used[i][j] = 0; tile[i][j] = '#';
			}
		}
		vector<int> q1,q2;
		for (j = 1; j <= h; j++) {
			cin >> s;
			for (i = 1; i <= w; i++) {
				tile[i][j] = s[i - 1];
				if (s[i - 1] == '@') {
					q1.push_back(i); q2.push_back(j); used[i][j] = 1;
				}
			}
		}
		while (!q1.empty()) {
			int x = q1[0]; int y = q2[0];
			q1.erase(q1.begin() + 0); q2.erase(q2.begin() + 0);
			if (used[x - 1][y] == 0 && tile[x - 1][y]=='.') {
				q1.push_back(x - 1); q2.push_back(y); used[x - 1][y] = 1;
			}
			if (used[x + 1][y] == 0 && tile[x + 1][y] == '.') {
				q1.push_back(x + 1); q2.push_back(y); used[x + 1][y] = 1;
			}
			if (used[x][y-1] == 0 && tile[x][y-1] == '.') {
				q1.push_back(x); q2.push_back(y - 1); used[x][y - 1] = 1;
			}
			if (used[x][y+1] == 0 && tile[x][y+1] == '.') {
				q1.push_back(x); q2.push_back(y + 1); used[x][y + 1] = 1;
			}
		}
		int count = 0;
		for (i = 0; i < 22; i++) {
			for (j = 0; j < 22; j++) {
				if (used[i][j] == 1)count++;
			}
		}
		cout << count << endl;
	}
	return 0;
}

C++: #include <stdio.h>
#include <string.h>
int cnt = 0;
int W, H;

void search(char data[][22], int self_i, int self_j) {
  //  printf("self_i = %d  self_j = %d\n", self_i, self_j);
  data[self_i][self_j] = '#';
  cnt++;
  if (self_i-1 >= 0 && data[self_i-1][self_j] == '.') {
	search(data, self_i-1, self_j);
  }
  if (self_j-1 >= 0 && data[self_i][self_j-1] == '.') {
	search(data, self_i, self_j-1);
  }
  if (self_j+1 <= W && data[self_i][self_j+1] == '.') {
	search(data, self_i, self_j+1);
  }
  if (self_i+1 <= H && data[self_i+1][self_j] == '.') {
	search(data, self_i+1, self_j);
  }
}

int main(void) {
  while(1) {
	cnt = 0;
	scanf("%d%d", &W, &H);
	if (W == H && W == 0) break;
	char data[22][22] = {{0}};
	for (int i = 0; i < H; i++) {
	  scanf("%s", data[i]);
	}
	int self_i, self_j;
	for (int i = 0; i < H; i++) {
	  for (int j = 0; j < W; j++) {
		if (data[i][j] == '@') {
		  self_j = j;
		  self_i = i;
		  goto L1;
		}
	  }
	}
  L1:
	search(data, self_i, self_j);
	printf("%d\n", cnt);


  }
  return 0;
}