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 <bits/stdc++.h>
using namespace std;

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

int main(){
    while(true){
        int w, h; cin >> w >> h;
        if(w == 0 && h == 0) break;
        int sx, sy; //スタート地点の場所
        vector<vector<char>> mp(h, vector<char>(w)); //入力を受け取る二次元配列
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                cin >> mp[i][j];
                if(mp[i][j] == '@'){
                    sx = j;
                    sy = i;
                }
            }
        }
        vector<vector<int>> ok(h, vector<int>(w)); //到達可能なら1を代入する
        queue<pair<int, int>> que;
        que.push({sx, sy});
        ok[sy][sx] = 1;
        while(!que.empty()){
            int x = que.front().first, y = que.front().second; //現在地を取得する
            que.pop();
            for(int i = 0; i < 4; i++){
                if(x + dx[i] < 0 || x + dx[i] >= w || y + dy[i] < 0 || y + dy[i] >= h) continue; //mapの範囲外はcontinue
                if(mp[y+dy[i]][x+dx[i]] == '#' || ok[y+dy[i]][x+dx[i]] == 1) continue; //障害物で行けないor一度行ったところはスルー
                //それ以外の場合
                ok[y+dy[i]][x+dx[i]] = 1; //okに1を代入する
                que.push({x+dx[i], y+dy[i]}); //現在地からまだ行ってない場所のうち,行ける場所を新たに追加
            }
        }
        int ans = 0; 
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){ //okの要素を全て足すと行ける場所の個数がわかる
                ans += ok[i][j];
            }
        }
        cout << ans << endl;
    }
}

C++: #include<iostream>
#define MAX (256)
using namespace std;

int N, M;
char field[MAX][MAX];
int sx, sy;
int ans = 0;

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

void dfs (int x, int y){
	if(0 <= x && x < N && 0 <= y && y < M && field[x][y] == '.'){
		field[x][y] = '#';
		ans++;
		
		for(int i =0; i < 4; i++){
			int nx = x + dx[i], ny = y + dy[i];
			dfs(nx, ny);
		
		}
	}
}

int main(){

	while(1){
	ans = 0;
	cin >> M >> N;
	
	if(M == 0 && N == 0) break;
	
	for(int i =0; i < N; i++){
		for(int j =0; j < M; j++){
			cin >> field[i][j];
		}
	}
	
	for(int i =0; i < N; i++){
		for(int j =0; j < M; j++){
			if(field[i][j] == '@'){
				sx = i;
				sy = j;
				field[sx][sy] = '.';
			}
		}
	}
		
	dfs(sx, sy);
	
	cout << ans << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
#include<stdio.h>
#include<queue>
#include <climits>
#include <map>
#include <set>
const long long mod = 1e9 + 7;
const int INF = 1 << 20;
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

char f[30][30];
int w,h;
int dx[4] = {-1,0,0,1};
int dy[4] = {0,-1,1,0};
int ans;

void dfs(int x,int y)
{
    f[y][x] = '#';
    ans++;
    for(int i = 0; i < 4; i++){
        int ny = y+dy[i];
        int nx = x+dx[i];
        if(ny >= 0 && ny < h && nx >= 0 && nx < w && f[ny][nx] == '.'){
            dfs(nx,ny);
        }
    }
    return ;
    
}

int main()
{
    while(true){
        cin >> w >> h;
        ans = 0;
        if(w == 0 && h == 0) break;
        int sx,sy;
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                cin >> f[i][j];
                if(f[i][j] == '@'){
                    sx = j;
                    sy = i;
                }
            }
        }
        dfs(sx,sy);
        cout << ans << endl;
    }
}

C++: #include <iostream>
#include <queue>

using namespace std;

char graph[20][20];
int cou = 0;
int W,H;

void bfs(int a1,int a2){
  queue<int> Q;
  int u1,u2;
  Q.push(a1);
  Q.push(a2);

  while(!Q.empty()){

    u1 = Q.front();Q.pop();
    u2 = Q.front();Q.pop();

    if(graph[u1+1][u2] == '.'){
      graph[u1+1][u2] = 'N';
      cou++;
      Q.push(u1+1);Q.push(u2);
    }

    if(graph[u1][u2+1] == '.'){
      graph[u1][u2+1] = 'N';
      cou++;
      Q.push(u1);Q.push(u2+1);
    }

    if(graph[u1-1][u2] == '.'){
      graph[u1-1][u2] = 'N';
      cou++;
      Q.push(u1-1);Q.push(u2);
    }

    if(graph[u1][u2-1] == '.'){
      graph[u1][u2-1] = 'N';
      cou++;
      Q.push(u1);Q.push(u2-1);
    }
    /*
    for(int i = 0; i< H ;i++){
      for(int j = 0; j < W; j++){
	cout << graph[i][j];
      }
      cout << endl;
      }*/
  }
}


int main(){

  int a1,a2;

  while(1){

    cin >> W >> H;

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


    for(int i = 0; i< H ;i++){
      for(int j = 0; j < W; j++){
	cin >> graph[i][j];
	if(graph[i][j] == '@'){
	  a1 = i;
	  a2 = j;
	}
      }
    }

    /*
      for(int i = 0; i< H ;i++){
      for(int j = 0; j < W; j++){
      cout << graph[i][j];
      }
      cout << endl;
      }
    */

    bfs(a1,a2);

    cout << cou+1 << endl;

    cou = 0;
    for(int i = 0;i < H ;i++){
      for(int j = 0;j < W; j++){
	graph[i][j] = 0;
      }
    }

  }
  return 0;
}