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++: // Red and Black
// 問題URL: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1130&lang=jp
// 中央*右
// 
// 所用時間: 1h
// BFSで解きました、キューの使い方について理解が深まりました。

#include <iostream>
#include <iostream>
#include <string>
#include <queue>

using namespace std;

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

int W, H,stX, stY, curX, curY, buffX, buffY;
string buff, FD[25];
queue<int> Qx, Qy;

bool isRed(int x, int y){ //(x,y)が赤か?
    return FD[y][x] == '#';
}

bool inField(int x, int y){ //(x,y)がフィールド内か?
    return 0 <= x && x <= (W-1) && 0 <= y && y <= (H-1);
}

void solve(){
    int cnt = 0;
    bool Checked[25][25]={};
    Qx.push(stX);
    Qy.push(stY);
    cnt++;
    Checked[stY][stX] = true;

    while(!Qx.empty()){ //キューが空になるまで
        curX = Qx.front(); curY = Qy.front();
        Qx.pop(); Qy.pop();
        // printf("(curX, curY) = (%d, %d)\n", curX, curY);

        for(int i = 0; i < 4; i++){//現在地の周囲4マスについて
            buffX = curX + dx[i]; buffY = curY + dy[i];
            if(inField(buffX, buffY) && !isRed(buffX, buffY) && !Checked[buffY][buffX]){ //未調査で移動可能であれば
                Qx.push(buffX); Qy.push(buffY);
                Checked[buffY][buffX] = true;
                cnt++;
            }
        }
    }
    cout << cnt << endl;
}

int main(){
    while(cin >> W >> H && W){
        for(int i = 0; i < H; i++){
            cin >> buff;
            FD[i] = buff;
            for(int j = 0; j < buff.size(); j++){
                    if(buff[j]=='@'){ //スタート地点を見つけたら記録する
                        stX = j;
                        stY = i;
                        // printf("(stX, stY) = (%d, %d)\n", stX, stY);
                    }
                }
            }
    solve();
    }
}

C++: #include <bits/stdc++.h>

#define rep(i,a,b) for(int i=a;i<b;i++)



using namespace std;



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



int main(){



	int W,H,sx,sy;

	string s[25];

	bool flag[25][25];



	while(1){

		cin>>W>>H;

		if(W==0&&H==0) break;

		for(int i=0; i<H; i++){

			cin>>s[i];

			for(int j=0; j<W; j++){

				if(s[i][j] == '@'){

					sx = j;

					sy = i;

				}

				flag[i][j] = false;

			}

		}





		queue<int> qx;

		queue<int> qy;

		qx.push(sx);

		qy.push(sy);



		flag[sy][sx] = true;



		int x, y, cnt = 1;

		while(!qx.empty()){



			x = qx.front();

			y = qy.front();

			qx.pop();

			qy.pop();



			for(int k=0; k<4; k++){

				int nx = x+dx[k], ny = y+dy[k];

				if(0<=nx && nx<W && 0<=ny && ny<H){

					if(s[ny][nx] == '.' && !flag[ny][nx]){

						qx.push(nx);

						qy.push(ny);

						flag[ny][nx] = true;

						cnt++;

					}

				}

			}

		}

		cout<<cnt<<endl;



	}	



	return 0;

}

C++: #include<iostream>
#include<queue>
#include<utility>
using namespace std;
int main(){
    int W,H,x0,y0,sum=0;
    char t[20][20];
    bool visit[20][20];
    const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};
    typedef pair<int,int>P;
    queue<P>Q;
    while(cin>>W>>H){
        if(W==0||H==0)break;
        for(int i=0;i<H;++i){
            for(int j=0;j<W;++j){
                cin>>t[i][j];
                if(t[i][j]=='@'){
                    x0=j;
                    y0=i;
                }
                visit[i][j]=true;
            }
        }
        Q.push(P(x0,y0));
        visit[y0][x0]=false;
        while(!Q.empty()){
            int srcx=Q.front().first;
            int srcy=Q.front().second;
            Q.pop();
            sum++;
            for(int i=0;i<4;++i){
                int curx=srcx+dx[i];
                int cury=srcy+dy[i];
                if(t[cury][curx]=='#') continue;
                if(curx<0||curx>=W||cury<0||cury>=H) continue;
                if(!visit[cury][curx]) continue;
                visit[cury][curx]=false;
                Q.push(P(curx,cury));
            }
            
        }
        cout<<sum<<endl;
    }
}

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

static const int margin = 1;
static const int board_size = 32;

char board[board_size][board_size];
int W, H;
int ans_accume;

void init() {
	for(int i = 0; i < board_size; i++) {
		for(int j = 0; j < board_size; j++) {
			board[i][j] = '#';
		}
	}
	ans_accume = 0;
}

bool input() {
	cin >> W >> H;
	if(!W && !H)
		return false;
	for(int i = margin; i < H + margin; i++) {
		cin >> board[i] + margin;
	}
	return true;
}

// (p.second, p.first)
void solve(pair<int, int> p) {
	if(board[p.first][p.second + 1] == '.') {
		board[p.first][p.second + 1] = 'x';
		ans_accume++;
		solve(pair<int, int>(p.first, p.second + 1));
	}
	if(board[p.first][p.second - 1] == '.') {
		board[p.first][p.second - 1] = 'x';
		ans_accume++;
		solve(pair<int, int>(p.first, p.second - 1));
	}
	if(board[p.first + 1][p.second] == '.') {
		board[p.first + 1][p.second] = 'x';
		ans_accume++;
		solve(pair<int, int>(p.first + 1, p.second));
	}
	if(board[p.first - 1][p.second] == '.') {
		board[p.first - 1][p.second] = 'x';
		ans_accume++;
		solve(pair<int, int>(p.first - 1, p.second));
	}
}

pair<int, int> search_start() {
	for(int i = margin; i < H + margin; i++) {
		for(int j = margin; j < W + margin; j++) {
			if(board[i][j] == '@') {
				ans_accume++;
				return pair<int, int>(i, j);
			}
		}
	}

	return pair<int, int>(-1, -1);
}

int main(void) {
	while(init(), input()) {
		solve(search_start());
		cout << ans_accume << endl;
	}
}