Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

var (
	sc = bufio.NewScanner(os.Stdin)
)

type Color int

const (
	ColorBlack Color = iota
	ColorRed
)

type Node struct {
	ID    int
	X, Y  int
	Color Color
	Nexts []*Node
}

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])

		// Initialize map
		m := map[int]map[int]*Node{}
		for x := 1; x <= w; x++ {
			m[x] = map[int]*Node{}
			for y := 1; y <= h; y++ {
				m[x][y] = &Node{ID: x + y*w, X: x, Y: y}
			}
		}
		for x := 1; x <= w; x++ {
			for y := 1; y <= h; y++ {
				nexts := []*Node{}
				if n, ok := m[x-1][y]; ok {
					nexts = append(nexts, n)
				}
				if n, ok := m[x][y-1]; ok {
					nexts = append(nexts, n)
				}
				if n, ok := m[x+1][y]; ok {
					nexts = append(nexts, n)
				}
				if n, ok := m[x][y+1]; ok {
					nexts = append(nexts, n)
				}
				m[x][y].Nexts = nexts
			}
		}

		// Set colors
		queue := []*Node{}
		touchedIDs := map[int]struct{}{}
		for y := 1; y <= h; y++ {
			sc.Scan()
			for i, c := range sc.Text() {
				switch c {
				case '.', '@':
					n := m[i+1][y]
					n.Color = ColorBlack
					if c == '@' {
						queue = append(queue, n)
						touchedIDs[n.ID] = struct{}{}
					}
				case '#':
					m[i+1][y].Color = ColorRed
				}
			}
		}

		// Search
		visitedIDs := map[int]struct{}{}
		for {
			if len(queue) == 0 {
				break
			}
			n := queue[0]
			queue = queue[1:]
			visitedIDs[n.ID] = struct{}{}
			for _, next := range n.Nexts {
				if next.Color == ColorRed {
					continue
				}
				if _, ok := touchedIDs[next.ID]; !ok {
					queue = append(queue, next)
					touchedIDs[n.ID] = struct{}{}
				}
			}
		}
		fmt.Println(len(visitedIDs))
	}
}

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

#define BLACK '.'
#define RED '#'
#define ST '@'

int w,h, ans=0;
char t[100][100];

int rec(int x, int y){
  if( x<0 || x>=h ) return 0;
  if( y<0 || y>=w ) return 0;

  if( t[x][y] == RED ) return 0;
  t[x][y] = RED;

  int dx[4] = {0,0,1,-1};
  int dy[4] = {1,-1,0,0};
  int r=1;
  for( int d=0;d<4;d++ )
     r+= rec(x+dx[d], y+dy[d]);
  return r;
}
int main(){
  while( cin>>w >> h, w){
    int si, sj;
    for( int i=0;i<h;i++ )
      for( int j=0;j<w;j++) {
	cin >> t[i][j];
	if( t[i][j] == ST ){
	  si=i; sj=j;
	}
      }
    cout << rec(si,sj) << endl;
  }
}

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;
    }
}