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 <vector>
#include <string>
#include <queue>
#include <utility>
#include <algorithm>

constexpr size_t di[]={size_t(-1), 0, 1, 0};
constexpr size_t dj[]={0, size_t(-1), 0, 1};

int testcase_ends() {
  size_t W, H;
  scanf("%zu %zu", &W, &H);

  if (W == 0 && H == 0)
    return 1;

  std::vector<std::string> S(H);
  size_t h=0, w=0;
  for (size_t i=0; i<H; ++i) {
    char buf[32]={};
    scanf("%s", buf);
    S[i] = buf;
    for (size_t j=0; j<W; ++j) {
      if (S[i][j] == '@') {
        h = i;
        w = j;
        break;
      }
    }
  }

  std::queue<std::pair<size_t, size_t>> q;
  q.emplace(h, w);
  int res=1;
  while (!q.empty()) {
    const auto p=q.front();
    q.pop();

    size_t i=p.first, j=p.second;
    S[i][j] = '*';
    for (size_t k=0; k<4; ++k) {
      size_t ni=i+di[k], nj=j+dj[k];
      if (!(ni < H && nj < W)) continue;

      if (S[ni][nj] == '.') {
        q.emplace(ni, nj);
        ++res;
        S[ni][nj] = '*';
      }
    }
  }

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

int main() {
  while (!testcase_ends()) {}
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int INF = 1000000000;
#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
#define rep(i,n) REP(i, 0, n)
typedef pair<int, int> pint;
const int dx[4] = {0, 1, -1, 0}, dy[4] = {1, 0, 0, -1};
int W, H;
int solve(vector<string>& fld, int sx, int sy){
    queue<pint> que;
    int res = 0;
    que.push(pint(sx, sy));
    while(!que.empty()){
        pint now = que.front(); que.pop();
        if(fld[now.second][now.first] == '.'){
            res++;
            fld[now.second][now.first] = '#';
        }else
          continue;
        rep(i, 4){
            int nx = now.first + dx[i], ny = now.second + dy[i];
            if(nx < 0 || ny < 0 || W <= nx || H <= ny)
              continue;
            que.push(pint(nx, ny));
        }
    }
    return res;
}
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(cin >> W >> H && W){
        vector<string> fld(H);
        rep(i, H) cin >> fld[i];
        int sx, sy;
        rep(i, H) rep(j, W)
          if(fld[i][j] == '@'){
              sx = j;
              sy = i;
              fld[i][j] = '.';
              break;
          }
        cout << solve(fld, sx, sy) << endl;
    }
}

C++: #include<stdio.h>
#include<iostream>
#include<list>
#include<map>
#include<math.h>
#include<vector>
#include<algorithm>
#include<string.h>
#include<iomanip>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) range(i,0,b)
#define debug(x) cout << "debug" << x << endl;
using namespace std;

int ans;

void solve(char map[][22], int x, int y){
    ans++;
    map[x][y] = '#';
    if(map[x+1][y] == '.')
        solve(map, x+1, y);
    if(map[x][y+1] == '.')
        solve(map, x, y+1);
    if(map[x-1][y] == '.')
        solve(map, x-1, y);
    if(map[x][y-1] == '.')
        solve(map, x, y-1);
}

int main(){
    int width, height;
    while(cin >> width >> height, width||height){
        char map[22][22], str[22];
        rep(i,22)
            rep(j,22)
                map[i][j] = '#';
        int x, y;
        ans = 0;
        range(i, 1, height + 1){
            cin >> str;
            range(j, 1, width + 1){
                map[i][j] = str[j-1];
                if(map[i][j] == '@'){
                    x = i; y = j;
                }
            }
        }
        solve(map, x, y);
        cout << ans << endl;
    }
}

C++: #include <bits/stdc++.h>
#define For(i, a, b) for(int (i)=(a); (i)<(b); (i)++)
#define rFor(i, a, b) for(int (i)=(a)-1; (i)>=(b); (i)--)
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
using namespace std;
typedef long long lint;
typedef vector<vector<lint>> mat;

int w, h, sx, sy;
char maze[30][30];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

void dfs(int x, int y){
    maze[x][y] = '$';
    rep(i, 4){
        int nx = x + dx[i]; int ny = y + dy[i];
        if(0<=nx && nx<h && 0<=ny && ny<w && maze[nx][ny]=='.') dfs(nx, ny);
    }
}

int main(){
    while(true){
        cin >> w >> h;
        if(w == 0) return 0;
        rep(i, h)rep(j, w){
            cin >> maze[i][j];
            if(maze[i][j] == '@') sx = i, sy = j;
        }
        dfs(sx, sy);
        int cnt = 0;
        rep(i, h)rep(j, w)if(maze[i][j] == '$') cnt++;
        cout << cnt << endl;
    }
}