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>
using namespace std;

struct Point
{
  int i;
  int j;
};

static Point minus_point = { -1, -1 };

class Map
{
  public:
    Map(int height, int width);
    ~Map();

  public:
    int Can_go_num(Point p = minus_point);

  private:
    bool CheckIndex(const Point& p);
    bool isBlack(const Point& p);
    bool CanMove(const Point& p);

  private:
    char** m_map;   //  '.' = black, '#' = red, '@' = start, 'x' = already
    int m_height;
    int m_width;
    Point m_start;
};

Map::Map(int height, int width)
{
  m_height = height;
  m_width = width;

  m_map = new char*[m_height];
  for (int i = 0; i < m_height; ++i)
  {
    m_map[i] = new char[m_width];
  }

  for (int i = 0; i < m_height; ++i)
  {
    for (int j = 0; j < m_width; ++j)
    {
      cin >> m_map[i][j];
      if (m_map[i][j] == '@')
      {
        m_start.i = i;
        m_start.j = j;
      }
    }
  }
}

Map::~Map()
{
  for (int i = 0; i < m_height; ++i)
  {
    delete[] m_map[i];
  }
  delete[] m_map;
}

int Map::Can_go_num(Point p)
{
  int result = 1;
  if (p.i == -1)
  {
    p = m_start;
  }
  m_map[p.i][p.j] = 'x';
  Point buf_ps[4] = {
    { p.i, p.j-1 },
    { p.i, p.j+1 },
    { p.i-1, p.j },
    { p.i+1, p.j },
  };
  for (Point buf_p : buf_ps)
  {
    if (CanMove(buf_p) )
    {
      result += Can_go_num(buf_p);
    }
  }

  return result;
}

inline bool Map::CheckIndex(const Point& p)
{
  return (0 <= p.i and p.i < m_height) and (0 <= p.j and p.j < m_width);
}

inline bool Map::isBlack(const Point& p)
{
  return m_map[p.i][p.j] == '.';
}

inline bool Map::CanMove(const Point& p)
{
  return CheckIndex(p) && isBlack(p);
}

int main(void)
{
  int height, width;
  while (true)
  {
    cin >> width >> height;
    if (height == 0 && width == 0) break;
    Map map(height, width);
    cout << map.Can_go_num() << endl;
  }
  return 0;
}

C++: #include<iostream>
#include<string>
#include<queue>
using namespace std;
int w,h;
string map[40];
int vec[4][2]={{0,1},{0,-1},{-1,0},{1,0}};

struct pos{
	pos(int X,int Y){
		x=X,y=Y;
	}
	int x,y;
};

int move(int x,int y){
	int ans=0;
	map[x][y]='#';
	queue<pos> dfs;
	dfs.push(pos(x,y));
	while(!dfs.empty()){
		pos n=dfs.front();dfs.pop();
		x=n.x;
		y=n.y;
		////
		ans++;
		for(int i=0;i<4;i++){
			if((x+vec[i][0]>=0)&&(y+vec[i][1]>=0)&&(x+vec[i][0]<h)&&(y+vec[i][1]<w)){
				if(map[x+vec[i][0]][y+vec[i][1]]=='.'){
					map[x+vec[i][0]][y+vec[i][1]]='#';
					dfs.push(pos(x+vec[i][0],y+vec[i][1]));
				}
			}
		}
		/////
	}
	
	
	return ans;
}

int main(){
	while(cin>>w>>h,w){
		int fx,fy;
		for(int i=0;i<h;i++){
			cin>>map[i];
		}
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(map[i][j]=='@'){
					fx=i;
					fy=j;
					break;
				}
			}
		}
		int ans=move(fx,fy);
		cout<<ans<<endl;
		
	}
}

C++: /*
ツ個ゥツづ按づェツつスツ姪「ツ妥ィツづ?つキツづ?
'@'ツづ個湘ェツ渉環つゥツづァ, ツ深ツつウツ優ツ静ヲツ探ツ催オツづ?嘉ーツつォツづ慊つオツづ・ツつ、.
 */
#include <iostream>
using namespace std;

int w, h;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
char t[22][22];

int solve(int x, int y){
  int res = 1;

  t[y][x] = '#';

  for(int i = 0; i < 4; i++){
    int nx = x + dx[i];
    int ny = y + dy[i];

    if(0 <= nx && nx < w && 0 <= ny && ny < h && t[ny][nx] != '#'){
      res += solve(nx, ny);
    }
  }

  return res;
}

int main(){
  while(cin >> w >> h, w || h){
    int sx, sy;

    for(int i = 0; i < h; i++){
      cin >> t[i];

      for(int j = 0; j < w; j++){
        if(t[i][j] == '@'){
          sy = i;
          sx = j;
          t[i][j] = '.';
        }
      }
    }

    cout << solve(sx, sy) << endl;
  }
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>

using namespace std;

int solve(int n,int m){
	int flag[21][21]={};
	int field[21][21]={};
	queue<pair<int,int> >q;
	for(int i=0;i<m;i++){
		string s;
		cin >> s;
		for(int j=0;j<n;j++){
			if(s[j]=='.')field[i][j]=0;
			if(s[j]=='#')field[i][j]=-1;
			if(s[j]=='@'){
				field[i][j]=1;
				flag[i][j]=1;
				q.push(make_pair(i,j));
			}
		}
	}
	while(!q.empty()){
		pair<int,int> p = q.front();
		q.pop();
		int a = p.first;
		int b = p.second;
		if(a>0){
			if(flag[a-1][b]==0&&field[a-1][b]==0){
				q.push(make_pair(a-1,b));
				flag[a-1][b]=1;
			}
		}
		if(a<m-1){
			if(flag[a+1][b]==0&&field[a+1][b]==0){
				q.push(make_pair(a+1,b));
				flag[a+1][b]=1;
			}
		}
		if(b>0){
			if(flag[a][b-1]==0&&field[a][b-1]==0){
				q.push(make_pair(a,b-1));
				flag[a][b-1]=1;
			}
		}
		if(b<n-1){
			if(flag[a][b+1]==0&&field[a][b+1]==0){
				q.push(make_pair(a,b+1));
				flag[a][b+1]=1;
			}
		}
	}
	int ans=0;
	for(int i=0;i<m;i++){
		for(int j=0;j<n;j++){
			if(flag[i][j]==1)ans+=1;
		}
	}
	return ans;
}


int main(){
	int n,m;
	while(1){
		cin >> n >> m;
		if(n==0&&m==0)break;
		cout << solve(n,m) <<endl;
	}
	return 0;
}