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 <bits/stdc++.h>
using namespace std;

#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define abs(a) max((a),-(a))
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define repe(i,n) rep(i,(n)+1)
#define per(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define pere(i,n) rep(i,(n)+1)
#define all(x) (x).begin(),(x).end()
#define SP <<" "<<
#define RET return 0
#define MOD 1000000007
#define INF 1000000000000000000

typedef long long LL;
typedef long double LD;

int main(){
  while(1){
    int h,w;
    cin >> w >> h;
    if(w==0) return 0;
    vector<vector<int>> field(h,vector<int>(w));
    vector<vector<bool>> searched(h,vector<bool>(w,false));
    char c;
    int sx,sy;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
        cin >> c;
        if(c=='.'){
          field[i][j]=0;
        }else if(c=='#'){
          field[i][j]=1;
        }else{
          sx=j;
          sy=i;
          field[i][j]=0;
        }
      }
    }
    searched[sy][sx]=true;
    queue<pair<int,int>> bfs;
    bfs.push({sx,sy});
    int nx,ny,houkou[5]={0,1,0,-1,0};
    int count=1;
    while(!bfs.empty()){
      pair<int,int> now=bfs.front();
      bfs.pop();
      for(int i=0;i<4;i++){
        nx=now.first+houkou[i];
        ny=now.second+houkou[i+1];
        if(0<=nx&&nx<w&&0<=ny&&ny<h){
          if(!searched[ny][nx]&&field[ny][nx]==0){
            searched[ny][nx]=true;
            bfs.push({nx,ny});
            count++;
          }
        }
      }
    }
    cout << count << endl;
  }
  return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<map>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;

int h, w;
char mp[22][22];

const int nx[] = { 1, 0,-1, 0};
const int ny[] = { 0, 1, 0,-1};

int dfs(int y, int x);

int main(){
  ios_base::sync_with_stdio(false);

  int sx, sy;

  while(true){

    cin >> w >> h;

    if(w == 0 && h == 0){
      break;
    }

    for(int i=0;i<h;i++){
      cin >> mp[i];
      for(int j=0;j<w;j++){
        if(mp[i][j] == '@'){
          sy = i;
          sx = j; 
        } 
      }
    }

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

  }
  return 0;
}

int dfs(int y, int x){
  if(y >= 0 && x >= 0 && y < h && x < w && mp[y][x] != '#'){
    mp[y][x] = '#';
    int t = 0;
    for(int i=0;i<4;i++){
      t += dfs(y + ny[i], x + nx[i]);
    }
    return t+1;
  }
  return 0;
}

C++:     #include<iostream>
    #include<stdio.h>
    //#include <bits/stdc++.h>
    #include<vector>
    #include<float.h>
    #include<iomanip>
    #include<algorithm>
    #include<string>
    #include<cstring>
    #include<math.h>
    #include<cmath>
    #include<sstream>
    #include<set>
    #include<map>
    #include<queue>
    #include<cassert>
    #include<cmath>
    #include<cstdint>
using namespace std;

bool seen[55][55];
vector<string> s(55);
int w, h;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
int res;

void dfs(int y, int x){
    seen[y][x] = true;

    for(int i = 0; i < 4; i++){
        
            
            int ny = y + dy[i], nx = x + dx[i];
            
            if(ny < 0 || ny >= h || nx < 0 || nx >= w)continue;
            if(seen[ny][nx])continue;
            if(s[ny][nx] == '#' || s[ny][nx] == '@')continue;
            res++;
            dfs(ny, nx);
        
    }
}

int main(){
    while(cin >> w >> h){
      if(w == 0 && h == 0)return 0;
      res = 0;
    int sy, sx;
    for(int i = 0; i < h; i++)cin >> s[i];
    for(int i = 0; i < h; i++)for(int j = 0; j < w; j++){
        if(s[i][j] == '@')sy = i, sx = j;
        seen[i][j] = false;
    }
    dfs(sy, sx);
    cout << res + 1 << endl;
    }
}

C++: int main()
{
  int W, H, i, j, f, d, count;
  int dx[4] = {0, 1, 0, -1};
  int dy[4] = {1, 0, -1, 0};
  char map[32][32];
  char s[32];

  while (1) {
    gets(s);
    sscanf(s, "%d %d", &W, &H); if (W == 0 && H == 0) { break; }
    for (i = 0; i < 30; i++) {
      strcpy(map[i], "##############################");
    }
    
    for (i = 0; i < H; i++) {
      gets(&map[i+1][1]);
      map[i+1][W+1] = '#';
      map[i+1][W+2] = '\0';
    }

    count = 1;
    while (1) {
      f = 0;
      for (i = 1; i <= H; i++) {
        for (j = 1; j <= W; j++) {
          if (map[i][j] == '@') {
            for (d = 0; d < 4; d++) {
              if (map[i+dy[d]][j+dx[d]] == '.') {
                map[i+dy[d]][j+dx[d]] = '@';
                count++;
                f = 1;
              }
            }
          }
        }
      }
      if (!f) {
        break;
      }
    }
    printf("%d\n", count);
  }
  return 0;
}