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 <stdio.h>
#include <queue>
#include <string.h>
using namespace std;	
int W,H;
char c[101][101];
bool visited[100][100];
 
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
 	
void compute();//????????????????????£?¨? 
	
int main(){
	
	while(scanf("%d %d",&W,&H),W |H){
	    int i;
		
		for(i=0;i<H;i++){
			scanf(" %s",&c[i][0]);
		}
	}
	return 0;
}

void compute(){
	
	memset(visited,false,sizeof visited);  //{????????????????????????1bit??????????????????1bit????????§?????????} 
	
	queue<int> que;
	int i,j;
	for(j=0;j<W;j++){ 
	 for(i=0;i<H;i++){
	  if(c[i][j]=='@'){
	  	que.push(j+i*H);
	  	visited[i][j]=true;
	  	 break;
	  }
	 }
	}
  int answer=1;
  while(!que.empty()){
	int cur=que.front();
	        que.pop();
	        int x=cur%H;
	        int y=cur/H;
    

  for(i=0;i<4;i++){
	int nx=x+dx[i],ny=y+dy[i];
	
  
  
  if(!(0<=nx&&nx<W&&0<=ny&&ny<H))continue;
  if(c[ny][nx]=='#'||visited[ny][nx])continue;

  que.push(nx+ny*H);
  visited[ny][nx]=true;
++answer;
    }
   }
 cout << answer << endl;
 }

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

int main(){
  //std::ifstream in("input.txt");
  //std::cin.rdbuf(in.rdbuf());
  int ans[1024] = {0};
  int dataCounter = 0;
  int i = 0;
  while(1){
    int visited[20][20] = {0};
    int data[20][20];
    int x,y;
    int w = 0;
    int h = 0;
    queue<int> xst;
    queue<int> yst;
    cin >> w;
    cin >> h;
    if(w == 0 && h == 0) break;
    for(i=0;i<h;i++){
      int inputCounter = 0;
      char d;
      while(cin >> d){
        if(d == '.')
          data[i][inputCounter] = 0;
        else if(d == '#')
          data[i][inputCounter] = 1;
        else if(d == '@'){
          data[i][inputCounter] = 2;
          x = inputCounter;
          y = i;
        }
        if(inputCounter == w-1)break;
        inputCounter++;
      }
    }
    int j=0;
    xst.push(x);
    yst.push(y);
    while(!xst.empty()){
      x = xst.front();
      y = yst.front();
      xst.pop();
      yst.pop();
      if(x < 0 || w <= x || y < 0 || h <= y)continue;
      if(visited[y][x]==1 || data[y][x] == 1)continue;
      visited[y][x] = 1;
      ans[dataCounter]++;
      xst.push(x+1);
      yst.push(y);
      xst.push(x);
      yst.push(y+1);
      xst.push(x-1);
      yst.push(y);
      xst.push(x);
      yst.push(y-1);
    }

    dataCounter++;
  }
  for(i=0;i<dataCounter;i++){
    cout << ans[i] << endl;
  }
  return 0;
}

C++: #include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <fstream>
#include <queue>

#pragma warning( disable: 4996 )

using namespace std;

typedef long long ll;

#define INF 100000000
#define EPS 1e-9

#define MAX_N 50
#define MAX_R 50

#define MAX_W 20
#define MAX_H 20

int W, H;
int ans = 0;
char maze[MAX_W][MAX_H];

void dfs(int sx, int sy){
	
	int dx[] = { 1, 0, -1, 0 };
	int dy[] = { 0, 1, 0, -1 };

	for (int i = 0; i < 4; i++){
		int nx = sx + dx[i];
		int ny = sy + dy[i];
		if (nx >= 0 && nx < W && ny >= 0 && ny < H && maze[ny][nx] != '#'){
			maze[ny][nx] = '#';
			ans++;
			dfs(nx, ny);
		}
	}

}

int main(){

	while (true){
		cin >> W >> H;
		if (W == 0 && H == 0){
			break;
		}
		int sx;
		int sy;
		for (int i = 0; i < H; i++){
			for (int j = 0; j < W; j++){
				cin >> maze[i][j];
				if (maze[i][j] == '@'){
					sx = j;
					sy = i;
				}
			}
		}

		ans = 0;
		dfs(sx,sy);
		cout << ans << endl;

	}


	return 0;

}

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

#define iota(i,n,b,s) for(int i=int(b);i!=int((b)+(s)*(n));i+=(s))
#define range(i,n,m) iota(i,(((n)>(m))?((n)-(m)+1):((m)-(n)+1)),(n),((n)>(m)?-1:1))
#define rep(i,n) iota(i,(n),0,1)

#define INF (1e9)
#define EPS (1e-9)
#define cons(a,b) (make_pair(a,b))
#define car(a) (a.first)
#define cdr(a) (a.second)
#define cadr(a) (car(cdr(a)))
#define cddr(a) (cdr(cdr(a)))
#define all(a) a.begin(), a.end()
#define trace(var) cerr<<">>> "<<#var<<" = "<<var<<endl;

typedef long long INTEGER;
typedef double FLOAT;

template<class S, class T>
ostream& operator<<(ostream& os, pair<S,T> p) {
  os << '(' << car(p) << ", " << cdr(p) << ')';
  return os;
}

template<class T>
ostream& operator<<(ostream& os, vector<T> v) {
  os << v[0];
  for (int i=1, len=v.size(); i<len; ++i) os << ' ' << v[i];
  return os;
}

int main()
{
entry:;

  int w, h; cin >> w >> h;
  if (!w && !h) return 0;

  int f[h][w];
  int ci, cj;
  rep (i, h) {
    string s; cin >> s;
    rep (j, s.size()) {
      if (s[j] == '.') {
        f[i][j] = 1;
      } else if (s[j] == '@') {
        f[i][j] = 1;
        ci = i;
        cj = j;
      } else {
        f[i][j] = 0;
      }
    }
  }

  stack<pair<int,int>> s;
  s.push(cons(ci, cj));

  int ans = 0;
  while (!s.empty()) {
    pair<int, int> x = s.top(); s.pop();
    if (f[car(x)][cdr(x)] == 0) continue;
    f[car(x)][cdr(x)] = 0;
    ++ans;

    if (car(x) > 0) s.push(cons(car(x) - 1, cdr(x)));
    if (cdr(x) > 0) s.push(cons(car(x), cdr(x) - 1));
    if (car(x) < h - 1) s.push(cons(car(x) + 1, cdr(x)));
    if (cdr(x) < w - 1) s.push(cons(car(x), cdr(x) + 1));

  }

  cout << ans << endl;

  goto entry;

  return 0;
}