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;

#define min(a, b) ((a)<(b)? (a):(b))
#define max(a, b) ((a)>(b)? (a):(b))
#define rep(i, n) REP(i, 0, (n))
#define REP(i, a, n) for(typeof(n) i=(a); i<(n); i++)

int H, W;
char t[30][30];
bool visited[30][30];

int dfs(int i, int j, int cnt) {
    if(i < 0 || j < 0 || i >= H || j >= W) return 0;
    if(visited[i][j]) return 0;
    if(t[i][j] == '#') return 0;
    
    visited[i][j] = true;
    
    return 1 + dfs(i+1, j, cnt) + dfs(i-1, j, cnt) + dfs(i, j+1, cnt) + dfs(i, j-1, cnt);
}

int main() {
	while(true) {
        cin >> W >> H;
        if(W == 0 && H == 0) break;
        
        int p, q;
        rep(i, H) {
            rep(j, W) {
                cin >> t[i][j];
                visited[i][j] = false;
                if(t[i][j] == '@') {p = i; q = j;}
            }
        }
        cout << dfs(p, q, 0) << endl;
	}
}

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

int w, h, black;
char tile[24][24];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

void dfs(int x, int y){
    black++;
    tile[x][y] = '#';
    for(int i = 0; i < 4; i++){
        if( x+dx[i] >= 0 && x+dx[i] < h &&
           y+dy[i] >= 0 && y+dy[i] < w &&
           tile[ x+dx[i] ][ y+dy[i] ] == '.' ){
            dfs( x+dx[i], y+dy[i] );
        }
    }
}

int main(){
    int i, j;
    while( cin >> w >> h, w||h ){
        for(i=0;i<w;i++){
            for(j = 0; j < h; j++){
                cin >> tile[i][j];
            }
        }
        black = 0;
        for(i = 0; i < h && !black; i++){
            for(j = 0; j < w && !black; j++){
                if( tile[i][j] == '@' ){
                    dfs(i, j);
                }
            }
        }
        cout << black << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
using namespace std;

bool map[20][20];//true??????
int W,H,X,Y;
int ct;
const int dx[] = {1,0,-1,0};
const int dy[] = {0,1,0,-1};

bool valid(int x,int y) {
	return 0 <= x && x < W && 0 <= y && y < H && map[x][y];
}

void dfs(int x,int y) {
	map[x][y] = false;
	ct++;
	for(int i = 0;i < 4;i++) {
		if(valid(x + dx[i],y + dy[i])) dfs(x + dx[i],y + dy[i]);
	}
}

int main() {
	while(cin >> W >> H && !(W == 0 && H == 0)) {
		ct = 0;
		for(int y = 0;y < H;y++) {
			char c;
			for(int x = 0;x < W;x++) {
				cin >> c;
				map[x][y] = !(c == '#');
				if(c == '@') {
					X = x;
					Y = y;
				}
			}
		}
		dfs(X,Y);
		cout << ct << endl;
	}
}

C++: #include <iostream>
#include <queue>
#include <array>

const int kSize = 20 + 2;

std::array<int, 4> ways = {-1, -kSize, +1, +kSize};

inline int xy2pos(const int x, const int y){
    return x + y * kSize;
}

std::array<bool, kSize*kSize> Bfs(const std::array<bool, kSize*kSize> &tiles, int pos){
    std::array<bool, kSize*kSize> arrived;
    std::queue<int> q;
    q.push(pos);
    std::fill(arrived.begin(), arrived.end(), false);
    while(!q.empty()){
        pos = q.front();
        q.pop();
        arrived[pos] = true;
        for(int way : ways)
            if(tiles.at(pos + way) && !arrived.at(pos + way))
                q.push(pos + way);
    }
    return arrived;
}

int main(){
    int w,h;
    std::array<bool,kSize*kSize> tiles; ///< true if black
    while((std::cin >> w >> h) && w != 0){
        std::fill(tiles.begin(), tiles.end(), false);
        int pos;
        for(int y=1; y<=h; ++y){
            for(int x=1; x<=w; ++x){
                char c;
                std::cin >> c;
                tiles[xy2pos(x,y)] = (c == '.');
                if(c == '@'){
                    pos = xy2pos(x,y);
                    tiles[pos] = true;
                }
            }
        }
        auto arrived = Bfs(tiles, pos);
        int sum = 0;
        for(bool v : arrived)
            sum += (v) ? 1 : 0;
        std::cout << sum << std::endl;
    }
}

C++: #include <bits/stdc++.h>
using namespace std;

using ll=long long;
#define int ll

#define FOR(i,a,b) for(int i=int(a);i<int(b);i++)
#define REP(i,b) FOR(i,0,b)

int W,H;
char data[30][30];
bool come[30][30];
int human_h,human_w;

int read(){
	int i;
	scanf("%lld",&i);
	return i;
}

int canGo(int w,int h){
	if(w < 0 || h < 0 || w >= W || h >= H) return 0;
	if(data[h][w] == '#') return 0;
	if(come[h][w]) return 0;
	come[h][w] = true;
	int ret = 1;
	ret += canGo(w+1,h);
	ret += canGo(w-1,h);
	ret += canGo(w,h+1);
	ret += canGo(w,h-1);
	return ret;
}

signed main(){
	// your code goes here
	while(true){
		W=read(),H=read();
		if(W==0 || H==0) break;
		REP(i,H){
			REP(j,W){
				come[i][j] = false;
				cin >> data[i][j];
				if(data[i][j] == '@'){
					human_h = i;
					human_w = j;
				}
			}
		}
		cout <<	canGo(human_w,human_h) << endl;
	}

}