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 <iostream>
#include <queue>
using namespace std;
const int dx[]={1,0,-1,0}, dy[]={0,-1,0,1};
int num =1,d[10000][10000];
char A[10000][10000];
bool valid(int x,int y,int W,int H)
{
    return (x>=0&&x<W)&&(y>=0&&y<H);
}
void Init(int W,int H)
{
    int i,j;
    num=1;
    for(i=0;i<W;i++)
        for(j=0;j<H;j++)
            d[i][j]=0;
}
void dfs(int x, int y, int W, int H, char A[10000][10000])
{
    int i;
    for(i=0;i<4;i++)
    {
        if(valid(x+dx[i],y+dy[i],W,H))
        {
            if(A[x+dx[i]][y+dy[i]]=='.' && d[x+dx[i]][y+dy[i]]==0)
            {
                num++;
                A[x+dx[i]][y+dy[i]]='#';
                d[x+dx[i]][y+dy[i]]=1;
                dfs(x+dx[i],y+dy[i],W, H,A);
            }
        }
    }
}
int main()
{
    int W,H,i,j,x,y;
    queue<int> Q;
    cin >> H >> W;
    while(W && H)
    {
        for(i=0;i<W;i++)
        {
            for(j=0;j<H;j++)
                cin >> A[i][j];
        }
        for(i=0;i<W;i++)
            for(j=0;j<H;j++)
            {
                if(A[i][j]=='@')
                {
                    x=i;
                    y=j;
                }
            }
        Init(W,H);
        d[x][y]=1;
        dfs(x, y, W, H, A);
        cout << num << endl;
        cin >> H >> W;
    }
    return 0;
}

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

const int NUM = 7;

void grooping(int y, int x, int w, int h, char** room, unsigned long*** group, unsigned long* next)
{
	if (room[y][x] != '#') {
		for (int i = y - 1; i <= y + 1; i++) {
			for (int j = x - 1; j <= x + 1; j++) {
				if (i < 0 || j < 0 || i >= h || j >= w) {
					continue;
				}
				if ((i == y && j != x) || (i != y && j == x)) {
					for (int k = 0; k < NUM; k++) {
						group[y][x][k] |= group[i][j][k];
					}
				}
			}
		}
		for (int i = 0; i < NUM; i++) {
			if (group[y][x][i] == 0UL && next[i] != 0UL) {
				group[y][x][i] |= next[i];
				next[i] <<= 1;
				break;
			}
		}
	}
}

int main()
{
	int w, h;
	while (true) {
		cin >> w >> h;
		if (w == 0 && h == 0) {
			break;
		}

		unsigned long next[NUM] = { 1UL, 1UL, 1UL, 1UL, 1UL, 1UL, 1UL };
		char** room = new char*[h];
		unsigned long*** group = new unsigned long**[h];
		int x, y;
		for (int i = 0; i < h; i++) {
			room[i] = new char[w];
			group[i] = new unsigned long*[w];
			for (int j = 0; j < w; j++) {
				cin >> room[i][j];
				if (room[i][j] == '@') {
					x = j;
					y = i;
				}
				group[i][j] = new unsigned long[NUM];
				for (int k = 0; k < NUM; k++) {
					group[i][j][k] = 0UL;
				}
			}
		}

		for (int k = 0; k < 2; k++) {
            bool flag=true;
			for (int i = (flag ? 0 : h-1); i >= 0; (flag ? i++ : i--)) {
				for (int j = (flag ? 0 : w-1); j >=0 ; (flag ? j++ : j--)) {
					grooping(i, j, w, h, room, group, next);
                    if (i==h-1 && j==w-1) {
                        flag=!flag;
                    }
				}
			}
			for (int j = (flag ? w-1 : 0); j >= 0; flag ? j-- : j++) {
				for (int i = (flag ? h-1 : 0); i >= 0; flag ? i-- : i++) {
					grooping(i, j, w, h, room, group, next);
                    if (i==h-1 && j==w-1) {
                        flag=!flag;
                    }
				}
			}
		}

		int count = 0;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				for (int k = 0; k < NUM; k++) {
					if ((group[i][j][k] & group[y][x][k]) != 0UL) {
						count++;
						break;
					}
				}
			}
		}

		cout << count << '\n';

		for (int i = 0; i < h; i++) {
			delete[] room[i];
			for (int j = 0; j < w; j++) {
				delete[] group[i][j];
			}
			delete[] group[i];
		}
		delete[] room;
		delete[] group;
	}

	return 0;
}

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

int h,w;
string grid[20];
bool visit[20][20];

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

int rec(int y,int x){      //この場所から到達できるタイルの数を返す
  if(visit[y][x])return 0; //もう来たことがあるならカウントしない
  visit[y][x] = true;

  int res = 1;             //今いるタイルですでに1枚到達
  for(int i=0;i<4;i++){
    int ny = y+dy[i], nx = x+dx[i];

    if(ny<0 || nx<0 || ny>=h || nx>=w)continue;
    if(grid[ny][nx] == '#')continue;
    res += rec(ny,nx);     //次の場所から到達できる枚数を足す
  }
  return res;
}

int main(){
  while(cin >> w >> h,h){
    for(int i=0;i<h;i++){
      cin >> grid[i];
      for(int j=0;j<w;j++)visit[i][j] = false;
    }

    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	if(grid[i][j] == '@')cout << rec(i,j) << endl;
      }
    }
  }
}

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]={};
    
        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+dx[i]>=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;
        
//        rep(i,h){rep(j,w)cout<<(visited[i][j]?'*':'_');cout<<endl;}
    }
    
    return 0;
}