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 <iostream>
#include <string>

using namespace std;

string map[21];
int w,h;
int dx[] = { -1, 1 , 0 , 0 };
int dy[] = { 0 , 0 , -1, 1 };

void check(int x , int y){
	int mx,my;

	//ãº¶EÌSûüÉ²×é
	for(int i=0 ; i<4 ; i++){
		mx = x + dx[i];
		my = y + dy[i];
		//tB[hOÉoÄ¢È¯êÎ²×é(0ÈãE³Èà)
		if(mx>=0 && mx<w && my>=0 && my<h){
			if(map[my][mx]=='.'){//^C¾Á½çhèÂÔµÄÄA
				map[my][mx] = 'f';
				check( mx , my );
			}
		}
	}
}

int main(){
	int sx,sy,count;

	while(1){
		//ÏÌú»
		count = 0;

		//E³ÌüÍ
		cin >> w >> h;
		
		if(w==0 && h==0)break;//0 0ªüÍ³ê½çI¹

		//üÍµ½lðmap[][]Éãü
		for(int y=0 ; y<h ; y++){
			cin >> map[y];
		}

		for(int y=0 ; y<h ; y++){
			for(int x=0 ; x<w ; x++){
				if(map[y][x]=='@'){//@¾Á½çX^[gn_
					sy = y;
					sx = x;
					map[y][x] = 'f';
				}	
			}
		}
		
		check( sx , sy );//X^[gn_Ìx,yÀWðnµÄ²×é

		//^CÌJEg(fÌÂð¦é)
		for(int y=0 ; y<h ; y++){
			for(int x=0 ; x<w ; x++){
				if(map[y][x]=='f')count++;
			}
		}

		//¦ÌoÍ
		cout << count << endl;
	}

	return 0;
}

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 <algorithm>
#include <deque>
#include <string>
#include <utility>

#define phb push_back
#define ppf pop_front
#define mkp make_pair
#define r first
#define c second
using namespace std;

typedef pair< int, int > Pr;
const int kDir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};

int R, C, SR, SC;
char M[25][25];

void read();
void solve();

int main() {
  while (cin >> C >> R, R != 0 && C != 0)
    read(), solve();

  return 0;
}

void read() {
  fill(&M[0][0], &M[25][0], '#');
  for (int i = 1; i <= R; ++i)
    for (int j = 1; j <= C; ++j) {
      cin >> M[i][j];
      if (M[i][j] == '@')
        M[i][j] = '.', SR = i, SC = j;
    }
}

void solve() {
  static int vst[25][25], vst_cnt;
  int res = 0;
  deque< Pr > que;

  que.phb(mkp(SR, SC)), vst[SR][SC] = ++vst_cnt;
  while (!que.empty()) {
    Pr cur = que.front(); que.ppf(), ++res;
    for (int di = 0; di < 4; ++di) {
      Pr nxt(cur.r + kDir[di][0], cur.c + kDir[di][1]);
      if (M[nxt.r][nxt.c] == '.' && vst[nxt.r][nxt.c] != vst_cnt)
        que.phb(nxt), vst[nxt.r][nxt.c] = vst_cnt;
    }
  }

  cout << res << "\n";
}