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>
//#define int long long
//#define _DEBUG
#include <queue>
#include <vector>
#include <string>
#include <utility>
using namespace std;
typedef pair<int,int> pos;
int const infty = -1, none = -9, dx[] = {1,-1,0,0}, dy[]={0,0,1,-1};
int W, H;
string Map[23]; //Map[i][j]:i行j列目
bool reached[23][23];
pos firstPos;
queue<pos> Q;
int count_reached(){
    int r = 0;
    for(int i=0; i<H; i++){
        for(int j=0; j<W; j++){
            if(reached[i][j]) r++;
        }
    }
    return r;
}
bool valid(int x, int y){
    return x>=0 && x<W && y>=0 && y<H && Map[y][x]!='#';
}
int solve(){
    Q.push(firstPos);
    reached[firstPos.second][firstPos.first] = true;
    while(true){
        auto top = Q.front(); Q.pop();
        int x = top.first, y = top.second;
        for(int d=0; d<4; d++){
            if(valid(x+dx[d], y+dy[d]) && !reached[y+dy[d]][x+dx[d]]){
                Q.push(make_pair(x+dx[d], y+dy[d]));
                reached[y+dy[d]][x+dx[d]] = true;
            }
        }
        if(Q.empty()) break;
        //cout << x<<","<<y<<", "<< Q.size()<<endl;
    }
    return count_reached();
}
signed main(){    
    while(cin >> W >> H && W > 0){
        firstPos = make_pair(none, none);
        for(int i=0; i<H; i++){
            cin >> Map[i];
            for(int j=0; j<W; j++){
                if(Map[i][j] == '@') firstPos = make_pair(j, i);
                reached[i][j] = false;
            }
        }
        cout << solve() << endl;
#ifdef _DEBUG
        cout << firstPos.first<<firstPos.second<< endl;
        for(int i=0; i<H; i++){
            for(int j=0; j<W; j++)cout << Map[i][j];
            cout << endl;
        }
        for(int i=0; i<H; i++){
            for(int j=0; j<W; j++){
                cout<<reached[i][j];
            }
            cout << endl;
        }
#endif
    }
}

C++: #include <cstdio>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

#define TEMP_T template<typename T>
TEMP_T void sort(T& v){ sort(v.begin(), v.end()); }
TEMP_T void revs(T& v){ reverse(v.begin(), v.end()); }
TEMP_T void uniq(T& v){ sort(v); v.erase(unique(v.begin(), v.end())); }
TEMP_T T    cums(T& v){ T r; r.push_back(v[0]); for(int i=1; i<v.size(); i++) r.push_back(r[i-1] + v[i]); return r; }
TEMP_T void show(T& v, char delim=' ', char end='\n'){ for(int i=0; i<v.size()-1; i++) cout << v[i] << delim; cout << v[v.size()-1] << end; }

static inline int in(){ int x; scanf("%d", &x); return x; }

struct UF {
  vector<int> data;
  UF(int size) : data(size, -1) {}
  void merge(int x, int y){
    x = root(x), y = root(y);
    if (x != y){
      if (data[x] > data[y]) swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
  }
  int root(int x){
    return data[x] < 0 ? x : data[x] = root(data[x]);
  }
  int size(int x){
    return -data[root(x)];
  }
};

struct Solver {
  int w, h;
  vector<string> field;
  Solver(int w, int h) : w(w), h(h) {}
  int read_input(){
    for (int i = 0; i < h; i++){
      string s;
      cin >> s;
      field.push_back(s);
    }
    return (0);
  }

  int solve(){
    UF uf(w * h);
    int pos;
    for (int y = 0; y < h; y++){
      for (int x = 0; x < w; x++){
        if (field[y][x] == '#') continue;
        for (int d = 0; d < 4; d++){
          static const int dx[] = {0, 0, 1, -1};
          static const int dy[] = {1, -1, 0, 0};
          int nx = x + dx[d];
          int ny = y + dy[d];
          if (nx < 0 || nx >= w || ny < 0 || ny >= h) continue;
          if (field[ny][nx] != '#') uf.merge(y * w + x, ny * w + nx);
        }
        if (field[y][x] == '@') pos = y * w + x;
      }
    }
    return uf.size(pos);
  }
};

int main()
{
  int w, h;
  while (w = in(), h = in()){
    Solver s(w, h);
    s.read_input();
    printf("%d\n", s.solve());
  }

  return (0);
}