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>
#include <vector>
#include <utility>
#include <stack>

using namespace std;

int mapToNum(char c)
{
	switch (c) {
	case '#':
			return -1;
	case '.':
			return 0;
	case '@':
			return 1;
	default:
			return -2;
	}
}

int main()
{
	while (1) {
		int w, h;
		
		cin >> w >> h;
		
		if (w == 0 && h == 0) break;
		
		stack< pair<int, int> > stk;
		vector< vector<int> > tiles(h + 2, vector<int>(w + 2));
		char c;
		
		for (int i = 0; i < w + 2; ++i) tiles.at(0).at(i) = tiles.at(h + 1).at(i) = -1;
		
		for (int i = 1; i < h + 1; ++i) {
			tiles.at(i).at(0) = tiles.at(i).at(w + 1) = -1;
			for (int j = 1; j < w + 1; ++j) {
				cin >> c;
				tiles.at(i).at(j) = mapToNum(c);
				if (c == '@') stk.push(make_pair(i, j));
			}
		}
		
		int num = 1;
		
		while (!stk.empty()) {
			pair<int, int> p = stk.top();
			stk.pop();
			int x = 1, y = 0, t;
			
			for (int i = 0; i < 4; ++i) {
				if (tiles.at(p.first + x).at(p.second + y) == 0) {
					tiles.at(p.first + x).at(p.second + y) = -1;
					stk.push(make_pair(p.first + x, p.second + y));
					++num;
				}
				
				t = -y;
				y = x;
				x = t;
			}
		}
		
		cout << num << endl;
	}
	
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
char m[21][21];
int cnt=1,w,h;
void dfs(int x,int y){
  int i,j;
  m[y][x]='#';
  for(i=-1;i<=1;++i){
    if(y+i<0) continue;
    if(m[y+i][x]=='.'){
      cnt++;
      dfs(x,y+i);
    }
  }
  for(j=-1;j<=1;++j){
    if(x+j<0) continue;
    if(m[y][x+j]=='.'){
      cnt++;
      dfs(x+j,y);
    }
  }
}
int main(){
  int i,j,x,y;
  while(cin>>w>>h){
    if(w==0&&h==0) break;
    for(i=0;i<21;++i)
      for(j=0;j<21;++j)
	m[i][j]=0;
    for(i=0;i<h;++i){
      for(j=0;j<w;++j){
	cin>>m[i][j];
	if(m[i][j]=='@'){
	  x=j;
	  y=i;
	}
      }
    }
    cnt=1;
    dfs(x,y);
    cout<<cnt<<endl;
  }
  return 0;
}

C++: #include<iostream>
#include<queue>
#include<string>
#include<map>
#define F first
#define S second
using namespace std;

char c[33][33];
int w,h;

int solve(int x, int y){
  typedef pair < int, int > P;
  queue < P > que;
  int count = 1;

  que.push( P(x, y) );
  c[y][x] = '#';

  while(!que.empty()){
    P q = que.front(); que.pop();
    
    int dx[] = {0, 0, 1, -1};
    int dy[] = {1, -1, 0, 0};
    
    for(int i=0;i<4;i++){
      int xx = dx[i] + q.F;
      int yy = dy[i] + q.S;

      if(xx < 0 || yy < 0 || xx == w || yy == h) continue;
      if(c[yy][xx] == '#') continue;

      c[yy][xx] = '#';
      que.push( P(xx, yy) );
      count++;
    }

  }

  return count;
}

int main(){

  int sx, sy;
  while(true){
    cin >> w >> h;
    if(!w && !h) break;

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

    cout << solve(sx, sy) << endl;
  }

}

C++: #include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>

using namespace std;
typedef long long ll;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define DEC(i, s) for (int i = (s); i >= 0; i--)

#define SIZE(v) (int)((v).size())
#define MEMSET(v, h) memset((v), h, sizeof(v))
#define FIND(m, w) ((m).find(w) != (m).end())

int w, h;
char field[100][100];

int dfs(int x, int y) {
  if (field[y][x] == '#') { return 0; }
  field[y][x] = '#';
  int ret = 1;
  const int dx[4] = { 1, -1, 0, 0 };
  const int dy[4] = { 0, 0, 1, -1 };
  REP(i, 4) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    ret += dfs(nx, ny);
  }
  return ret;
}

int main() {
  while (scanf("%d %d", &w, &h), w|h) {
    MEMSET(field, '#');
    int sx, sy;
    FOR(y, 1, h + 1) FOR(x, 1, w + 1) {
      scanf(" %c ", &field[y][x]);
      if (field[y][x] == '@') {
        field[y][x] = '.';
        sx = x; sy =y;
      }
    }
    printf("%d\n", dfs(sx, sy));
  }
}