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 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 <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cctype>
#include <climits>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <list>
#include <queue>
#include <deque>
#include <algorithm>
#include <numeric>
#include <utility>
#include <complex>
#include <memory>
#include <functional>
using namespace std;
#define ALL(g) (g).begin(),(g).end()
#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i,n) REP(i,0,n)
#define F(i,j,k) fill(i[0],i[0]+j*j,k)
#define P(p) cout<<(p)<<endl;
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define INF 1<<25
#define pb push_back
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<double> vd;
typedef pair<int,int> pii;
typedef pair<long,long> pll;
typedef long long LL;
typedef long long int LLI;

int memo[20][20];

int mx[] = {0,0,-1,1};
int my[] = {-1,1,0,0};

char table[21][21];

int dfs(int x,int y);
int W=0,H=0;

int main(void)
{
    while(1){
    int i,j;
    int x,y;
    int ans=0;
    cin>> W >> H;
    
    if(W==0 && H==0)break;
    
    REP(i,0,H){
        REP(j,0,W){
            cin >> table[i][j];
            if(table[i][j] == '@'){
                x=i;
                y=j;
            }
        }
    }
    ans = dfs(x,y);
    cout << ans << endl;
    }
    return 0;
}

int dfs(int x,int y){
    int cnt=1;
    for(int i=0;i<4;i++){
        int nx = x+mx[i],ny = y+my[i];
        if(0 <= nx && 0 <= ny && ny < W && nx < H && table[nx][ny] == '.'){
            table[nx][ny] = '#';
            cnt += dfs(nx,ny);
        }
    }
    return cnt;
}

C++: #include<iostream>
#include <functional>
#include<algorithm>
#include <cmath>
#include <string>
#include <map>
using namespace std;

int W,H;
char a[30][30];
bool re[30][30];

void se(int x,int y,int *c){
	if(x<0||W<=x||y<0||H<=y||a[y][x]=='#')
		return;
	if(re[y][x])
		return;
	
	re[y][x]=true;
		*c=*c+1;
	
	se(x+1,y,c);
	se(x,y+1,c);
	se(x-1,y,c);
	se(x,y-1,c);
}

int main(){
	int x,y;
	int x1,y1;
	while(cin>>W>>H){
		if(W!=0&&H!=0){
			int k=0;
		for(int i=0;i<H;i++){
			for(int j=0;j<W;j++){
			cin>>a[i][j];
				if(a[i][j]=='@'){
				x1=j;
				y1=i;}
			}
		}
		se(x1,y1,&k);
		cout<<k<<endl;
		}
		else
			break;
	}

	
}

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

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

char field[20][20];
bool flag[20][20];
int W, H, ans;

void dfs(int x, int y) {
  //field[x][y] = '.';
  //cout << x << ' ' << y << endl;
  //cout << flag[x][y] << endl;
  //cout << ans << endl;
  for(int i = 0 ; i < 4 ; i++) {
    int nx = x + dx[i], ny = y + dy[i];
    if(0 <= nx && nx < H && 0 <= ny && ny < W && field[nx][ny] == '.' &&
       flag[nx][ny] == false) {
      ans++; flag[nx][ny] = true; dfs(nx, ny);
      //cout << "OK" << endl;
    }
    //else dfs(nx, ny);
  }
}

int main(void) {
  int x, y;
  int res;
  
  while(cin >> W >> H, W != 0 && H != 0) {
    memset(flag, false, sizeof(flag));
    ans = 1;
    for(int i = 0 ; i < H ; i++) {
      for(int j = 0 ; j < W ; j++) {
	cin >> field[i][j];
	if(field[i][j] == '@') x = i, y = j;
      }
    }
    /*for(int i = 0 ; i < H ; i++) {
      for(int j = 0 ; j < W ; j++) {
	cout << field[i][j];
      }
      cout << endl;
      }*/
    //printf("@x = %d, @y = %d\n", x, y);
    dfs(x, y);
    cout << ans << endl;
  }
  return 0;
}

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

#define REP(i, n) for(int i = 0;i < (n);i++)
#define FOR(i, a, b) for(int i = (a);i < (b);i++)
#define all(v) v.begin(),v.end()

using int64 = long long;

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

int main(void){
	int H, W;
	while(cin >> W >> H && H && W){
		vector<string> f(H+2, string(W+2, '#'));
		int sy, sx;
		REP(i, H){
			cin >> f[i+1];
			REP(j, W)
				if(f[i+1][j] == '@'){
					sy = i+1;
					sx = j+1;
				}
			f[i+1] = '#'+f[i+1]+'#';
		}
		bool visited[30][30] = {};
		int cnt = 0;
		
		function<void(int, int)> dfs = [&](int y, int x){
			visited[y][x] = 1;
			cnt++;
			REP(i, 4){
				int yy = y+dy[i], xx = x+dx[i];
				if(f[yy][xx] != '#' && !visited[yy][xx]){
					dfs(yy, xx);
				}
			}
		};
		dfs(sy, sx);
		cout << cnt << endl;
	}
}