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 <stdio.h>
#include <queue>
#include <string.h>
using namespace std;	
int W,H;
char c[101][101];
bool visited[100][100];
 
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
 	
void compute();//????????????????????£?¨? 
	
int main(){
	
	while(scanf("%d %d",&W,&H),W |H){
	    int i;
		
		for(i=0;i<H;i++){
			scanf(" %s",&c[i][0]);
		}
		compute();
	}
	return 0;
}

void compute(){
	
	memset(visited,false,sizeof visited);  //{????????????????????????1bit??????????????????1bit????????§?????????} 
	
	queue<int> que;
	int i,j;
	for(j=0;j<W;j++){ 
	 for(i=0;i<H;i++){
	  if(c[i][j]=='@'){
	  	que.push(j+i*W);
	  	visited[i][j]=true;
	  	 break;
	  }
	 }
	}
  int answer=1;
  while(!que.empty()){
	int cur=que.front();
	        que.pop();
	        int x=cur%W;
	        int y=cur/W;
    

  for(i=0;i<4;i++){
	int nx=x+dx[i],ny=y+dy[i];
	
  
  
  if(!(0<=nx&&nx<W&&0<=ny&&ny<H))continue;
  if(c[ny][nx]=='#'||visited[ny][nx])continue;

  que.push(nx+ny*W);
  visited[ny][nx]=true;
++answer;
    }
   }
 //cout << answer << endl;
 printf("%d\n",answer);
 }

C++: #include<iostream>
#include<vector>
using namespace std;
const int MAX = 20;
char map[MAX][MAX];
bool book[MAX][MAX];
struct Point
{
	int x;
	int y;
	Point(int x = 0, int y = 0) :x(x), y(y) {}
}start;
int sum = 0;
void depthFSearch(int H, int W, Point start)
{
	int next[4][2] = { {0,1},{1,0},{0,-1},{-1,0} };
	for (int k = 0; k <= 3; k++)
	{
		int nx = start.x + next[k][0];
		int ny = start.y + next[k][1];
		if (nx < 0 || nx >= H || ny < 0 || ny >= W||map[nx][ny]=='#')
			continue;
		if (!book[nx][ny] && map[nx][ny] == '.')
		{
			book[nx][ny] = true;
			Point p(nx, ny);
			sum++;
			depthFSearch(H, W, p);
		}
	}
	return;
}
int main()
{
	while (true)
	{
		int H, W;
		cin >> W >> H;
		if (!H && !W)
			break;
		for (int i = 0; i < H; i++)
			for (int j = 0; j < W; j++)
			{
				cin >> map[i][j];
				book[i][j] = false;
				if (map[i][j] == '@')
					start = Point(i, j);
			}
		book[start.x][start.y] = true;
		sum = 1;
		depthFSearch(H, W, start);
		cout << sum << endl;
	}
}

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

int G[1010];
int R[110][110];
int D[1010];
int bfs(int src,int num);

int main() {
	int W,H,root;
	char c;


	while (true) {
		for(int i=0;i<100;i++) {
			for(int j=0;j<100;j++) {
				R[i][j] = 0;
			}
		}
		for(int i=0;i<1000;i++) {
			G[i] = 0;
		}
		for(int i=0;i<1000;i++) {
			D[i] = 100;
		}
		cin >> W >> H;
		if((W == 0) && (H == 0)) break;
		for(int i=0;i<W*H;i++) {
			cin >> c;
			if(c == '.') G[i] = 1;
			if(c == '#') G[i] = 0;
			if(c == '@') {
				G[i] = 1;
				root = i;
			}
		}
		for(int i=0;i<W*H;i++) {
			if(G[i] == 1) {
				if(i%W != 0) { //????????????
					if(G[i-1] == 1) {//??????????????? 
						R[i][i-1] = 1;
					}
				}
				if(i%W != W-1) { //????????????
					if(G[i+1] == 1) { //???????????????
						R[i][i+1] = 1;
					}			

				}
				if(i > W-1) { //???????????????
					if(G[i-W] == 1) { //???????????????
						R[i][i-W] = 1;
					}	

				}
				if(i < W*H-W) {//???????????????
					if(G[i+W] == 1){
						R[i][i+W] = 1;
					}	

				}
			}
		}
		cout << bfs(root,W*H) << endl;
	}
	/*
	for(int i=0;i<99;i++) {
		cout << i << " " << G[i] << endl;
	}
	cout << G[98] << endl;
	cout << R[87][98] << endl;
	*/
	
}





int bfs(int src,int num) {
	int cur,dst;
	int count = 1;
	//cerr << "bfs root = " << src << endl;
	queue<int> Q; // ??´??°????????????????????\????????????
	Q.push(src);
	D[src] = 0; // ?????????
	while (!Q.empty()) {
		
		int cur = Q.front(); // ??????????´????????????????
		Q.pop();
// ???????¢??????¨??¨???
		//cerr << cur << " " << D[cur] << endl;
		for (int dst=0;dst<num;dst++) { // ???????????? dst ????????????
			if ((R[cur][dst] == 1) && (D[dst] == 100)) { // cur ?????? dst ??????????????????dst ???????¨??????????
				D[dst] = D[cur]+1; //
				Q.push(dst); // dst ????¨???????????????????
				count++;
			}
		}
	}
	return count;
}

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<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[y][x]!=',')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);
  }
}