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 <string>
#include <iostream>
using namespace std;
  
int w,h,ans,dx[]={0,-1,0,1}, dy[]={-1,0,1,0};
char c[22][22];
 
void solve(int x,int y){
    ans++;
    c[x][y]='#';
    for(int i=0;i<4;i++){
        int nx=x+dx[i], ny=y+dy[i];
        if(0<=nx && nx<w && 0<=ny && ny<h && c[nx][ny]=='.')solve(nx,ny);
    }
}
 
int main(){
    while(cin>>w>>h, w){
        ans=0;
        int x,y;
        for(int i=0;i<h;i++){
            for(int j=0; j<w; j++){
                cin>>c[j][i];
                if(c[j][i]=='@')x=j,y=i;
            }
        }
        
        solve(x,y);
        cout<<ans<<endl;
    }
}

C++: #include<iostream>
#include<queue>

using namespace std;

typedef pair<int,int> P;
int W,H;
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
char ma[21][21];
int sx,sy;

int bfs(){
  int count=1;
  int d[21][21];
  queue<P> que;
   for(int i=0;i<W;i++)
   for(int j=0;j<H;j++) d[i][j]=1000000;
  que.push(P(sx,sy));
  d[sx][sy]=0;

  while(!que.empty()){
    P p=que.front(); que.pop();
    for(int i=0;i<4;i++){
	int nx=p.first+dx[i],ny=p.second+dy[i];
	if(0<=nx && nx<W && 0<=ny && ny<H && ma[nx][ny]!='#' &&
	   d[nx][ny] == 1000000){
	  que.push(P(nx,ny));
	  d[nx][ny]=0;
	  // cout<<nx<<"  "<<ny<<endl;
	  count++;
      }
    }
  }
    return count;
  }


int main(){
  while(1){
  cin>>H>>W;
  if(H+W==0) break;
  for(int i=0;i<W;i++){
    for(int j=0;j<H;j++){
      cin>>ma[i][j];
      if(ma[i][j]=='@') {
	sx=i;
	sy=j;
      }
    }
  }
  
  int ans =bfs();
  cout<<ans<<endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define fi first
#define se second
#define FOR(i, a, b) for(int i=(a);i<(b);i++)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for(int i=(a);i>=(b);i--)
#define RREP(i, n) FOR(i, n, 0)
#define MFOR(i, m) for(auto i=(m).begin();i!=(m).end();i++)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((int)(x).size())

typedef long long int ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;

const double eps = 1e-10;
const int MOD = 1000000007;

template<typename T>
void printv(vector<T> const& s) {
  REP(i, s.size()) {
    cout << s[i] << " ";
  }
  cout << endl;
}

int main () {
  cin.tie(0);
  cout << setprecision(10);
  vi ans;

  while(1) {
    int w, h;
    cin >> w >> h;
    if(w == 0 && h == 0) break;
    vector<vector<char>> tiles(h);
    P st;
    REP(i, h) {
      tiles[i].resize(w);
      REP(j, w) {
        cin >> tiles[i][j];
        if(tiles[i][j] == '@') st = make_pair(i, j);
      }
    }
    queue<P> q;
    vector<vector<bool>> sel(h);
    REP(i, h) {
      sel[i].resize(w, false);
    }
    q.push(st);
    sel[st.fi][st.se] = true;
    vi dx = {-1, 1, 0, 0}, dy = {0, 0, -1, 1};
    int res = 0;
    
    while(!q.empty()) {
      P now = q.front(); q.pop();
      res++;
      REP(i, 4) {
        int nextx = now.fi + dx[i], nexty = now.se + dy[i];
          
        if(0 <= nextx && nextx <= h-1 &&
           0 <= nexty && nexty <= w-1 &&
           !sel[nextx][nexty] && tiles[nextx][nexty] == '.') {
          q.push(make_pair(nextx, nexty));
          sel[nextx][nexty] = true;
        }
      }
    }

    ans.pb(res);
  }

  REP(i, ans.size()) {
    cout << ans[i] << endl;
  }
}

C++: #include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <cstdio>
#include <cmath>

using namespace std;

#define all(c) ((c).begin()),((c).end())
#define debug(c) cerr<<"> "<<#c<<" = "<<(c)<<endl;
#define iter(c) __typeof((c).begin())
#define tr(i,c) for(iter(c) i=(c).begin();i!=(c).end();i++)
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define REP(i,a,b) for(int i=(int)(a);i<=(int)(b);i++)

#define mp make_pair
#define fst first
#define snd second
#define pb push_back

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;

int go(int x, int y, char m[21][21], int w, int h) {
	int dx[] = {1, -1, 0, 0};
	int dy[] = {0, 0, 1, -1};
	m[y][x] = '#';
	int res = 1;
	rep (i, 4) {
		int tx = x + dx[i];
		int ty = y + dy[i];
		if(tx < 0 || tx >= w || ty < 0 || ty >= h) continue;
		if(m[ty][tx] == '.') res += go(tx, ty, m, w, h);
	}
	return res;
}

int main(){
	for (int w, h; cin >> w >> h, (w | h) != 0; ) {
		char m[21][21];
		int sx, sy;
		rep (i, h) rep (j, w) {
			cin >> m[i][j];
			if (m[i][j] == '@') sx = j, sy = i;
		}
		cout << go(sx, sy, m, w, h) << endl;
	}
	
	return 0;
}

C++: #include<bits/stdc++.h>
#include<vector>
using namespace std;
//上下右左を差すための配列
int keep_nx[4] = {1 , 0 , -1 , 0};
int keep_ny[4] = {0 , 1 , 0 , -1};
vector< vector<bool> > can_Go (20 , vector<bool> (20));   //フィールドで行ける場所

//深さ優先探索
void dfs(int x, int y, int height, int width, int *cnt){
  can_Go[y][x] = false;   *cnt += 1;
  for(int i = 0 ; i < 4 ; i++){
      int nx = x + keep_nx[i], ny = y + keep_ny[i];
      if(0 <= nx && nx < width && 0 <= ny && ny < height && can_Go[ny][nx] == true)
        dfs(nx, ny, height, width, cnt);
  }
}


int main(void){

while(true){
  int width, height, start_x, start_y;    cin >> width >> height;
  if(width == 0 && height == 0)   break;

  for(int i = 0 ; i < height ; i++){
    for(int j = 0 ; j < width ; j++){
      char input;   cin >> input;
      if(input == '.')  can_Go[i][j] = true;
      if(input == '#')  can_Go[i][j] = false;
      if(input == '@'){
        start_x = j;  start_y = i;
      }
    }
  }

  int ans = 0;
  dfs(start_x, start_y, height, width, &ans);

  cout << ans << endl;
}

  return 0;
}