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 <vector>
#include <cstdint>
#include <array>

using namespace std;


typedef std::array<uint32_t, 2> Idx;

uint32_t checkAndSetIdx(vector<string>& mp, vector<Idx>& stack, uint32_t i, uint32_t j)
{
    if(i > mp.size()-1 || j > mp.at(0).size()-1)
        return 0;

    if(mp[i][j] == '.'){
        mp[i][j] = 'x';
        Idx e = {i, j};
        stack.push_back(e);
        return 1;
    }

    return 0;
}


int main()
{
    while(1)
    {
        uint32_t h, w;
        cin >> w >> h;

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


        Idx iniPos;

        vector<string> mp;
        for(uint32_t i = 0; i < h; ++i){
            mp.push_back([](){string str; cin >> str; return str;}());

            for(uint32_t j = 0; j < w; ++j){
                if(mp[i][j] == '@'){
                    Idx e = {i, j};
                    iniPos = e;
                    mp[i][j] = 'x';
                }
            }
        }


        vector<Idx> stack;
        stack.push_back(iniPos);
        uint32_t cnt = 1;


        while(stack.size() != 0){
            auto nowPos = stack.at(stack.size() - 1);
            stack.pop_back();
            const auto i = nowPos[0],
                       j = nowPos[1];

            cnt += checkAndSetIdx(mp, stack, i+1, j);
            cnt += checkAndSetIdx(mp, stack, i-1, j);
            cnt += checkAndSetIdx(mp, stack, i, j+1);
            cnt += checkAndSetIdx(mp, stack, i, j-1);
        }

        cout << cnt << endl;
    }

    return 0;
}

C++: #include<functional>
#include<algorithm>
#include<iostream>
#include<utility>
#include<string>
#include<vector>
#include<cmath>
#include<stack>
#include<queue>
#include<deque>
#include<map>
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,x,n) for(int i=x;i<n;i++)
#define all(v) (v).begin(),(v).end()
#define pf push_front
#define pb push_back
#define int long long
using namespace std;
const int INF=1000000007;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

int w,h,sx,sy,sum;
char mp[21][21];

int dfs(int y,int x){
  if(0<=y&&y<h&&0<=x&&x<w&&(mp[y][x]=='.'||mp[y][x]=='@')){
    mp[y][x]='$';
    sum++;
    rep(i,4){
      dfs(y+dy[i],x+dx[i]);
    }
  }
  return sum;
}

signed main(){
 
  while(cin>>w>>h,w){
    rep(i,h)rep(j,w){
      cin>>mp[i][j];
      if(mp[i][j]=='@'){
        sy=i;sx=j;
      }
    }
    sum=0;
    cout<<dfs(sy,sx)<<endl;
  }
  return 0;
}

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

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

int W, H, ans;
char maze[20][20];

bool check(int x, int y){
	return x >= 0 && x < W && y >= 0 && y < H && maze[y][x] != '#';
}

void dfs(int x, int y){
	
	for(int i = 0; i < 4; ++i){
		int nx = x + dx[i], ny = y + dy[i];
		if(check(nx, ny)){
			maze[ny][nx] = '#';
			ans++;
			dfs(nx, ny);
		}
	}
}

int main() {
	while(cin >> W >> H && (W || H)){
		for(int i = 0; i < H; ++i)
			for(int j = 0; j < W; ++j)
				cin >> maze[i][j];
		
		ans = 0;
		for(int i = 0; i < H; ++i){
			for(int j = 0; j < W; ++j){
				if(maze[i][j] == '@'){
					dfs(j, i);
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <queue>
using namespace std;

#define MP make_pair
typedef pair<int,int> pii;

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

int main(){
    int W,H;
    while(cin>>W>>H){
        if(W==0&&H==0) return 0;

        vector<string> t(H);
        vector<vector<bool>> used(H,vector<bool>(W,false));
        for(int i=0;i<H;i++) cin>>t[i];

        int sx,sy;
        for(int i=0;i<H;i++) for(int j=0;j<W;j++) if(t[i][j]=='@'){
            sx = i;
            sy = j;
        }
        used[sx][sy]=true;

        queue<pii> que;
        que.push(make_pair(sx,sy));

        while(que.size()!=0){
            pii p = que.front();
            que.pop();

            int x = p.first;
            int y = p.second;

            for(int i=0;i<4;i++){
                int nx = x + dx[i];
                int ny = y + dy[i];
                if(nx>=0 && nx<H && ny>=0 && ny<W && t[nx][ny]!='#' && used[nx][ny]==false){
                    used[nx][ny] = true;
                    que.push(MP(nx,ny));
                }
            }
        }

        int cnt = 0;
        for(int i=0;i<H;i++) for(int j=0;j<W;j++) if(used[i][j]==true) cnt++;
        cout << cnt << endl;
    }
}