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 _GRIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define int long long

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

signed main()
{
    int h, w;
    while(cin>>w>>h, w) {
        char field[h+1][w+1];
        int sx, sy;
        for(int i=0; i<h; ++i) {
            for(int j=0; j<w; ++j) {
                char c;
                cin >> c;
                if(c=='@') {
                    sx = i;
                    sy = j;
                }
                field[i][j] = c;
            }
        }

        queue<pair<int, int> > q;
        q.push(make_pair(sx, sy));
        int ans = 0;
        while(!q.empty()) {
            ans++;
            pair<int, int> p = q.front();
            q.pop();
            int x = p.first;
            int y = p.second;
            for(int i=0; i<4; ++i) {
                int nx = x + dx[i];
                int ny = y + dy[i];
                if(nx>=0&&nx<=h&&ny>=0&&ny<=w&&field[nx][ny]=='.') {
                    q.push(make_pair(nx, ny));
                    field[nx][ny] = '#';
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include<stdio.h>
char a[21][21];
int c;
void dfs(int y,int x){
  c++;
  a[y][x]=0;
if(y!=0)
  if(a[y-1][x]=='.')
	dfs(y-1,x);
if(y!=20)
  if(a[y+1][x]=='.')
	dfs(y+1,x);
if(x!=0)
  if(a[y][x-1]=='.'&&x!=0)
	dfs(y,x-1);
if(x!=20)
  if(a[y][x+1]=='.'&&x!=20)
	dfs(y,x+1);
  return;
}
int main(){
  int w,h,i,j;
  while(1){
    scanf("%d%d",&w,&h);
    if(w==0&&h==0)break;
    for(i=0;i<21;i++){
      for(j=0;j<21;j++){
	a[i][j]=0;
      }
    }
    for(i=0;i<h;i++){
      for(j=0;j<=w;j++){
	scanf("%c",&a[i][j]);
      }
    }
    c=0;
    for(i=0;i<h;i++){
      for(j=0;j<w;j++){
	if(a[i][j]=='@')dfs(i,j);
      }
    }
    printf("%d\n",c);

   
  }
  return 0;
}

C++: #include<iostream>
#include<string>
#include<queue>
#include <utility>
#include <functional>
//#include<>
using namespace std;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
//#define int long long;
signed main(){
  int w, h;
  while(cin >> w >> h, w){
    int cnt{};
    vector<string> v(h);
    for(int i = 0; i < h; ++i){
      cin >> v[i];
    }
    //initialize
    pair<int, int> p;
    queue<pair<int, int>> q;	
    for(int i = 0; i < h; ++i){
      for(int j = 0; j < w; ++j){
	if(v[i][j] == '@'){
	  p = make_pair(i,j);
	  q.push(p);
	}
      }
    }
    while(!q.empty()){
      pair<int, int> p;
      p = q.front();
      q.pop();

      for(int k = 0; k < 4; ++k){
	int x = p.first + dx[k]; int y = p.second + dy[k];
	if(0 > x || h <= x || 0 > y || w <= y)continue;
	if('.' == v[x][y]){
	  v[x][y] = '@';
	  ++cnt;
	  q.push(make_pair(x, y));
	}
      }
    }
    cout << cnt+1 << endl;
  }
  return 0;
}

C++: #include <cstdio>
using namespace std;

int W, H;
const int SIZE = 20;
char f[SIZE + 1][SIZE + 1];
bool c[SIZE][SIZE];

int dfs(int x, int y){
	if(x < 0 || y < 0 || x >= W || y >= H) return 0;
	if(c[y][x] || f[y][x] == '#') return 0;
	c[y][x] = true;
	
	return 1 + dfs(x + 1, y) + dfs(x, y + 1) + dfs(x - 1, y) + dfs(x, y - 1);
}

int main(){
	int sx, sy;
	
	while(true){
		scanf("%d%d", &W, &H);
		if(H == 0 && W == 0) break;
		for(int i = 0; i < H; i++){
			scanf("%s", f[i]);
		}
		
		for(int y = 0; y < H; y++){
			for(int x = 0; x < W; x++){
				c[y][x] = false;
				if(f[y][x] == '@'){
					sx = x; sy = y;
				}
			}
		}
		
		printf("%d\n", dfs(sx, sy));
	}
	
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <stack>

using std::cin;
using std::cout;
using std::endl;

int main(void) {
	int W,H;
	cin >> W;
	cin >> H;

	while (W!=0||H!=0) {
		int count = 0;
		char c;
		std::stack<int> st_x;
		std::stack<int> st_y;
		int xn,yn;
		std::vector< std::vector<bool> > tile(W+2, std::vector<bool>(H+2));
		for (int i = 0; i < H+2; i++) {
			for (int j = 0; j < W+2; j++) {
				if (i==0||i==H+1||j==0||j==W+1) {
					tile[j][i] = false;
				} else {
					cin >> c;
					if (c=='.') {
						tile[j][i] = true;
					}
					if (c=='#') {
						tile[j][i] = false;
					}
					if (c=='@') {
						tile[j][i] = false;
						st_x.push(j);
						st_y.push(i);
						count++;
					}
				}
			}
		}
		while (!st_x.empty()) {
			xn = st_x.top();
			yn = st_y.top();
			st_x.pop();
			st_y.pop();

			if (tile[xn+1][yn]) {
				st_x.push(xn+1);
				st_y.push(yn);
				tile[xn+1][yn] = false;
				count++;
			}
			if (tile[xn-1][yn]) {
				st_x.push(xn-1);
				st_y.push(yn);
				tile[xn-1][yn] = false;
				count++;
			}
			if (tile[xn][yn+1]) {
				st_x.push(xn);
				st_y.push(yn+1);
				tile[xn][yn+1] = false;
				count++;
			}
			if (tile[xn][yn-1]) {
				st_x.push(xn);
				st_y.push(yn-1);
				tile[xn][yn-1] = false;
				count++;
			}
		}
		cout << count << endl;

		cin >> W;
		cin >> H;
	}
	return 0;
}