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<utility>
#include<queue>
#include<map>
using namespace std;
typedef pair<int, int> P;

const int MAX_H = 22;
const int MAX_W = 22;

char grid[MAX_H][MAX_W];
bool is_checked[MAX_H][MAX_W];

void solve(int W, int H) {
    map<P, bool> is_exist;
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            cin >> grid[i][j];
            is_checked[i][j] = false;
            is_exist[P(i, j)] = true;
        }
    }
    queue<P> Q;
    int dh[4] = {-1, 0, 1, 0}, dw[4] = {0, -1, 0, 1};
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            if (grid[i][j] == '@') {
                Q.push(P(i, j));
                is_checked[i][j] = true;
                while (!Q.empty()) {
                    P now_pos = Q.front();
                    Q.pop();
                    for (int k = 0; k < 4; k++) {
                        P new_pos = P(now_pos.first + dh[k], now_pos.second + dw[k]);
                        if (!is_exist[new_pos] || is_checked[new_pos.first][new_pos.second]) continue;
                        if (grid[new_pos.first][new_pos.second] == '#') continue;
                        is_checked[new_pos.first][new_pos.second] = true;
                        Q.push(new_pos);
                    }
                }
            }
        }
    }
    int ans = 0;
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            if (is_checked[i][j]) ans++;
        }
    }
    cout << ans << endl;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int W, H;
    while (true) {
        cin >> W >> H;
        if (W == 0 && H == 0) break;
        solve(W, H);
    }
    return 0;
}

C++: #include <iostream>
#include <string>
using namespace	std;
int result,w,h;
int d1[4]={-1,0,1,0},d2[4]={0,1,0,-1};
string a[21];

void dfs(int x,int y){

  int i;

  result++;
  a[y][x]='#';
  for(i=0;i<4;i++) if(0<=y+d2[i] && y+d2[i]<h && 0<=x+d1[i] && x+d1[i]<w) if(a[y+d2[i]][x+d1[i]] == '.') dfs(x+d1[i],y+d2[i]);

}

int main(){

  int x1,y1,i,j;

  while(cin>>w>>h,w){

    result=0;
    for(i=0;i<h;i++) cin>>a[i];

    for(i=0;i<h;i++) for(j=0;j<w;j++) if(a[i][j] == '@'){
          x1=j;
          y1=i;
	}

    dfs(x1,y1);

    cout<<result<<endl;


  }

  return 0;

}

C++: #include <iostream>
#include <vector>
#include <algorithm> 
#include <queue>
#include <functional>
using namespace std;

int w, h;
int vx[4] = { 1, 0, -1, 0 };
int vy[4] = { 0, 1, 0, -1 };
bool used[20][20];
char c[20][20];

int dfs(int x, int y){
    int ret = 1;
    for (int i = 0; i < 4; i++){
        int nextx = x + vx[i];
        int nexty = y + vy[i];

        if (0 < nextx && nextx < w + 1 && 0 < nexty && nexty < h + 1 && c[nextx][nexty] == '.' && !used[nextx][nexty]){
            used[nextx][nexty] = true;
            ret += dfs(nextx, nexty);
        }
    }

    return ret;
    
}

int main(void){

    while (true){

        cin >> w >> h;

        if (w == 0 && h == 0)break;

        for (int i = 0; i <= 20; i++){
            for (int j = 0; j <= 20; j++){
                used[i][j] = false;
            }
        }

        int sx, sy;

        for (int i = 1; i <= h; i++){
            for (int j = 1; j <= w; j++){
                cin >> c[j][i];
                if (c[j][i] == '@'){
                    sx = j; sy = i;
                }
            }
        }

        cout << dfs(sx, sy) << endl;
    }
 

}

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

char tile[30][30];
int W, H;
int ix, iy;
int cnt;
int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,1,0,-1 };

void dfs(int x, int y){
	tile[y][x] = '#';
	cnt++;
	for (int i = 0; i < 4; i++){
		int X = x + dx[i];
		int Y = y + dy[i];
		if (0<=X && X<W && 0<=Y && Y<H && tile[Y][X]!='#'){
			dfs(X, Y);//関数の再起
		}
	}
}





int main(){

	while (cin >> W >> H, W + H != 0){
		for (int y = 0; y < H; y++){
			for (int x = 0; x < W; x++){
				cin >> tile[y][x];
				if (tile[y][x] == '@'){
					ix = x;
					iy = y;
				}
			}
		}
		dfs(ix, iy);

		cout << cnt << endl;
		cnt = 0;
	}

	return 0;
}

C++: #include <cstdio>

using namespace std;

#define rep(i,x) for(int i=0;i<(x);++i)

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

char mp[20][22];
int H, W;

void dfs(int y, int x)
{
	if (y < 0 || y >= H || x < 0 || x >= W || mp[y][x] == '#' || mp[y][x] == ' ') return;

	mp[y][x] = ' ';

	rep(i, 4) dfs(y + dy[i], x + dx[i]);
}

int main()
{
	while (scanf("%d %d", &W, &H), H || W) {
		int sy, sx;

		rep(i, H) rep(j, W) {
			scanf("\n%c", &mp[i][j]);
			if (mp[i][j] == '@') sy = i, sx = j, mp[i][j] = '.';
		}

		dfs(sy, sx);

		int cnt = 0;

		rep(i, H) rep(j, W) if (mp[i][j] == ' ') cnt++;

		printf("%d\n", cnt);
	}
}

C++: #if 0

#endif

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

int W, H;
int x[4] = {1,0,-1,0};
int y[4] = {0,-1,0,1};

bool in_map(int h, int w) {
  return (1 <= h && h <= H && 1 <= w && w <= W);
}

char M[25][25];
int cnt;
void dfs(int h, int w) {
  cnt += 1;
  M[h][w] = '#';
  for (int i = 0; i < 4; i++) {
    int nh = h+y[i]; int nw = w+x[i];
    if (in_map(nh, nw) && M[nh][nw] != '#') {
      dfs(nh, nw);
    }
  }
}

int main() {
  int sh,sw;
  while(cin >> W >> H && W > 0) {
    cnt = 0;
    for (int h = 1; h <= H; h++) {
      for (int w = 1; w <= W; w++) {
        cin >> M[h][w];
        if (M[h][w] == '@') {
          sh = h; sw = w;
        }
      }
    }
    dfs(sh, sw);
    cout << cnt << endl;
  }
}