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++: /*http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1130&lang=jp*/

#include <iostream>

using namespace std;

struct point{
    int x;
    int y;
};

char map[20][20];

int move(const struct point p,int h,int w){
    int count=1;
    map[p.x][p.y]='/';
    for (int i = 0; i < 4; ++i) {
        struct point tmp=p;

        switch (i){
            case 0:
                tmp.x++;
                break;
            case 1:
                tmp.x--;
                break;
            case 2:
                tmp.y++;
                break;
            case 3:
                tmp.y--;
                break;
            default:
                break;
        }
        if (tmp.x<w && tmp.y<h && map[tmp.x][tmp.y]=='.') {
            count+=move(tmp,h,w);
        }
    }
    return count;
}

int main(){
    int w,h;

    while (cin>>w>>h && (w>0 || h>0)){
        struct point start;

        for (int i = 0; i < h; ++i) {
            for (int j = 0; j < w; ++j) {
                cin>>map[j][i];
                if(map[j][i]=='@'){
                    start.x=j;
                    start.y=i;
                }
            }
        }
        cout<<move(start,h,w)<<endl;
    }
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
const int INF = 1e9;

//pair<int,int>;

int W,H;
vector<string> s(20);
int nextX[] = {1,0,-1,0};
int nextY[] = {0,1,0,-1};
vector<vector<int>> count_map(20,vector<int>(20,0));

bool range_check(int y,int x){
  if(y<0||x<0||y>=H||x>=W){
    return false;
  }
  return true;
}

void bfs(int y,int x){
  if(!range_check(y,x)){
    return;
  }
  //printf("%d %d",y,x);
  if(s[y][x]=='#'){
    //printf("aaa\n");
    return;
  }
  if(count_map[y][x]){
    return;
  }
  count_map[y][x]=1;
  queue<pair<int,int>> node;
  for(int i=0;i<4;i++){
    int ny = y+nextY[i],nx = x + nextX[i];
    if(!range_check(ny,nx)){
      continue;
    }
    if(s[ny][nx]=='.'){
      node.emplace(ny,nx);
    }
  }
  while(!node.empty()){
    pair<int,int> yx = node.front();
    node.pop();
    bfs(yx.first,yx.second);
  }
}

int main(){
  while(1){
    cin>>W>>H;
    if(W==0&&H==0){
      break;
    }
    for(int i=0;i<H;i++){
      cin>>s[i];
    }
    for(int i=0;i<H;i++){
      for(int j=0;j<W;j++){
        if(s[i][j]=='@'){
          bfs(i,j);
        }
      }
    }
    int ans=0;
    for(int i=0;i<H;i++){
      for(int j=0;j<W;j++){
        //printf("%3d",count_map[i][j]);
        if(count_map[i][j]){
          ans++;
          count_map[i][j]=0;
        }
      }
      //cout<<endl;
    }
    cout<<ans<<endl;
  }
  return 0;
}

C++: #include<iostream>
#include<string>
#include<queue>
#include<vector>
using namespace std;
#define frp(i,n) for((i)=0;(i)<(n);i++)
int vx[4]={0,1,0,-1};
int vy[4]={1,0,-1,0};
int H,W;
int check(int n1,int n2,int rednum,vector<int> red1,vector<int> red2){
  for(int i=0;i<rednum;i++){
    if(n1==red1[i]&&n2==red2[i]||n1<0||n2<0||n1>=H||n2>=W){
      return 0;
    }
  }
  return 1;
}
int main(){
  int i,j,now1,now2;
  while(cin>>W>>H,W+H!=0){
    int num=1;
    string str2;
    vector<string> str;
    vector<int> red1;
    vector<int> red2;
    frp(i,H){
      cin>>str2;
      str.push_back(str2);
    }
    frp(i,H){
      frp(j,W){
	if(str[i][j]=='#'){
	  red1.push_back(i);
	  red2.push_back(j);
	}
      }
    }
    queue<pair<int,int> > qii;
    frp(i,H){
      frp(j,W){
	if(str[i][j]=='@'){
	  qii.push(make_pair(i,j));
	  red1.push_back(i);
	  red2.push_back(j);
	}
      }
    }
    while(!qii.empty()){
      now1=qii.front().first;
      now2=qii.front().second;
      qii.pop();//?????????????´??????´?£?
      for(i=0;i<4;i++){
	if(check(now1+vx[i],now2+vy[i],red1.size(),red1,red2)==1){
	  cout<< now1<<" "<<now2<<endl;
	  red1.push_back(now1+vx[i]);
	  red2.push_back(now2+vy[i]);
	  qii.push(make_pair(now1+vx[i],now2+vy[i]));
	  num++;
	}
      }
    }
    cout<<num<<endl;
  }
  return 0;
}

C++: #include<iostream>
#include<fstream>
#include<vector>

//探索関数
//引数の座標が移動可能なら1を足し、上下左右を再探索。
//同じ地点をカウントしないよう、一度カウントした座標はtに置き換える。
int tansaku(char tile[20][20],int w1,int h1){
    if(tile[w1][h1]!='.'&&tile[w1][h1]!='@')return 0;
    tile[w1][h1]='t';
    return 1+tansaku(tile,w1+1,h1)+tansaku(tile,w1-1,h1)+tansaku(tile,w1,h1+1)+tansaku(tile,w1,h1-1);
}

int main(){
    int w,h;
    int w1,h1; //スタート地点 @ の座標
   while(1){
    std::cin >> w;
    std::cin >> h;
    if(w==0&&h==0)break;
    //配列の初期化
    char tile[20][20]={};
    // 配列を取得.
    for(int i=0; i<h; i++){
        for(int j=0; j<w; j++){
            std::cin >> tile[j][i];
            if(tile[j][i]=='@'){
                w1=j; h1=i;
            }
        }
    }
    std::cout << tansaku(tile,w1,h1) << std::endl;
   }
    return 0;
}