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 int long long
#define DBG 0
#define dump(o) if(DBG){cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) if(DBG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define each(it,c) for(auto it=(c).begin();it!=(c).end();it++)
#define all(c) c.begin(),c.end()
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);

char A[22][22] = {};
int f[22][22] = {};
int W, H;

bool isinrange(int i, int j) {
	return 0 <= i&&i < H && 0 <= j&&j < W;
}

int di[] = { 1,0,-1,0 };
int dj[] = { 0,1,0,-1 };
void dfs(int i, int j) {
	if (isinrange(i, j) == false)return;
	if (A[i][j] == '#')return;
	if (f[i][j])return;
	f[i][j] = true;
	rep(k, 0, 4) {
		int ii = i + di[k];
		int jj = j + dj[k];
		dfs(ii, jj);
	}
}

signed main() {
	for (; cin >> W >> H&&W&&H;) {
		memset(f, 0, sizeof(f));
		memset(A, 0, sizeof(A));
		int ai, aj;
		rep(i, 0, H) rep(j, 0, W) {
			cin >> A[i][j];
			if (A[i][j] == '@') {
				ai = i, aj = j;
			}
		}
		dfs(ai, aj);
		int ans = 0;
		rep(i, 0, H)rep(j, 0, W) {
			if (f[i][j]) {
				ans++;
				dump(i);
				dump(j);
			}
		}
		cout << ans << endl;
		rep(i, 0, H)dumpc(f[i]);
	}
	return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cassert>
#include <iostream>
#include <cctype>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <utility>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <bitset>
#include <complex>
#include <fstream>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
template<class T> T RoundOff(T a){ return int(a+.5-(a<0)); }
template<class T, class C> void chmax(T& a, C b){ if(a < b) a = b; }
template<class T, class C> void chmin(T& a, C b){ if(b < a) a = b; }
template<class T, class C> pair<T, C> mp(T a, C b){ return make_pair(a, b); }

struct P{ P(int x, int y) : x(x), y(y) {} double x, y; };

int main()
{
    int w, h;
    while(cin >> w >> h && w)
    {
        vector<vector<char> > m(h, vector<char>(w));
        P s(0, 0);
        
        rep(y, h) rep(x, w)
        {
            cin >> m[y][x];
            if(m[y][x] == '@')
                s = P(x, y);
        }
        
        int res = 1;
        queue<P> que;
        for(que.push(s); !que.empty(); que.pop())
        {
            P p = que.front();
            
            int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
            rep(i, 4)
            {
                int nx = p.x + dx[i], ny = p.y + dy[i];
                
                if(0 <= nx && nx < w && 0 <= ny && ny < h && m[ny][nx] == '.')
                {
                    m[ny][nx] = '#';
                    res++;
                    
                    que.push(P(nx, ny));
                }
            }
        }
        
        cout << res << endl;
    }
}

C++: #include <iostream>
#include <queue>
#include <cstdio>	
#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

typedef pair<int, int> P;
char tile[25][25];

int main()
{
	const int dir[][2] = { {-1, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 } };
	int w, h;
	while (scanf ("%d %d", &w, &h ) && w && h ){
		P s = P (-1, -1 );
		rep (i, h ){
			rep (j, w ){
				scanf (" %c", tile[i]+j );
				if (tile[i][j] == '@' ){
					s.first = i, s.second = j;
				} // end if
			} // end rep
		} // end rep
		queue<P> que;
		que.push (s );
		while (!que.empty() ){
			P curr = que.front(); que.pop();
			int row = curr.first;
			int col = curr.second;
			rep (k, 4 ){
				int nr = row + dir[k][0];
				int nc = col + dir[k][1];
				if (nr < 0 || nc < 0 || nr >= h || nc >= w || tile[nr][nc] == '#' || tile[nr][nc] == '@' ) continue;
				que.push (P (nr, nc ) );
				tile[nr][nc] = '@';
			} // end rep
		} // end while
		int res = 0;
		rep (i, h ){
			rep (j, w ){
				if (tile[i][j] == '@' ) res++;
			} // end rep
		} // end rep
		printf ("%d\n", res );
	} // end while

	return 0;
}