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 <vector>
#include <string>
#include <queue>
using namespace std;
int main() {
  int W, H, x, y, ans, c;
  string s;
  while (cin >> W >> H, W) {
    vector<string> M;
    ans = 0;
    M.push_back(string(W+2, '#'));
    for (int i = 0; i < H; ++i) {
      cin >> s;
      for (int j = 0; j < W; ++j) {
	if (s[j] == '@') {
	  x = i+1;
	  y = j+1;
	  s[j] = '!';
	  break;
	}
      }
      s = '#' + s + '#';
      M.push_back(s);
    }
    M.push_back(string(W+2, '#'));
    queue<vector<int>> que;
    que.push({x, y});
    while (!que.empty()) {
      ++ans;
      x = que.front()[0];
      y = que.front()[1];
      que.pop();
      if (M[x-1][y] == '.') {
	M[x-1][y] = '!';
	que.push({x-1,y});
      }
      if (M[x][y-1] == '.') {
	M[x][y-1] = '!';
	que.push({x,y-1});
      }
      if (M[x+1][y] == '.') {
	M[x+1][y] = '!';
	que.push({x+1,y});
      }
      if (M[x][y+1] == '.') {
	M[x][y+1] = '!';
	que.push({x,y+1});
      }
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
char maze[21][21];
#define r(z,x) for(int z=0;z<x;z++)
int ans,a,b;
void bfs(int x,int y){
	int dx[4]={0,-1,0,1},dy[4]={1,0,-1,0};
	maze[x][y]='#',ans++;
	r(i,4)if(maze[x+dx[i]][y+dy[i]]=='.')bfs(x+dx[i],y+dy[i]);
}
int main(){
	while(cin>>a>>b,a||b){
		ans=0;
		fill(maze[0],maze[20],'#');
		r(i,b)r(j,a)cin>>maze[i][j];
		r(i,b)r(j,a)if(maze[i][j]=='@')bfs(i,j),cout<<ans<<endl;
	}
}

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

using namespace std;
using vec = vector<int>;
using mat = vector<vec>;

void init(vec & parent, vec & size)
{
	for (int i = 0; i < parent.size(); ++i)
	{
		parent[i] = i;
		size[i] = 1;
	}
}


int find(vec & parent, int x)
{
	if (parent[x] == x)
	{
		return x;
	}
	return x = find(parent,parent[x]);
}


void unite(vec & parent, vec & size, int x, int y)
{
	x = find(parent, x);
	y = find(parent, y);

	if (x == y)return;

	parent[y] = x;
	size[x] += size[y];
}


int change(int y,int x,int W)
{
	return y*W + x;
}


int main()
{
	while (true)
	{
		int W, H;
		cin >> W >> H;
		if (W == 0 && H == 0)return 0;

		mat color(H, vec(W));
		vec parent(W*H);
		vec size(W*H);

		init(parent, size);

		int start;

		for (int y = 0; y < H; ++y)
		{
			string c;
			cin >> c;

			for (int x = 0; x < W; ++x)
			{
				
				if (c[x] == '.') { 
					
					color[y][x] = 0; 
					
				}
				else if (c[x] == '#') { color[y][x] = 1; }
				else
				{
					color[y][x] = 0;
					start = change(y, x, W);
				}

				if (x != 0 && color[y][x] == color[y][x - 1])
				{
					unite(parent, size, change(y, x, W), change(y, x - 1, W));
				}
				if (y != 0 && color[y][x] == color[y - 1][x])
				{	
					unite(parent, size, change(y, x, W), change(y - 1, x, W));
				}
				
			}
		}

		cout << size[find(parent, start)] << endl;
	}
}

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

const int dy[4] = {-1,0,1,0}, dx[4] = {0,1,0,-1};
int H,W,ans;
char fld[20][20];

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

int main(){
    int sy,sx;
    while(cin>>W>>H,H||W){
        ans = 0;
        for(int i=0;i<H;i++){
            for(int j=0;j<W;j++){
                cin>>fld[i][j];
                if(fld[i][j]=='@') sy = i, sx = j;
            }
        }
        dfs(sy,sx);
        printf("%d\n",ans);
    }
}

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

int w, h;
char d[20][25];
bool wasOn[20][20];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int solve(int x, int y) {
  if (x < 0 || y < 0 || x >= w || y >= h) return 0;
  if (d[y][x] == '#') return 0;
  if (wasOn[y][x]) return 0;
  wasOn[y][x] = true;
  int ans = 1;
  for (int i = 0; i < 4; i++) ans += solve(x + dx[i], y + dy[i]);
  return ans;
}

int main() {
  while (1) {
    scanf("%d%d", &w, &h);
    if (w == 0) break;
    for (int i = 0; i < h; i++) scanf("%s", d[i]);
    int x, y;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        wasOn[i][j] = false;
        if (d[i][j] == '@') y = i, x = j;
      }
    }
    printf("%d\n", solve(x, y));
  }
}

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

char S[22][22];
int count = 0;

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

void a(int i, int j){
	if(S[i][j] == '.'){
		count++;
		S[i][j] = '#';
		for(int k = 0; k < 4; k++){
			a(i + dy[k],j + dx[k]);
		}
		//a(i - 1, j);
		//a(i + 1, j);
		//a(i, j + 1);
		//a(i, j - 1);
	}
}


int main(){
	while(true){
		memset(S,'#',sizeof(S));
		count = 0;
		int x, y;
		cin >> x >> y;
		if(x == 0 && y == 0){
			break;
		}
		for(int i = 1; i < y + 1; i++){
			for(int j = 1; j < x + 1; j++){
				cin >> S[i][j];
			}
		}

		int startx, starty;
		for(int i = 0; i < y; i++){
			for(int j = 0; j < x; j++){
				if(S[i][j] == '@'){
					startx = j;
					starty = i;
					S[i][j] = '.';
					break;
				}
			}
		}
		a(starty, startx);
		cout << count << endl;
	}
}

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

int n, m;
char map[20][20];
int ans = 0;

int dfs(int x, int y){
	if(0 <= x && x < m && 0 <= y && y < n){
		if(map[x][y] != '.') return 0;
		map[x][y] = '@';
		dfs(x-1, y);
		dfs(x+1, y);
		dfs(x, y-1);
		dfs(x, y+1);
	}
	return 0;
}

int main(){
	while(1){
		for(int i=0; i<20; i++){
			for(int j=0; j<20; j++){
				map[i][j] = '#';
			}
		}
		scanf("%d %d", &n, &m);
		if(n == 0 && m == 0) break;
		for(int i=0; i<m; i++){
			scanf("%s", map[i]);
		}
		int x, y;
		for(int i=0; i<m; i++){
			for(int j=0; j<n; j++){
				if(map[i][j] == '@'){
					x = i;
					y = j;
				}
			}
		}
		dfs(x-1, y);
		dfs(x+1, y);
		dfs(x, y-1);
		dfs(x, y+1);
		for(int i=0; i<m; i++){
			for(int j=0; j<n; j++){
				if(map[i][j] == '@') ans++;
			}
		}
		printf("%d\n", ans);
		ans = 0;
	}
	return 0;
}