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 mod 1000000007
#define all(c) begin(c),end(c)
template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
using ll = long long;
using vi = vector<int>; using vs = vector<string>;
int dd[] = { 0, 1, 0, -1, 0 }; //→↓←↑

bool B[25][25]; int N, M;
void dfs(int ty, int tx, vs S)
{
	for (int r = 0; r < 4; r++)
	{
		int y = ty + dd[r];
		int x = tx + dd[r + 1];
		if (y < 0 || y >= N || x < 0 || x >= M)
		{
			continue;
		}
		if (S[y][x] == '.' && B[y][x] == false)
		{
			B[y][x] = true;
			dfs(y, x, S);
		}
	}
}

void solve()
{
	while (true)
	{
		cin >> M >> N;
		if (N == 0 && M == 0)
		{
			break;
		}

		vs S(N);
		for (int i = 0; i < N; i++)
		{
			cin >> S[i];
		}
		int sy = -1, sx = -1;
		for (int i = 0; i < N; i++)
		{
			for (int j = 0; j < M; j++)
			{
				if (S[i][j] == '@')
				{
					sy = i;
					sx = j;
					break;
				}
				B[i][j] = false;
			}
		}

		dfs(sy, sx, S);
		int ans = 0;
		for (int i = 0; i < N; i++)
		{
			for (int j = 0; j < M; j++)
			{
				if (B[i][j])
				{
					ans++;
				}
			}
		}
		cout << (ans+1) << endl;
	}

}

int main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	solve();
	return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>
#include <numeric>
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

const long long MAX = 5100000;
const long long INF = 1LL << 60;
const long long mod = 1000000007LL;
//const long long mod = 998244353LL;

using namespace std;
typedef unsigned long long ull;
typedef long long ll;

int dh[4] = { 1,0,-1,0 };
int dw[4] = { 0,1,0,-1 };
int main()
{
	/*
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	*/
	while (1) {
		int H, W; scanf("%d %d", &W, &H);
		if (H == 0 && W == 0) break;
		vector<string> vs(H); for (ll i = 0; i < H; i++) cin >> vs[i];
		int sh, sw;
		for (int i = 0; i < H; i++) {
			for (int j = 0; j < W; j++) {
				if (vs[i][j] == '@') {
					sh = i;
					sw = j;
				}
			}
		}
		queue<pair<int, int>> q;
		q.emplace(sh, sw);
		vs[sh][sw] = '#';
		int res = 1;
		while (!q.empty()) {
			int h, w; tie(h, w) = q.front();
			q.pop();

			for (int i = 0; i < 4; i++) {
				int nh = h + dh[i];
				int nw = w + dw[i];
				if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue;
				if (vs[nh][nw] == '#') continue;
				res++;
				vs[nh][nw] = '#';
				q.emplace(nh, nw);
			}
		}
		printf("%d\n", res);
	}
	return 0;
}

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

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

int a,b,ans;
int q[20+5][20+5];
char p[20+5][20+5];

using namespace std;
int main() {
	queue<pair<int,int> > M;
	pair<int,int> X;
	while(cin >> a >> b){
		for (int i=1; i<=21; i++){
			for (int j=1; j<=21; j++){
				q[i][j] = 0;
				p[i][j] = ' ';
			}
		}
		if (a==0) return 0;
		ans = 0;
		for (int i=1; i<=b; i++){
			for (int j=1; j<=a; j++){
				cin >> p[i][j];
				if (p[i][j] == '@'){
					M.push(make_pair(i,j));
					ans = 1;
					q[i][j] = 1;
				}
			}
		}
	

		while(!M.empty()){
			a = M.front().first;
			b = M.front().second;
			M.pop();
			for (int i=0; i<=3; i++){		
				if (p[a+dx[i]][b+dy[i]]=='.' && q[a+dx[i]][b+dy[i]]==0){
					M.push(make_pair(a+dx[i],b+dy[i]));
					q[a+dx[i]][b+dy[i]] = 1;
					ans+=1;
				}
			}
		}
		cout << ans << endl;
	}
}