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 <cstdio>
#include <iostream>
#include <queue>
#include <set>
#include <cstring>
using namespace std;
typedef pair<int,int> pos;
char map[20][20];
const int next[4][2] = {
  {-1,0},{1,0},{0,-1},{0,1},
};
int main()
{
  for(;;){
    int w,h;
    scanf("%d %d",&w,&h);
    if(w == 0 || h == 0) break;
    string dummy;
    getline(cin,dummy);
    memset(map,'.',sizeof(map));
    pos p;
    for(int i=0; i<h; i++){
      string s;
      getline(cin,s);
      for(int j=0; j<w; j++){
        if(s[j] == '@'){
          p = make_pair(i,j);
        }
        map[i][j] = s[j];
      }
    }
    set<pos> memo;
    memo.insert(p);
    queue<pos> q;
    q.push(p);
    for(;;){
      if(q.empty()) break;
      p = q.front(); q.pop();
      for(int i=0; i<4; i++){
        pos pn = make_pair(p.first+next[i][0], p.second+next[i][1]);
        if(pn.first < 0 || pn.first >= h || pn.second < 0 || pn.second >= w ||
           map[pn.first][pn.second] == '#'){
          continue;
        }
        if(memo.insert(pn).second){
          q.push(pn);
        }
      }
    }
    printf("%d\n", memo.size());
  }
  return 0;
}

C++: #include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <deque>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <complex>
#include <functional>
#include <utility>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>

using namespace std;

#define dump(n) cout<<"# "<<#n<<"="<<(n)<<endl
#define debug(n) cout<<__FILE__<<","<<__LINE__<<": #"<<#n<<"="<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,n) repi(i,0,n)
#define iter(c) __typeof((c).begin())
#define tr(c,i) for(iter(c) i=(c).begin();i!=(c).end();i++)
#define allof(c) (c).begin(),(c).end()
#define mp make_pair

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

int search(int i,int j,vector<string>& field)
{
    int r=field.size(),c=field[0].size();
    if(i<0 || r<=i || j<0 || c<=j)
        return 0;
    if(field[i][j]=='#')
        return 0;
    field[i][j]='#';
    int di[]={-1,1,0,0};
    int dj[]={0,0,-1,1};
    int sum=1;
    rep(k,4)
        sum+=search(i+di[k],j+dj[k],field);
    return sum;
}

int main()
{
	for(int c,r;cin>>c>>r,c|r;){
        vector<string> field(r);
        rep(i,r)
            cin>>field[i];
        rep(i,r)
            rep(j,c)
                if(field[i][j]=='@')
                    cout<<search(i,j,field)<<endl;
    }
}

C++: #include <iostream>
#include <vector>
#include <functional>
#include <set>
using namespace std;

int main() {
  int h, w;
  while(cin >> w >> h, w || h) {
    vector<vector<bool> > qfield(h, vector<bool>(w)); // black: true, red: true
    int starty, startx;
    for(int y = 0; y < h; ++y) for(int x = 0; x < w; ++x) {
      char c;
      cin >> c;
      qfield[y][x] = (c == '#' ? false : true);
      if(c == '@') {
        starty = y;
        startx = x;
      }
    }
    set<pair<int, int> > countedPoses;
    function<int(int, int)> tileCount = [&](int y, int x) {
      int rtn = 0;
      if(y >= 0 && y < h && x >= 0 && x < w && qfield[y][x] && countedPoses.count(make_pair(y, x)) == 0) {
        countedPoses.insert(make_pair(y, x));
        int dy[] = {0, 1, 0, -1};
        int dx[] = {1, 0, -1, 0};
        for(int i = 0; i < 4; ++i) rtn += tileCount(y + dy[i], x + dx[i]);
        ++rtn;
      }
      return rtn;
    };
    cout << tileCount(starty, startx) << endl;
  }
  return 0;
}

C++: //
//  RedAndBlack.cpp
//  
//
//  Created by ??°????????? on 2015/10/30.
//
//

#include <iostream>
using namespace std;

int w, h, num = 0, room[20][20];
int dx[] = {-1, 1, 0, 0};
int dy[] = { 0, 0,-1, 1};

void dfs(int x, int y) {
    num++;
    room[y][x] = 0;
    
    // ??????????????¢?´¢
    for (int i = 0; i < 4; i++) {
        int mx = x + dx[i];
        int my = y + dy[i];
        
        if (mx < 0 || w <= mx) continue;
        if (my < 0 || h <= my) continue;
        if (room[my][mx] == 0) continue;
        dfs(mx, my);
    }

    return;
}

int main() {
    while (cin >> w >> h, w || h) {
        int manX, manY;
        
        for (int i = 0; i < h; i++) {
            string s;
            cin >> s;
            for (int j = 0; j < w; j++) {
                if (s[j] == '.') {
                    room[i][j] = 1;
                } else if (s[j] == '#') {
                    room[i][j] = 0;
                } else if (s[j] == '@') {
                    room[i][j] = 1;
                    manX = j;
                    manY = i;
                }
            }
        }
        
        dfs(manX, manY);
        
        cout << num << endl;
        
        num = 0;
    }
    
    return 0;
}