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 <queue>
#include <utility>
#include <algorithm>
using namespace std;

int w, h;
char tiles[20][21];

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

bool isRegion(int x, int y){
    return x >= 0 && x < w && y >= 0 && y < h;
}

int main(){
    while(1){
        cin >> w >> h;
        if(!w && !h) break;
        
        int sx, sy;
        for(int i = 0; i < h; i++){
            cin >> tiles[i];
            
            auto j = find(tiles[i], tiles[i]+w, '@');
            if(j != tiles[i] + w){
                sx = j - tiles[i]; sy = i;
            }
        }
        
        int cnt = 0;
        queue<pair<int, int> > q;
        q.push(make_pair(sx, sy));
        while(!q.empty()){
            cnt++;
            int x = q.front().first, y = q.front().second;
            q.pop();
            
            for(int i = 0; i < 4; i++){
                int nx = x + dx[i], ny = y + dy[i];
                
                if(isRegion(nx, ny) && tiles[ny][nx] == '.'){
                    tiles[ny][nx] = '#';
                    q.push(make_pair(nx, ny));
                }
            }
        }
        cout << cnt << endl;
        
    }
}

C++: #include <iostream>
#include <cstring>
using namespace std;

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

char f[50][50];
bool v[50][50];

int M,N;

int dfs(int x, int y)
{
	int ret=1;
	v[x][y]=1;

	for(int i=0; i<4; i++)
	{
		int tx=x+dx[i], ty=y+dy[i];
		if(tx<0||tx>=M||ty<0||ty>=N) continue;
		if(f[tx][ty]=='#') continue;
		if(v[tx][ty]) continue;

		ret+=dfs(tx,ty);
	}

	return ret;
}

int main()
{
	while(cin >> M >> N, (M||N))
	{
		int sx,sy;
		memset(v,0,sizeof(v));
		for(int i=0; i<N; i++)
		for(int j=0; j<M; j++)
		{
			cin >> f[j][i];
			if(f[j][i]=='@')
			{
				sx=j;
				sy=i;
			}
		}

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

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

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

int a,b,ans;
int q[20+5][20+5];
char p[20+5][20+5];

using namespace std;
int main() {
	queue<pair<int,int> > M;
	pair<int,int> X;
	cin >> a >> b;
	for (int i=1; i<=b; i++){
		for (int j=1; j<=a; j++){
			cin >> p[i][j];
			if (p[i][j] == '@'){
				M.push(make_pair(i,j));
				ans = 1;
				q[i][j] = 1;
			}
		}
	}
	

	while(!M.empty()){
		a = M.front().first;
		b = M.front().second;
		M.pop();
		for (int i=0; i<=3; i++){		
			if (p[a+dx[i]][b+dy[i]]=='.' && q[a+dx[i]][b+dy[i]]==0){
				M.push(make_pair(a+dx[i],b+dy[i]));
				q[a+dx[i]][b+dy[i]] = 1;
				ans+=1;
			}
		}
	}
	cout << ans << endl;
	
}

C++: #include <iostream>

using namespace std;

struct D
{
  bool movable[20][20];
};

int const dir[][2] = {
  {  1, 0 },
  { -1, 0 },
  { 0,  1 },
  { 0, -1 }
};

void count( D& d, int& num, int const px, int const py )
{
  if ( !d.movable[px][py] )
    return;

  ++num;
  d.movable[px][py] = false;

  for( int i=0; i<4; ++i ) {
    int const npx = px + dir[i][0];
    int const npy = py + dir[i][1];
    if ( npx < 0 || npy < 0 || npx >= 20 || npy >= 20 )
      continue;
    count( d, num, npx, npy );
  }
}

int main()
{
  int w, h;
  while( cin >> w >> h ) {
    if ( w == 0 && h == 0 )
      break;

    D d = {};
    int px, py;
    for( int y=0; y<h; ++y ) {
      for( int x=0; x<w; ++x ) {
        char c;
        cin >> c;
        if ( c == '@' ) {
          px = x;
          py = y;
        }

        d.movable[x][y] = ( c != '#' );
      }
    }

    int num = 0;
    count( d, num, px, py );
    cout << num << endl;
  }
}