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 <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG

const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};

vector<vector<int>> M;
vector<vector<int>> checked;
int ans;
int w, h;

void dfs(int row, int col) {
  ans++;
  checked[row][col] = 1;
  for (int i = 0; i < 4; i++) {
    int x = col + dx[i];
    int y = row + dy[i];
    if (0 <= x && x < w && 0 <= y && y < h && M[y][x] == 1 &&
        checked[y][x] == 0) {
      dfs(y, x);
    }
  }
}
int main() {
  while (cin >> w >> h) {
    if (!w && !h)
      break;

    ans = 0;
    M = vector<vector<int>>(h, vector<int>(w));
    checked = vector<vector<int>>(h, vector<int>(w, 0));

    int s_row, s_col;
    for (int i = 0; i < h; i++) {
      string line;
      cin >> line;
      for (int j = 0; j < w; j++) {
        char c = line[j];
        if (c == '.') {
          M[i][j] = 1;
        } else if (c == '#') {
          M[i][j] = 0;
        } else {
          M[i][j] = 1;
          s_row = i;
          s_col = j;
        }
      }
    }
    dfs(s_row, s_col);
    cout<<ans<<endl;
  }
}

C++: #include "bits/stdc++.h"

using namespace std;

#define rep(i,n) for(int i=0;i<(n);i++)

int main(){
    int w,h;
    while(cin>>w>>h){
        if(w==0 && h==0)break;
        int sx,sy;
        int sum=0;
        int dy[]={-1,0,1,0};
        int dx[]={0,-1,0,1};
    
    
        string data[30]={};
    
        for(int i=0;i<h;i++){
            cin>>data[i];
        }
    
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(data[i][j]=='@'){
                    sx=j;sy=i;
                    break;
                }
            }
        }
    
        bool visited[30][30]={};
        rep(i,30)rep(j,30)visited[i][j]=false;
        
    
        queue<pair<int,int> > que;
    
        que.push( pair<int,int>(sy,sx) );
        visited[sy][sx]=true;
    
        while(que.size()){
            int qff=que.front().first,qfs=que.front().second;
            sum++;
            rep(i,4){
                if( qff+dy[i]<h && qff+dy[i]>=0 && qfs+dx[i]<w && qfs>=0 && visited[ qff+dy[i] ][ qfs+dx[i] ]==false && data[ qff+dy[i] ][ qfs+dx[i] ]!='#' ){
                    que.push( pair<int,int>(qff+dy[i] , qfs+dx[i]) );
                    visited[ qff+dy[i] ][ qfs+dx[i] ]=true;
                }
            }
            
            que.pop();
        }
        cout<<sum<<endl;
    }
    
    return 0;
}

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

typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pair<int, int> > vii;
#define rrep(i, m, n) for(int (i)=(m); (i)<(n);  (i)++)
#define erep(i, m, n) for(int (i)=(m); (i)<=(n); (i)++)
#define  rep(i, n)    for(int (i)=0; (i)<(n);  (i)++)
#define rrev(i, m, n) for(int (i)=(n)-1; (i)>=(m); (i)--)
#define erev(i, m, n) for(int (i)=(n); (i)>=(m); (i)--)
#define  rev(i, n)    for(int (i)=(n)-1; (i)>=0; (i)--)
#define vrep(i, c)    for(__typeof((c).begin())i=(c).begin(); i!=(c).end(); i++)
#define  ALL(v)       (v).begin(), (v).end()
#define mp            make_pair
#define pb            push_back
template<class T, class S> inline bool minup(T& m, S x){ return m>(T)x ? (m=(T)x, true) : false; }
template<class T, class S> inline bool maxup(T& m, S x){ return m<(T)x ? (m=(T)x, true) : false; }

const int    INF = 1000000000;
const ll     MOD = 1000000007LL;
const double EPS = 1E-12;

const int MAX_W = 50;
const int MAX_H = 20;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
int H, W;
bool ok[MAX_H][MAX_W];
char maze[MAX_H][MAX_W+1];
int sx, sy;

inline bool check(int hx, int hy){ return 0 <= hx && hx < H && 0 <= hy && hy < W; }

int main()
{
  while((cin >> W >> H) && W){
    rep(i, H) cin >> maze[i];

    rep(i, H) rep(j, W) ok[i][j] = false;
    rep(i, H) rep(j, W) if(maze[i][j] == '@') sx = i,  sy = j;

    stack<pii> st;
    st.push(mp(sx, sy));
    maze[sx][sy] = '.';
    ok[sx][sy] = true;
    while(!st.empty()){
      pii here = st.top();  st.pop();
      int hx = here.first;
      int hy = here.second;
      rep(k, 4){
        int nx = hx + dx[k];
        int ny = hy + dy[k];
        if(!check(nx, ny) || maze[nx][ny] == '#') continue;
        if(ok[nx][ny]) continue;
        ok[nx][ny] = true;
        st.push(mp(nx, ny));
      }
    }

    int res = 0;
    rep(i, H) rep(j, W) if(ok[i][j]) res += 1;
    cout << res << endl;
  }

  return 0;
}

C++: #include<stdio.h>

int dfs(int x,int y);
int w,h,i,j;
char maps[21][21];
int dx[4]={0,-1,0,1};
int dy[4]={-1,0,1,0};
int sum=0;

int main(){
  int sx,sy;
  while(1){
    sum=0;
    scanf("%d %d",&h,&w);
    if(h==0 && w==0) break;
    for(i=0;i<h;i++){
      for(j=0;j<w;j++){
	scanf("%c",&maps[i][j]);
	if(maps[i][j]=='@'){
	  sy=i;
	  sx=j;
	}
      }
    }
    dfs(sx,sy);
    printf("%d\n",sum);
  }
  return(0);
}

int dfs(int x,int y){
  int nx,ny;
  for(i=0;i<4;i++){
    nx=x+dx[i];
    ny=y+dy[i];
    maps[y][x]='#';
    if(0<=nx && nx<w && ny<h && maps[ny][nx]!='#'){
      dfs(nx,ny);
      sum++;
    }
  }
  return 0;
}