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 <algorithm>

using namespace std;

struct Point{
	int x,y;
};

const Point dir[4] = {{1,0},{0,1},{-1,0},{0,-1}};

int w,h;
char map[20][20];
bool visited[20][20];//訪問済みならtrue
int cnt;//黒いタイルを数えるカウンタ

bool inField(Point p){
	return 0<=p.x && p.x < w && 0<=p.y && p.y < h;
}

//幅優先探索
void dfs(Point pos){
	cnt++;
	visited[pos.y][pos.x] = true;
	for(int i=0;i<4;i++){
		Point next = {pos.x+dir[i].x, pos.y+dir[i].y};
		if(inField(next)==true){
			if(visited[next.y][next.x] == false && map[next.y][next.x] == '.'){
				dfs(next);
			}
		}
	}
}

int main(){
	while(true){
		cin >> w >> h;
		if(w==0){
			break;
		}
		Point init;//初期位置
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				cin >> map[i][j];
				visited[i][j] = false;
				if(map[i][j] == '@'){
					//初期代入以外はキャストする必要がある。
					init = (Point){j,i};
					map[i][j] = '.';
				}
			}
		}
		cnt = 0;
		dfs(init);
		cout << cnt << endl;
	}
	return 0;
}

C++: #if 0
08-162015?????????????????°???????????????????????????

#endif

#include <iostream>
#include <queue>
#include <string.h>
#include <utility>
using namespace std;

int w,h;
const int dx[]={1,0,-1,0}, dy[]={0,-1,0,1}; //??????????§??????¨
char P[30][30]; //??§?¨?????????????????????¨???

bool valid(int x,int y){ //??????????????????
	return (x>=0)&&(x<w)&&(y>=0)&&(y<h)&&(P[x][y]!='#');
}

void bfs(int x,int y){ //???????????¢?´¢
	int tot=1; //?????£????????????(?????????????????????)
	queue<pair<int,int> > q;
	pair<int,int> p;
	p = make_pair(x,y);
	while(!q.empty()){
		p=q.front();
		q.pop();
		for(int i=0;i<4;++i){
			if(valid(p.first+dx[i],p.second+dy[i]))
				tot+=1;
				p.first+=dx[i]; p.second+=dy[i];
				q.push(p);
		}
	}
}


int main(){
	int x1,y1;
	while((cin>>w>>h)&&(w!=0&&h!=0)){
		for(int i=0;i<h;++i){
			for(int j=0;j<w;++j){
				cin>>P[j][i];
				if(P[j][i]=='@')
					x1=j; y1=i; 
			}
		}
		bfs(x1,y1); //??????????????°????????£??\????????¢?´¢????§?
	}
}

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

int masu[20][20];
int W,H;

int dfs(int x,int y){
     if(x>=W||y>=H||x<0||y<0)return 0;
     if(masu[x][y]==1)return 0;
     masu[x][y]=1;
     return dfs(x+1,y)+dfs(x,y+1)+dfs(x-1,y)+dfs(x,y-1)+1;
}

int main(){
     while(1){
	  cin>>W>>H;
	  if(W==0)break;
	  int pos[2];
	  for(int j=0;j<H;j++){
	       string temp;
	       cin>>temp;
	       for(int i=0;i<W;i++){
		    switch(temp[i]){
		    case '.':
			 masu[i][j]=0;
			 break;
		    case '@':
			 masu[i][j]=0;
			 pos[0]=i;pos[1]=j;
			 break;
		    case '#':
			 masu[i][j]=1;
			 break;
		    }
	       }
	  }
	  int ans=dfs(pos[0],pos[1]);
	  cout<<ans<<endl;
     }
}

C++: #include<iostream>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>

using namespace std;

#define INF 999999999

char map[20][21];
bool ch[20][20];
int ans;

