Go: package main

import (
	"bufio"
	"encoding/json"
	"fmt"
	"os"
	"strconv"
	"strings"
)

var (
	sc = bufio.NewScanner(os.Stdin)
)

type Color int

const (
	ColorBlack Color = iota
	ColorRed
)

var strByColor = map[Color]string{
	ColorBlack: "black",
	ColorRed:   "red",
}

func (c Color) String() string {
	return strByColor[c]
}

type Node struct {
	ID      int
	X, Y    int
	Color   Color
	NextIDs map[int]struct{}
}

func (n *Node) String() string {
	d, _ := json.Marshal(n)
	return string(d)
}

type Map struct {
	Width, Height int
	Nodes         map[int]*Node
}

func (m *Map) GetByID(id int) *Node {
	return m.Nodes[id]
}

func (m *Map) Get(x, y int) *Node {
	return m.Nodes[m.getID(x, y)]
}

func (m *Map) SetColor(x, y int, c Color) {
	m.Nodes[m.getID(x, y)].Color = c
}

func (m *Map) getID(x, y int) int {
	return x + y*m.Width
}

func (m *Map) init() {
	for x := 1; x <= m.Width; x++ {
		for y := 1; y <= m.Height; y++ {
			id := m.getID(x, y)
			m.Nodes[id] = &Node{ID: id, X: x, Y: y}
		}
	}
	for x := 1; x <= m.Width; x++ {
		for y := 1; y <= m.Height; y++ {
			nextIDs := map[int]struct{}{}
			if x > 1 {
				nextIDs[m.getID(x-1, y)] = struct{}{}
			}
			if x < m.Width {
				nextIDs[m.getID(x+1, y)] = struct{}{}
			}
			if y > 1 {
				nextIDs[m.getID(x, y-1)] = struct{}{}
			}
			if y < m.Height {
				nextIDs[m.getID(x, y+1)] = struct{}{}
			}
			m.Get(x, y).NextIDs = nextIDs
		}
	}
}

func NewMap(w, h int) *Map {
	m := &Map{Width: w, Height: h, Nodes: map[int]*Node{}}
	m.init()
	return m
}

type Searcher struct {
	Map        *Map
	queue      []*Node
	touchedIDs map[int]struct{}
	visitedIDs map[int]struct{}
}

func NewSearcher(m *Map) *Searcher {
	return &Searcher{
		Map:        m,
		queue:      []*Node{},
		touchedIDs: map[int]struct{}{},
		visitedIDs: map[int]struct{}{},
	}
}

func (s *Searcher) Search(start *Node) {
	s.enqueue(start)

	for {
		if len(s.queue) == 0 {
			break
		}
		for nextID := range s.dequeue().NextIDs {
			n := s.Map.GetByID(nextID)
			if n.Color == ColorRed || s.hasTouched(n) {
				continue
			}
			s.enqueue(n)
		}
	}
}

func (s *Searcher) VisitedNodeCount() int {
	return len(s.visitedIDs)
}

func (s *Searcher) hasTouched(n *Node) bool {
	_, ok := s.touchedIDs[n.ID]
	return ok
}

func (s *Searcher) enqueue(n *Node) {
	s.queue = append(s.queue, n)
	s.touchedIDs[n.ID] = struct{}{}
}

func (s *Searcher) dequeue() *Node {
	n := s.queue[0]
	s.queue = s.queue[1:]
	s.visitedIDs[n.ID] = struct{}{}
	return n
}

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])

		m := NewMap(w, h)

		// Set colors
		var start *Node
		for y := 1; y <= h; y++ {
			sc.Scan()
			for i, c := range sc.Text() {
				switch c {
				case '.', '@':
					m.SetColor(i+1, y, ColorBlack)
					if c == '@' {
						start = m.Get(i+1, y)
					}
				case '#':
					m.SetColor(i+1, y, ColorRed)
				}
			}
		}

		// Search
		s := NewSearcher(m)
		s.Search(start)
		fmt.Println(s.VisitedNodeCount())
	}
}

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;
	}
	
}