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<map>
#include<vector>
#include<list>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<numeric>
#include<climits>
#include<ctime>
#include<cstring>
#define REP(i,p,n) for(int i=p;i<(int)(n);i++)
#define rep(i,n) REP(i,0,n)
#define rep_split(tok,a_str,re) for(char *tok = strtok((char *)a_str.c_str(),re); tok != NULL; tok = strtok(NULL,re))
#define ALL(c) (c).begin(), (c).end()
#define dump(a) cerr << #a << "=" << (a) << endl

using namespace std;


int W,H;
int _black_cnt;
char sima[21][21];
bool is_visit[21][21];
int dw[4] = {1,0,-1,0};
int dh[4] = {0,1,0,-1};

void saiki(int w, int h) {
	int aw,ah;
	rep(i,4) {
		aw = w+dw[i];
		ah = h+dh[i];
		if ((0 <= aw && aw <= W) || (0 <= ah && ah <= H)) { 
			if (sima[aw][ah] == '.' && !is_visit[aw][ah]) {
				is_visit[aw][ah] = true;
				_black_cnt++;
				saiki(aw,ah);
			}
			else {
				is_visit[aw][ah] = true;
			}
		}
	}

	return;
}

int main() {
	string a_str;
	while(cin >> W >> H) {
		if (W==0 && H==0) { break; }

		_black_cnt=0;
		//初期化
		rep(h,21) {
			rep(w,21) {
				sima[w][h] = 0;
				is_visit[w][h] = 0;
			}
		}
		
		//値挿入
		int fw=0,fh=0;
		rep(h,H) {
			cin >> a_str;
			rep(w,W) {
				if (a_str[w]=='@') { fw=w; fh=h; }
				sima[w][h] = a_str[w];
			}
		}

		is_visit[fw][fh] = true;
		_black_cnt=1;
		saiki(fw,fh);
		cout << _black_cnt << endl;
	}

	return 0;
}

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

int n, m;
char field[100][100];

int dfs(int x, int y){
	field[x][y] = '.';

	for(int dx = -1; dx <= 1; dx++){
		for(int dy = -1; dy <= 1; dy++){
			int nx = x + dx, ny = y + dy;

			if(0 <= nx && nx < n && 0 <= ny && ny < m && field[nx][ny] == 'W') dfs(nx, ny);
		}
	}
	return 0;
}

int main(){
	scanf("%d %d", &n, &m);
	for(int i=0; i<n; i++){
		scanf("%s", field[i]);
	}
	int ans = 0;
	for(int i=0; i<n; i++){
		for(int j=0; j<m; j++){
			if(field[i][j] == 'W'){
				dfs(i, j);
				ans++;
			}
		}
	}
	printf("%d\n", ans);
	return 0;
}

C++: //#define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
//#include <cmath>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <list>
#include <queue>
#include <set>
using namespace std;

bool check[30][30] = { false };
int movex[] = { 0, 1, 0, -1 };
int movey[] = { 1, 0, -1, 0 };

int move(int num, int x, int y){
	int retnum = num;
	if (check[x][y]){
		retnum++;
		check[x][y] = false;
		for (int i = 0; i < 4; i++){
			retnum = move(retnum, x + movex[i], y + movey[i]);
		}
	}

	return retnum;
}

int main(void){

	int W ,H;
	// char ;

	while (scanf("%d %d", &W, &H) != EOF){
		if (W == 0 && H == 0) break;
		int ax, ay;

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

		for (int i = 1; i<=H; i++){
				char buff[20];
				scanf("%s", buff);
				for (int j = 1; j <= W; j++){
				if (buff[j- 1] == '.') check[i][j] = true;
				else if (buff[j - 1] == '@'){
					check[i][j] = true;
					ax = i;
					ay = j;
				}
			}
		}
		
		cout << move(0, ax, ay) << endl;

	}

	return 0;
}

