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++: #define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <math.h>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <math.h>
#include <iostream>
#include <random>
#include<map>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <cassert>
#include<fstream>
#include <unordered_map>
#include <cstdlib>
#include <complex>
using namespace std;
#define Ma_PI 3.141592653589793
#define eps 0.00000001
#define LONG_INF 1e9
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007
#define MOD 998244353
#define REP(i,n) for(long long i = 0;i < n;++i)    
#define seg_size 524288
long long gcd(long long a, long long b) {
	if (b == 0) return a;
	return gcd(b, a % b);
}
int main() {
	while (true) {
		int w, h;
		cin >> w >> h;
		if (w == 0) return 0;
		vector<string> s;
		int xe[4] = { 1,-1,0,0 };
		int ye[4] = { 0,0,1,-1 };
		queue<pair<int, int>> next;
		int visited[21][21] = {};
		REP(i, h) {
			string a;
			cin >> a;
			s.push_back(a);
			REP(q, a.length()) {
				if (a[q] == '@') {
					next.push(make_pair(i, q));
					visited[i][q] = 1;
				}
			}
		}
		int ans = 1;
		while (next.empty() == false) {
			pair<int, int> now = next.front();
			next.pop();
			REP(q, 4) {
				int now_x = now.first + xe[q];
				int now_y = now.second + ye[q];
				if (now_x >= 0 && now_x < h && now_y >= 0 && now_y < w) {
					if (visited[now_x][now_y] == 0 && s[now_x][now_y] != '#') {
						visited[now_x][now_y] = 1;
						next.push(make_pair(now_x, now_y));
						ans++;
					}
				}
			}
		}
		cout << ans << endl;
	}
}

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

int moved = 0;
int w, h;
char myMap[20][20];

bool safe(int x, int y){
    return x < h && y < w;
}

void move(int x, int y){
    myMap[x][y] = '#';
    moved++;
    if (myMap[x+1][y] == '.' && safe(x+1, y)) move(x+1, y);
    if (myMap[x-1][y] == '.' && safe(x-1, y)) move(x-1, y);
    if (myMap[x][y+1] == '.' && safe(x, y+1)) move(x, y+1);
    if (myMap[x][y-1] == '.' && safe(x, y-1)) move(x, y-1);
    return;
}

int main(){
    while (1){
	cin >> w >> h;
	if (w == 0 && h == 0) break;

	int initX, initY;
	
	for (int i = 0; i < h; i++){
	    for (int j = 0; j < w; j++){
		cin >> myMap[i][j];
		if (myMap[i][j] == '@'){
		    initX = i; initY = j;
		}
	    }
	}
	
	move(initX, initY);
	
	cout << moved << endl;
    }
}

C++: #include <stdio.h>
#include <iostream>
#include <algorithm>
#include <functional>
#include <cstdlib>
#include <sstream>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <complex>
#include <vector>
#include <tuple>
#include <cstdio>
#include <math.h>
#include <cmath>
#include <time.h>
#define all(c) ((c).begin(),(c).end())
#define rall(c) (c).rbegin(),(c).rend()
#define sort(v,n) sort(v,v+n)
#define vsort(v) sort(v.begin(),v.end())
#define vvsort(v) sort(v.begin(),v.end(),greater<int>())
#define ll long long
#define pb(a) push_back(a)
#define fi first
#define se second
#define inf 999999999
using namespace std;
const ll MOD = 1e9 + 7;
const double PI = acos(-1.0);
//---------------------------------------------------------------------------------------------//
int w, h;
char a[21][21];

int bfs(int sh,int sw) {
	//cout<<"--------------------------------------------"<<endl;
	int ans = 1;
	queue<pair<int,int> > q;
	q.push(pair<int,int>(sh, sw));
	while (!q.empty())
	{
		//for (int i = 0; i < h; i++) {
			//for (int j = 0; j < w; j++) {
				//cout << a[i][j] << " ";
			//}
			//cout << endl;
		//}

		//cout << ans << endl;
		int nowx, nowy;
		nowy = q.front().first;
		nowx = q.front().second;
		//cout << nowy << " " << nowx << endl;
		q.pop();
		if (a[nowy][nowx + 1] == '.' && nowx<w-1) {
			q.push(pair<int,int>(nowy, nowx + 1));
			a[nowy][nowx + 1] = '#';
			ans++;
		}
		if (a[nowy][nowx - 1]=='.' && nowx>0) {
			q.push(pair<int,int>(nowy, nowx - 1));
			a[nowy][nowx - 1] = '#';
			ans++;
		}
		if (a[nowy + 1][nowx] == '.' && nowy<h-1) {
			q.push(pair<int,int>(nowy + 1, nowx));
			a[nowy + 1][nowx] = '#';
			ans++;
		}
		if (a[nowy - 1][nowx] == '.' && nowy>0) {
			q.push(pair<int,int>(nowy - 1, nowx));
			a[nowy - 1][nowx] = '#';
			ans++;
		}
	}
	return ans;
}

int main() {
	while (cin >> w >> h, w) {
		int sw, sh;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				cin >> a[i][j];
				if (a[i][j] == '@') {
					sw = j;
					sh = i;
				}
			}
		}
		cout << bfs(sh, sw) << endl;
	}
}