void dfs(int w, int h, char map[20][21], bool c[20][20], int x, int y){
	//cout << x << " " << y << " " << ans << endl;
	ans++;
	c[x][y] = 1;
	if(x>0 && map[x-1][y]=='.' && c[x-1][y]==0){
		dfs(w,h,map,c,x-1,y);
	}
	if(x<h-1 && map[x+1][y]=='.' && c[x+1][y]==0){
		dfs(w,h,map,c,x+1,y);
	}
	if(y>0 && map[x][y-1]=='.' && c[x][y-1]==0){
		dfs(w,h,map,c,x,y-1);
	}
	if(y<w-1 && map[x][y+1]=='.' && c[x][y+1]==0){
		dfs(w,h,map,c,x,y+1);
	}
}

int main(){
	int i, j, w, h, x, y;
	while(1){
		cin >> w >> h;
		if(!w) break;
		for(i=0;i<h;i++){
			cin >> map[i];
			for(j=0;j<w;j++){
				ch[i][j] = 0;
				if(map[i][j] == '@'){
					x = i;
					y = j;
				}
			}
		}
		ans = 0;
		dfs(w,h,map,ch,x,y);
		
		cout << ans << endl;
	}
	return 0;
}

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

using namespace std;
int W, H;
string s[20];
int num;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {-1, 0, 1, 0};

void dfs(int x, int y){
  num++;
  s[y][x] = '#';
  for(int i=0; i<4; i++){
    if(0<=x+dx[i] && x+dx[i]<W && 0<=y+dy[i] && y+dy[i]<H && s[y+dy[i]][x+dx[i]]=='.'){
      dfs(x+dx[i], y+dy[i]);
    }
  }
}

int main(){
  while(cin >> W >> H, W || H){
    for(int i=0; i<H; i++){
      cin >> s[i];
    }
    int x, y;
    for(int i=0; i<H; i++){
      for(int j=0; j<W; j++){
	if(s[i][j] == '@'){
	  x = j;
	  y = i;
	}
      }
    }
    num = 0;
    dfs(x, y);
    cout << num << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
 
using namespace std;
typedef long long ll;
 
#define rep(i, n) for(int i=0; i<(n); ++i)

char field[21][21];
char visit[21][21];
int dx[]= {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int W, H;
void dfs(int y, int x) {
    visit[y][x] = 1;
    rep(i, 4) {
        int ny = y+dy[i];
        int nx = x+dx[i];
        if(field[ny][nx] == '.' && visit[ny][nx] == 0 &&
               nx >= 0 && nx < W && ny >= 0 && ny < H) {
            dfs(ny, nx);
        }
    }
}
int main(void){
    while(cin >> W >> H && W) {
        int y, x;
        rep(i, H) rep(j, W){
            cin >> field[i][j];
            if(field[i][j] == '@') y = i, x = j;
        }
        memset(visit, 0, sizeof(visit));
        dfs(y, x);
        int ans = 0;
        rep(i, H) rep(j, W){
            ans += visit[i][j];
        }
        cout << ans << endl;
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
int w, h;
char ma[30][30];
char check[30][30];
int mx[4] = {-1, 0, 1, 0};
int my[4] = {0, -1, 0, 1};
int ans;

void Rec(int x, int y);

int main(void){
	while(1){
		ans = 0;

		cin >> w >> h;
		if(w == 0 && h == 0) break;
		for(int i=0; i<h; i++){
			scanf("%s", ma[i]);
		}

		int sx, sy;
		for(int i=0; i<h; i++){
			for(int j=0; j<w; j++){
				if(ma[i][j] == '@'){
					sx = j;
					sy = i;
				}
				check[i][j] = 0;
			}
		}
		
		check[sy][sx] = 1;
		ans++;
		Rec(sx, sy);

		cout << ans << endl;
	}

	return 0;
}

void Rec(int x, int y){
	for(int i=0; i<4; i++){
		int nx = x + mx[i];
		int ny = y + my[i];

		if(nx >= 0 && nx < w && ny >= 0 && ny < h && ma[ny][nx] == '.' && check[ny][nx] == 0){
			check[ny][nx] = 1;
			ans++;
			Rec(nx, ny);
		}
	}
}