/*
for(i=0;i<N;i++){
cin >> X[i];
}


for(i=0;i<N;i++){
for(j=0;j<N;j++){
cin >> X[i][j];
}
}
*/

C++: #include<iostream>
#include<algorithm>
#include<stack>
#include<queue>
#include<string>
#include<string.h>
#include<functional>
using namespace std;

#define INF 1<<21
#define DIV 1000000007

typedef pair<int, int>P;


int w, h;
int cnt;
char f[21][21];
int dx[] = { 1,-1,0,0 }, dy[] = { 0,0,1,-1 };

void rec(int x, int y) {
  f[y][x] = '#';
  cnt++;

  for (int i = 0; i < 4; i++) {
	int nx = x + dx[i];
	int ny = y + dy[i];

	if (nx <= 0 || w < nx || ny <= 0 || h < ny||f[ny][nx]!='.')continue;

	rec(nx, ny);
  }
}

int main(){
  while (cin >> w >> h,(w || h)) {
	int sx, sy;
	cnt = 0;
	for (int y = 1; y <= h; y++) {
	  for (int x = 1; x <= w; x++) {
		cin >> f[y][x];
		if (f[y][x] == '@') {
		  sx = x;
		  sy = y;
		}
	  }
	}
	rec(sx, sy);
	cout << cnt << endl;
  }
}

C++: #include <iostream>
#include <deque>
#include <algorithm>

using namespace std;

int w,h;
int ret;
char f[32][32];
bool visited[32][32];
int dx[4] = {0,-1,0,1};
int dy[4] = {1,0,-1,0};

int main(){
	while(cin >> w >> h){
		if(!(h|w)) break;
		int x, y;
		deque<pair<int,int> > q;
		fill((char *)f, (char *)f+32*32, '#');
		fill((bool *)visited, (bool *)visited+32*32, false);
		ret = 0;
		for(int i=1;i<=h;i++){
			for(int j=1;j<=w;j++){
				cin >> f[i][j];
				if(f[i][j] == '@'){
						q.push_back(make_pair(i,j));
				}
			}
		}
		while(!q.empty()){
			x = q.front().first;
			y = q.front().second;
			q.pop_front();
			visited[x][y] = true;
			ret++;
			for(int i=0;i<4;i++){
				if(f[x+dx[i]][y+dy[i]] == '.' &&
				   visited[x+dx[i]][y+dy[i]] == false &&
				find(q.begin(),q.end(),make_pair(x+dx[i], y+dy[i])) == q.end()){
					q.push_back(make_pair(x+dx[i], y+dy[i]));
				}
			}
		}
		
		cout << ret << endl;
	}
	
}

C++: #include "bits/stdc++.h"
using namespace std;

int COUNT=0;

void dfs(vector<string> map,int state[20][20],int x,int y,int W,int H){
  int dx[4]={-1,0,1,0};
  int dy[4]={0,-1,0,1};
  if(x<0 || x>H-1 || y<0 || y>W-1)return; //????????????
  if(state[x][y]!=0 || map[x][y]=='#')return; //?????§????????£??????????????????????????????
  ++COUNT;
  state[x][y]=1;
  for(int i=0;i<4;++i)dfs(map,state,x+dx[i],y+dy[i],W,H); //??????????????¨??¢?´¢
}

int main(){
  int W,H,x,y;
  vector<string> map;
  string a;
  while(true){
    COUNT=0;
    cin >> W >>H;
  if(!W && !H)break;
  int state[20][20]={0}; //?????§????????£???????????????
  for(int i=0;i<H;++i){ //???????????\???
    cin >> a;
    map.push_back(a);
  }
  for(int i=0;i<H;++i){//????????????????????¢?´¢
    if(0<=map[i].find('@') && map[i].find('@')<W){
      y=map[i].find('@');
      x=i;
      break;
    }
  }
    dfs(map,state,x,y,W,H);
    cout << COUNT << endl;
    map.clear();
  }
}