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;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using msi = map<string, int>;
using mii = map<int, int>;
using psi = pair<string, int>;
using pii = pair<int, int>;
using vlai = valarray<int>;
#define rep(i,n) for(int i=0;i<n;i++)
#define range(i,s,n) for(int i=s;i<n;i++)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define fs first
#define sc second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define INF 1E9
#define EPS 1E-9
#define MOD (ll)(1E9+7)
#define PI 3.1415926535897932384

template <class T>ostream &operator<<(std::ostream &o, const vector<T> &v)
{
	rep(i, v.size()) {
		o << (i > 0 ? " " : "") << v[i];
	}
	return o;
}



int main() {
	int w, h;
	int dx[4] = { -1,0,1,0 };
	int dy[4] = { 0,-1,0,1 };
	char input;
	while (1) {
		pair<int, int> start;
		cin >> w >> h;
		if (w == 0) break;
		vector<vector<int>> field(h + 2, vector<int>(w + 2, 1));
		for (int i = 1; i < h + 1; i++) {
			for (int j = 1; j < w + 1; j++) {
				cin >> input;
				if (input == '.') {
					field[i][j] = 0;
				}
				else if (input == '@') {
					start.first = i;
					start.second = j;
					field[i][j] = 0;
				}
			}
		}
		/*
		rep(i, h + 2) {
			rep(j, w + 2) {
				cout << field[i][j] << " " ;
			}
			cout << endl;
		}
		*/

		queue<pair<int, int>> que;
		int ans = 0;
		que.emplace(start.first, start.second);
		while (!que.empty()) {
			pair<int, int> now = que.front();
			que.pop();
			if (field[now.first][now.second] == 1) continue;
			ans++;
			field[now.first][now.second] = 1;
			rep(k, 4) {
				//cout << now.first + dy[k] << " " << now.second + dx[k] << endl;
				//cout << field[now.first + dy[k]][now.second + dx[k]] << endl;
				
				if (field[now.first + dy[k]][now.second + dx[k]] == 0) {
					que.emplace(now.first + dy[k], now.second + dx[k]);
				}
				
			}
		}
		
		cout << ans << endl;
	}
}

C++: 
#define B(a,x,b) ((a)<=(x)&&(x)<(b))

char T[999];
int x,y,cx,cy,i,c;

void R(int a,int b){
	if(B(0,a,x)&&B(0,b,y)&&T[b*20+a]){
		T[b*20+a]=0;i++;R(a-1,b);R(a+1,b);R(a,b-1);R(a,b+1);
	}
}

main(){
	for(;scanf("%d%d",&x,&y),x;){
		for(i=0;i<x*y;i++){
			int _x=i%x, _y=i/x;
			for(;(c=getchar())!='\n';);
			switch(c){
				case '@': cx=_x;cy=_y;
				case '.': T[_y*20+_x]=1; break;
				case '#': T[_y*20+_x]=0; break;
			}
		}
		i=0;
		R(cx,cy);
		printf("%d\n",i);
	}
	return 0;
}

C++: #include <stdio.h>
#include <iostream>
#include <string>

using namespace std;
void checkPiece(int x, int y);//, char map[][]);
void check(int x, int y);
char map[22][22];
int flag = 1;

int main(){
	int W, H;
	while(1){
		int count = 0;
		int x, y;
		cin>>W>>H;
		if(W == 0 && H == 0) { return 0; }
		//char map[H+2][W+2];
		
		for (int i = 0; i < H + 2; ++i)
		{
			for (int j = 0; j < W + 2; ++j)
			{
				map[j][i] = '#';
			}
		}

		for (int i = 1; i < H + 1; ++i)
		{
			for (int j = 1; j < W + 1; ++j)
			{
				cin>>map[j][i];
				if(map[j][i]=='@'){x = j; y = i;}
			}
		}

		//cout<<map[2][1]<<y<<endl;
		check(x, y);
	while(flag){
		flag = 0;
		for (int i = 1; i < H + 1; ++i)
		{
			for (int j = 1; j < W + 1; ++j)
			{
				//cout<<i<<endl;
				check(j, i);
			}
		}
		//flag = 0;
		//cout<<flag<<endl;
	}
/*
	for (int i = 0; i < H + 2; ++i)
		{
			for (int j = 0; j < W + 2; ++j)
			{
				cout<<map[j][i];
			}
			cout<<endl;
		}
*/
		for (int i = 0; i < H + 2; ++i)
		{
			for (int j = 0; j < W + 2; ++j)
			{
				if(map[j][i] == '@'){ count++; }
			}
		}
		cout<<count<<endl;

	}
	return 0;

}

void checkPiece(int x, int y){//, char map[][]){
	if(map[x][y] == '.') { map[x][y] = '@'; }
	if(map[x][y] == '@'){
		if(map[x+1][y] != '#') return checkPiece(x+1, y);
		else if(map[x-1][y] != '#') return checkPiece(x-1, y);
		else if(map[x][y+1] != '#') return checkPiece(x, y+1);
		else if(map[x][y-1] != '#') return checkPiece(x, y-1);
	}
	return;
}

void check(int x, int y){
	if(map[x][y] == '@'){
		if(map[x+1][y] != '#' && map[x+1][y] == '.') { map[x+1][y] = '@'; flag = 1;}
		if(map[x-1][y] != '#' && map[x-1][y] == '.') { map[x-1][y] = '@'; flag = 1;}
		if(map[x][y+1] != '#' && map[x][y+1] == '.') { map[x][y+1] = '@'; flag = 1;}
		if(map[x][y-1] != '#' && map[x][y-1] == '.') { map[x][y-1] = '@'; flag = 1;}
	}
	return;
}