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 <iostream>
using namespace std;

static const int margin = 1;
static const int board_size = 32;

char board[board_size][board_size];
int W, H;
int ans_accume;

void init() {
	for(int i = 0; i < board_size; i++) {
		for(int j = 0; j < board_size; j++) {
			board[i][j] = '#';
		}
	}
	ans_accume = 0;
}

bool input() {
	cin >> W >> H;
	if(!W && !H)
		return false;
	for(int i = margin; i < H + margin; i++) {
		cin >> board[i] + margin;
	}
	return true;
}

// (p.second, p.first)
void solve(pair<int, int> p) {
	if(board[p.first][p.second + 1] == '.') {
		board[p.first][p.second + 1] = 'x';
		ans_accume++;
		solve(pair<int, int>(p.first, p.second + 1));
	}
	if(board[p.first][p.second - 1] == '.') {
		board[p.first][p.second - 1] = 'x';
		ans_accume++;
		solve(pair<int, int>(p.first, p.second - 1));
	}
	if(board[p.first + 1][p.second] == '.') {
		board[p.first + 1][p.second] = 'x';
		ans_accume++;
		solve(pair<int, int>(p.first + 1, p.second));
	}
	if(board[p.first - 1][p.second] == '.') {
		board[p.first - 1][p.second] = 'x';
		ans_accume++;
		solve(pair<int, int>(p.first - 1, p.second));
	}
}

pair<int, int> search_start() {
	for(int i = margin; i < H + margin; i++) {
		for(int j = margin; j < W + margin; j++) {
			if(board[i][j] == '@') {
				ans_accume++;
				return pair<int, int>(i, j);
			}
		}
	}

	return pair<int, int>(-1, -1);
}

int main(void) {
	while(init(), input()) {
		solve(search_start());
		cout << ans_accume << endl;
	}
}

C++: #include "bits/stdc++.h"

using namespace std;

#define rep(i,n) for(int i=0;i<(n);i++)

int main(){
    int w,h;
    while(cin>>w>>h){
        if(w==0 && h==0)break;
        int sx,sy;
        int sum=0;
        int dy[]={-1,0,1,0};
        int dx[]={0,-1,0,1};
    
    
        string data[30]={};
    
        for(int i=0;i<h;i++){
            cin>>data[i];
        }
    
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(data[i][j]=='@'){
                    sx=j;sy=i;
                    break;
                }
            }
        }
    
        bool visited[30][30]={};
//        rep(i,30)rep(j,30)visited[i][j]=false;
        
    
        queue<pair<int,int> > que;
    
        que.push( pair<int,int>(sy,sx) );
        visited[sy][sx]=true;
    
        while(que.size()){
            int qff=que.front().first,qfs=que.front().second;
            sum++;
            rep(i,4){
                if( qff+dy[i]<h && qff+dy[i]>=0 && qfs+dx[i]<w && qfs>=0 && visited[ qff+dy[i] ][ qfs+dx[i] ]==false && data[ qff+dy[i] ][ qfs+dx[i] ]!='#' ){
                    que.push( pair<int,int>(qff+dy[i] , qfs+dx[i]) );
                    visited[ qff+dy[i] ][ qfs+dx[i] ]=true;
                }
            }
            
            que.pop();
        }
        cout<<sum<<endl;
    }
    
    return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cctype>
#include <climits>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <list>
#include <queue>
#include <deque>
#include <algorithm>
#include <numeric>
#include <utility>
#include <complex>
#include <memory>
#include <functional>
using namespace std;
#define ALL(g) (g).begin(),(g).end()
#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i,n) REP(i,0,n)
#define F(i,j,k) fill(i[0],i[0]+j*j,k)
#define P(p) cout<<(p)<<endl;
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define INF 1<<25
#define pb push_back
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<double> vd;
typedef pair<int,int> pii;
typedef pair<long,long> pll;
typedef long long LL;
typedef long long int LLI;

int memo[20][20];

int mx[] = {0,0,-1,1};
int my[] = {-1,1,0,0};

char table[21][21];

int dfs(int x,int y);
int W=0,H=0;

int main(void)
{
    while(1){
    int i,j;
    int x,y;
    int ans=0;
    cin>> W >> H;
    
    if(W==0 && H==0)break;
    
    REP(i,0,H){
        REP(j,0,W){
            cin >> table[i][j];
            if(table[i][j] == '@'){
                x=i;
                y=j;
            }
        }
    }
    ans = dfs(x,y);
    cout << ans << endl;
    }
    return 0;
}

int dfs(int x,int y){
    int cnt=1;
    for(int i=0;i<4;i++){
        int nx = x+mx[i],ny = y+my[i];
        if(0 <= nx && 0 <= ny && ny < W && nx < H && table[nx][ny] == '.'){
            table[nx][ny] = '#';
            cnt += dfs(nx,ny);
        }
    }
    return cnt;
}