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 <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <stack>
#include <queue>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
static const double EPS = 1e-5;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
bool arrive[25][25];
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};
void dfs(vector<string> &grid, int x, int y){
  REP(r,4){
    if(arrive[y+dy[r]][x+dx[r]]==false&&grid[y+dy[r]][x+dx[r]]=='.'){
      arrive[y+dy[r]][x+dx[r]] = true;
      dfs(grid,x+dx[r],y+dy[r]);
    }
  }
}
int main(void){
  int W,H;
  while(cin>>W>>H,W){
    memset(arrive,0,sizeof(arrive));
    vector<string> grid;
    string tmp;
    REP(i,W+2)tmp+="#";
    grid.push_back(tmp);
    REP(i,H){
      string tmp;
      cin>>tmp;
      tmp = "#"+tmp+"#";
      grid.push_back(tmp);
    }
    grid.push_back(tmp);
    REP(y,grid.size())REP(x,grid[0].size())if(grid[y][x]=='@')dfs(grid,x,y);
    int ans=1;
    REP(y,25)REP(x,25)if(arrive[y][x])ans++;
    cout<<ans<<endl;
  }
  return 0;
}

C++: #include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <functional>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <deque>
#include <ctime>

using namespace std;
  
#define rep(i,n) REP(i,0,n)
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define pb push_back
#define mp make_pair
#define all(r) (r).begin(),(r).end()
#define rall(r) (r).rbegin(),(r).rend()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 
const int INF = 1e9;
const ll LINF = 1e18;
const ll MOD = 1e9 + 7;
double EPS = 1e-8;
const double PI = acos(-1);

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

bool check(int x, int y, int h, int w){
	return x < 0 || x >= w || y < 0 || y >= h;
}

int main(){
	int w, h;
	while(cin>>w>>h && w > 0 && h > 0){
		vector<string> v(h);
		rep(i, h) cin>>v[i];
		pii s;
		rep(i, h) rep(j, w) if(v[i][j] == '@') s.fi = i, s.se = j;
		int ans = 0;
		set<pii> used;
		used.insert(s);
		ans++;
		queue<pii> q;
		q.push(s);
		while(!q.empty()){
			pii p = q.front();
			q.pop();

			int x = p.se, y = p.fi;
			rep(i, 4){
				int nx = x + dx[i], ny = y + dy[i];
				if(check(nx, ny, h, w) || used.count({ny, nx}) || v[ny][nx] == '#') continue;
				used.insert({ny, nx});
				q.push({ny, nx});
				ans++;
			}
		}

		cout<<ans<<endl;

	}
}

C++: #include<iostream>
#include<queue>
using namespace std;
int main(){
	int w, h; 
	while (cin >> w >> h) {
		if (!w && !h)return 0;
		char map[21][21];
		bool b[21][21];
		queue<pair<int, int>>bfs;
		for (int i = 0; i < h; i++)for (int j = 0; j < w; j++) {
			cin >> map[i][j];
			b[i][j] = false;
			if (map[i][j] == '@') {
				bfs.push(make_pair(i, j));
				b[i][j] = true;
			}
		}
		int ans = 1;
		while (bfs.size() > 0) {
			pair<int, int>p = bfs.front();
			int x = p.first;
			int y = p.second;
			map[x][y] = '#';
			if (x > 0){
				if (map[x - 1][y] == '.' && !b[x - 1][y]) {
					bfs.push(make_pair(x - 1, y));
					ans++;
					b[x - 1][y] = true;
				}
			}
			if (x < h) {
				if (map[x + 1][y] == '.' && !b[x + 1][y]) {
					bfs.push(make_pair(x + 1, y));
					ans++;
					b[x + 1][y] = true;
				}
			}
			if (y > 0) {
				if (map[x][y - 1] == '.'&& !b[x][y - 1]) {
					bfs.push(make_pair(x, y - 1));
					ans++;
					b[x][y - 1] = true;
				}
			}
			if (y < w) {
				if (map[x][y + 1] == '.' && !b[x][y + 1]) {
					bfs.push(make_pair(x, y + 1));
					ans++;
					b[x][y + 1] = true;
				}
			}
			bfs.pop();
		}
		cout << ans << endl;
	}
}