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 <cstring>
#include <stack>
using namespace std;

class P {
public:
	int x, y;
	P() {}
	P(int _x, int _y) : x(_x), y(_y) {}
};

int w, h;
char map[22][22];

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

int main() {
	while (1) {
		cin >> w >> h;
		if (w == 0 && h == 0) break;
		P start;
		for (int i = 0; i < h; i++) {
			cin >> map[i];
			char* at = strchr(map[i], '@');//文字列から文字を検索する
			if (at != NULL) {//@が存在したらスタート地点なので記録
				start.x = at - map[i];
				start.y = i;
			}
		}
		//深さ優先探索
		stack<P> deq;
		int count = 0;
		deq.push(start);//初期位置をpush
		while (!deq.empty()) {
			P current = deq.top();//pop
			deq.pop();
			count++;
			for (int i = 0; i < 4; i++) {
				int nextX = dx[i] + current.x;
				int nextY = dy[i] + current.y;
				if (0 <= nextX && nextX < w && 0 <= nextY && nextY < h
						&& map[nextY][nextX] == '.') {
					deq.push(P(nextX, nextY));//次のノードをpush
					map[nextY][nextX] = '#';//戻らないように埋める
				}
			}

		}
		cout << count << endl;
	}
	return 0;
}

/*	ポイント
		領域の塗りつぶし問題
		再帰による実装の方が一般的かも
		埋めるタイミングが間違ってて詰まってた
		mapはy,xの順の添字で使うと入力が楽
			でもその分参照は注意
		探索の基礎
			push()
			while (!empty) {
				pop()
				if (ゴール判定) {
					break;
				}
				if (次のノードの条件) {
					push()
				}
			}
			※stackで深さ,queueで幅,priority_queueでダイクストラ(コストがpriority)
*/

C++: #pragma GCC optimize("O3")

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int N=25;
int a[N*N];
char c[N][N];
int root(int x){
	int i=x;
	while(1){
		if(a[x]==x) break;
		x=a[x];
	}
	int j=x;
	while(1){
		if(a[i]==i) break;
		x=a[i],a[i]=j,i=x;
	}
	return j;
}
int main(){
	int m,n;
	while(scanf("%d%d",&n,&m)){
		if(m==0&&n==0) break;
		getchar();
		int i,j;
		for(i=1;i<=m*n;i++) a[i]=i;
		for(i=0;i<m;i++) gets(c[i]);
		for(i=0;i<m;i++){
			for(j=0;j<n;j++){
				if(c[i][j]=='#') continue;
				int r=root(i*n+j+1),rr;
				if(i&&c[i-1][j]!='#'){
					rr=root((i-1)*n+j+1);
					if(r!=rr) a[rr]=r;
				}
				if(i+1<m&&c[i+1][j]!='#'){
					rr=root((i+1)*n+j+1);
					if(r!=rr) a[rr]=r;
				}
				if(j&&c[i][j-1]!='#'){
					rr=root(i*n+j-1+1);
					if(r!=rr) a[rr]=r; 
				}
				if(j+1<n&&c[i][j+1]!='#'){
					rr=root(i*n+j+1+1);
					if(r!=rr) a[rr]=r;
				}
			}
		}
		int s,ans=0;
		for(i=0;i<m;i++){
			for(j=0;j<n;j++){
				if(c[i][j]=='@'){
					s=root(i*n+j+1);
					for(int k=1;k<=m*n;k++){
						if(s==root(k)) ans++;
					}
					printf("%d\n",ans);
					break;
				}
			}
			if(j<n) break;
		}
	}
	
	return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <algorithm>
#include <iostream>
#include <string>

#define REP(i,n) for(int i=0;i<n;++i)
#define REPR(i,n) for(int i=n;i>=0;--i)
#define REPI(itr,v) for(auto itr=v.begin();itr!=v.end();++itr)
#define REPIR(itr,v) for(auto itr=v.rbegin();itr!=v.rend();++itr)
#define FOR(i,a,b) for(int i=a;i<b;++i)
#define SORT(v,n) sort(v, v+n)
#define SORTV(v) sort(v.begin(), v.end())
#define ALL(v) v.begin(),v.end()
#define llong long long
#define INF 999999999
#define SUR 1000000007
#define pb push_back
#define pf push_front
#define MP make_pair

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

using namespace std;

typedef pair<int,int> pii;

int h, w;

int solve(bool used[20][20], char s[21][21], int x, int y){
  int res = 0;
  if(s[y][x] == '#'){
    return 0;
  }else{
    res++;
  }

  REP(i,4){
    int next_x = x + dx[i], next_y = y + dy[i];
    if(next_x >= 0 && next_x < w && next_y >= 0 && next_y < h){
      if(!used[next_y][next_x]){
        used[next_y][next_x] = true;
        res += solve(used, s, next_x, next_y);
      }
    } 
  }
  return res;
}

int main(){

  while(true){
    scanf("%d %d", &w, &h);
    if(h == 0 && w == 0){
      break;
    }

    char s[21][21];
    REP(i,h){
      scanf("%s", s[i]);
    }

    bool found = false;
    REP(i,h){
      REP(j,w){
        if(s[i][j] == '@'){
          bool used[20][20] = {};
          used[i][j] = true;
          int ans = solve(used, s, j, i);
          printf("%d\n", ans);
          break;
          found = true;
        }
      }
      if(found) break;
    }
  }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
​
int W, H;
char visit[21][21];
int dfs(int h, int w){
  int sum=1;
  if(0>w || w==W || 0>h || h==H) return 0;
  if(visit[h][w]=='#')  return 0;
  if(visit[h][w]=='!')   return 0;
    visit[h][w] = '!';
    sum += dfs(h, w+1);
    sum += dfs(h, w-1);
    sum += dfs(h+1, w);
    sum += dfs(h-1, w);
  
  return sum;
}
​
int main(void){
  int w1, h1;
  string str;
  while(cin >> W >> H){
    if(W == 0 && H == 0)  break;
    for(int i=0; i<H; i++){
      cin >> str;
      for(int j=0; j<W; j++){
        visit[i][j] = str[j];
        if(visit[i][j] == '@'){
          visit[i][j] = '.';
          w1 = j; h1 = i;  
        }
      }
    }
    cout << dfs(h1, w1) <<endl;
  }
  
  return 0;
}