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++: // 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;
    }
}