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 <algorithm>
#include <functional>
#include<vector>
#include<math.h>
#include<bitset>
#include<string>
#include <deque>
#include<queue>
#include<map>
#include<type_traits>
using namespace std;
char D[28][28];
int Y = 0;
void serch(int X, int Q) {
	if (D[X][Q] == '.'||D[X][Q]=='@') {
		Y++;
		D[X][Q] = '#';
		serch(X - 1, Q);
		serch(X + 1, Q);
		serch(X , Q-1);
		serch(X , Q+1);
		

	}
	else return;
}


int main() {
	int H, W;
	cin >> W >> H;
	for (int i = 0; i <28; i++) {
		for (int j = 0; j <28; j++) {
			D[i][j] = '#';
		}
	}
	int Sx,Sy;
	while (H) {
		 Y = 0;
		for (int i = 1; i <= H; i++) {
			for (int j = 1; j <= W; j++) {
				cin >> D[j][i];
				if (D[j][i] == '@') {
					Sx = j;
					Sy = i;
				}
			}
		}
		serch(Sx, Sy);
		
		cout <<"Y"<< Y << endl;
		for (int i = 0; i <28; i++) {
			for (int j = 0; j <28; j++) {
				D[i][j] = '#';
			}
		}
		cin >> W >> H;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <math.h>

#define REP(i, n) for (int (i) = 0; (i) < (n); (i)++)

using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
template<class T> using hset = unordered_set<T>;
template<class Key, class T> using hmap = unordered_map<Key, T>;

int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
bool isOutOfBounds(int y, int x, int h, int w) {
    return y < 0 || y >= h || x < 0 || x >= w;
}

int main() {
    int W, H;
    vs room;

    int sy, sx;
    while (cin >> W >> H) {
        if (W == 0) break;

        room.resize(H);
        REP(y, H) {
            cin >> room[y];
            REP(x, W) {
                if (room[y][x] == '@') { sy = y; sx = x; room[y][x] = '#'; }
            }
        }

        queue<int> que;
        que.push(sy);
        que.push(sx);
        int y, x, cnt=1;
        while (!que.empty()) {
            y = que.front(); que.pop();
            x = que.front(); que.pop();

            for (int i=0; i<4; i++) {
                if (isOutOfBounds(y+dy[i], x+dx[i], H, W)) continue;
                if (room[y+dy[i]][x+dx[i]] == '#') continue;

                cnt++;
                room[y+dy[i]][x+dx[i]] = '#';
                que.push(y+dy[i]);
                que.push(x+dx[i]);
            }
        }

        cout << cnt << endl;
    }

    return 0;
}

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

using std::cin;
using std::cout;
using std::cerr;
using std::endl;

const int vx[4] = { 0, 1, 0, -1 }, vy[4] = { 1, 0, -1, 0 };

int W, H;
std::string s[21];
bool is_searched[21][21];

int solve() {
  int sx, sy;

  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      if (s[i][j] == '@') {
        sx = i, sy = j;
        goto L;
      }
    }
  }
L:

  std::memset(is_searched, false, sizeof is_searched);

  int ans = 1;

  std::queue<std::pair<int, int> > que;
  que.push({ sx, sy });
  is_searched[sx][sy] = true;

  while (!que.empty()) {
    std::pair<int, int> p = que.front();
    que.pop();

    for (int i = 0; i < 4; ++i) {
      int nx = p.first + vx[i], ny = p.second + vy[i];

      if (0 <= nx && nx < H && 0 <= ny && ny < W && !is_searched[nx][ny] && s[nx][ny] == '.') {
        ans++;
        is_searched[nx][ny] = true;
        que.push({ nx, ny });
      }
    }
  }

  return ans;
}

int main() {
  while (cin >> W >> H, H | W) {
    for (int i = 0; i < H; ++i) {
      cin >> s[i];
    }

    int ans = solve();

    cout << ans << endl;
  }

  return 0;
}

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

using namespace std;
typedef pair<int, int> P;

char t[20][20];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int ans,h,w;
queue<P> q;

int bfs(int x, int y){

  q.push( P(x, y) );
  
  while( !q.empty() ){
    P now = q.front();
    q.pop();
    for( int i = 0 ; i < 4 ; i++ ){
      int nx = now.first + dx[i];
      int ny = now.second + dy[i];
      if( 0 <= nx && nx < w && 0 <= ny && ny <= h 
	  && t[ny][nx] == '.' ){
	t[ny][nx] = '#';
	q.push( P(nx, ny) );
	ans++;
      }
    }
  }
  return ans;
}

int main(void){

  int x,y;
  while( cin >> w >> h, h + w ){

    memset( t, 0, sizeof(t) );
    ans = 1;

    for( int i = 0 ; i < h ; i++ ){
      for( int j = 0 ; j < w ; j++ ){
	cin >> t[i][j];
	if( t[i][j] == '@' ){
	  x = j;
	  y = i;
	}
      }
    }
    cout << bfs(x,y) << endl;
  }
  return 0;
}