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<cstdio>
using namespace std;
int w,h;
char m[21][21];

int dfs(int y,int x){
  if(x < 0 || x >= w)return 0;
  if(y < 0 || y >= h)return 0;
  if(m[y][x] == '#') return 0;
  int ret = 1;
  m[y][x] = '#';
  ret += dfs(y+1,x);
  ret += dfs(y-1,x);
  ret += dfs(y,x+1);
  ret += dfs(y,x-1);
  return ret;
}

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

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

using namespace std;

#define rep(a,b) for(int (a)=0;(a)<(b);(a)++)

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<iostream>
using namespace std;
#define MAX (256)

char field[MAX][MAX];
int W, H;
int ans = 0;
int sx, sy;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

void dfs(int x, int y){
	if(0 <= x && x < H && 0 <= y && y < W && field[x][y] == '.'){
		ans++;
		field[x][y] = '#';

		for(int i=0; i < 4; i++){
			int nx = x + dx[i]; int ny = y + dy[i];
			dfs(nx, ny);
		}
	}
}

int main(){
	while(cin >> W >> H, W || H){
		ans = 0;
		for(int i=0; i < H; i++){
			for(int j=0; j < W; j++){
				cin >> field[i][j];
				if(field[i][j] == '@'){
					sx = i;
					sy = j;
					field[i][j] = '.';
				}
			}
		}
		dfs(sx, sy);

		cout << ans << endl;
	}

}

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 = 0;

  que.push( P(x, y) );
  
  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 <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <utility>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <limits>
#include <cmath>
#include <cassert>
#include <queue>

using namespace std;
using ll = long long;

const int INF = 1<<30;
const int MOD = (int)1e9 + 7;
const int MAX_N = (int)1e5 + 5;
const int dy[] = {1, 0, -1, 0};
const int dx[] = {0, 1, 0, -1};
#define debug(x) cout << #x << ": " << x << endl

int W, H;

int solve(int x, int y, const vector<string> &table)
{
    queue<pair<int, int>> que;
    que.push(make_pair(y, x));
    vector<vector<int>> used(H, vector<int>(W, 0));
    used[y][x] = 1;
    int res = 0;
    while(que.size())
    {
        pair<int, int> p = que.front(); que.pop();
        res++;
        for(int i = 0; i < 4; i++)
        {
            int ny = p.first + dy[i];
            int nx = p.second + dx[i];
            if(nx < 0 or ny < 0 or nx >= W or ny >= H) continue;
            if(table[ny][nx] == '#' or used[ny][nx]) continue;
            used[ny][nx] = 1;
            que.push(make_pair(ny, nx));
        }
    }
    return res;
}

signed main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(cin >> W >> H, W or H)
    {
        vector<string> table(H);
        int sx, sy;
        for(int i = 0; i < H; i++)
        {
            cin >> table[i];
            for(int j = 0; j < table[i].size(); j++)
            {
                if(table[i][j] == '@') sx = j, sy = i;
            }
        }
        cout << solve(sx, sy, table) << endl;
    }
    return 0;
}

C++: #include <stdio.h>

void search(int x, int y, int w, int h, int tile[20][20], int& num){
  if(tile[y][x] != 0){
    num++;
    tile[y][x] = 0;
  
    if(x-1 >= 0){
      search(x-1, y, w, h, tile, num);
    }
    if(x+1 < w){
      search(x+1, y, w, h, tile, num);
    }
    if(y-1 >= 0){
      search(x, y-1, w, h, tile, num);
    }
    if(y+1 < h){
      search(x, y+1, w, h, tile, num);
    }
  }
}

int main(void){
  int w, h, x, y;
  char c[20][21];
  int tile[20][20];
  while(1){
    scanf("%d%d", &w, &h);
    if(w==0 && h==0){
      break;
    }
    int num = 0;
    for(int i=0; i<h; i++){
      scanf("%s", c[i]);
    }
    for(int i=0; i<h; i++){
      for(int j=0; j<w; j++){
	if(c[i][j] == '.'){
	  tile[i][j] = 1;
	}else if(c[i][j] == '@'){
	  tile[i][j] = 1;
	  x = j;
	  y = i;
	}else if(c[i][j] == '#'){
	  tile[i][j] = 0;
	}
      }
    }
    search(x, y, w, h, tile, num);
    printf("%d\n", num);
  }
  return 0;
}