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>
#include <vector>
#include <string>
#include <utility>
#include <queue>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
#include <climits>
//#include "h.h"
using namespace std;

#define ll long long
#define pint pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define mp make_pair
#define pm(first, second) pb(mp(first, second))
#define SPACE " "
#define fpf first.first
#define fps first.second
#define spf second.first
#define sps second.second

#define all(X) (X).begin(), (X).end()

int main(){

	while(true){
		int W, H;
		cin >> W >> H;
		if(W == 0 && H == 0)break;
		vector<string> S(H);
		queue<pair<int, int>> Q;
		for(int i = 0; i < H; i ++){
			cin >> S[i];
			for(int j = 0; j < W; j ++){
				if(S[i][j] == '@')Q.push(mp(i, j));
			}
		}
		pair<int, int> P;
		while(!Q.empty()){
			P = Q.front();
			Q.pop();
			S[P.first][P.second] = '@';
			if(P.first > 0 && S[P.first - 1][P.second] == '.')Q.push(mp(P.first - 1, P.second));
			if(P.first < H - 1 && S[P.first + 1][P.second] == '.')Q.push(mp(P.first + 1, P.second));
			if(P.second > 0 && S[P.first][P.second - 1] == '.')Q.push(mp(P.first, P.second - 1));
			if(P.second < W - 1 && S[P.first][P.second + 1] == '.')Q.push(mp(P.first, P.second + 1));
		}
		int ans = 0;
		for(int i = 0; i < H; i ++){
			for(int j = 0; j < W; j ++){
				if(S[i][j] == '@')ans ++;
			}
		}
		cout << ans << endl;
	}

	return 0;
}

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

const int W = 20, H = 20;
char board[H][W];
int w, h;

int dfs(int x, int y){
	int dx[] = {0, -1, 1, 0}, dy[] = {-1, 0, 0, 1};
	int c = 1;
	board[y][x] = '#';
	for(int i = 0;i < 4;++i){
		int tx = x + dx[i], ty = y + dy[i];
		if(0 <= tx && 0 <= ty && tx < w && ty < h && board[ty][tx] == '.'){
			c += dfs(tx, ty);
		}
	}
	return c;
}

int main(){
	int sx, sy;
	while(cin >> w >> h, w){
		for(int i = 0;i < h;++i){
			for(int j = 0;j < w;++j){
				cin >> board[i][j];
				if(board[i][j] == '@'){
					sx = j;
					sy = i;
				}
			}
		}
		cout << dfs(sx, sy) << endl;
//		for(int i = 0;i < h;++i){
//			for(int j = 0;j < w;++j){
//				cout << board[i][j];
//			}
//			cout << endl;
//		}
	}
	return 0;
}

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

int main(){
	while(1){
		int W, H, x[30][30] = {};
		char str[30][30];
		cin>>W>>H;
		if(W==0 && H==0) break;
		for(int i = 0; i < H; i++){
			for(int j = 0; j < W; j++){
				cin>>str[i][j];
				if(str[i][j] == '@') x[i][j] = 1;
			}
		}
	
		int q = 0;
		while(1){
			int z = 0;
			for(int i = 0; i < H; i++){
				for(int j = 0; j < W; j++){
					if(x[i][j] == 1){
						if(i > 0 && str[i - 1][j] == '.') x[i - 1][j] = 1;
						if(i < H - 1  && str[i + 1][j] == '.') x[i + 1][j] = 1;
						if(j > 0 && str[i][j - 1] == '.') x[i][j - 1] = 1;
						if(j < W - 1&& str[i][j + 1] == '.') x[i][j + 1] = 1;
					}
				}
			}
			for(int i = 0; i < H; i++){
				for(int j = 0; j < W; j++){
					if(x[i][j] == 1){
						z++;
					}
				}
			}
			if(z == q) break;
			q = z;
		}

		
		cout<<q<<endl;
	}
	return 0;
}

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

int ans,w,h;
string g[30];
bool visit[30][30];

int rec(int y,int x){
  int res = 1;
  int dx[] = {0,1,0,-1}, dy[] = {-1,0,1,0};
  visit[y][x] = true;

  for(int k=0;k<4;k++){
    int sy = y+dy[k] ,sx = x+dx[k];
    if(sy<0 || sx<0 || h<=sy || w<=sx)continue;
    if(visit[sy][sx] || g[sy][sx] == '#')continue;
    res += rec(sy,sx);
  }
  return res;
}

int main(){
  while(cin >> w >> h && (w||h)){

    for(int i=0;i<h;i++)for(int j=0;j<w;j++)visit[i][j] = false;
    for(int i=0;i<h;i++)cin >> g[i];

    ans = 0;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	if(g[i][j] == '@')cout << rec(i,j) << endl;
      }
    }
  }
}

C++: #include <iostream>
#include <queue>
using namespace std;
typedef pair<int,int> P;
int W,H,sx,sy;
int dx[4] = {-1,1,0,0},dy[4] = {0,0,-1,1};
char F[21][21];
int visited[21][21] = {};

bool in(int x,int y){
    return 1<=x && x<=H && 1<=y && y<=W;
}

int main(){
    while(cin >> W >> H && W>0){
        queue<P> Q;
        for(int i=1;i<=H;i++){
            for(int j=1;j<=W;j++){
                visited[i][j] = 0;
                cin >> F[i][j];
                if(F[i][j]=='@'){
                    visited[i][j] = 1;
                    Q.push({i,j});
                }
            }
        }
        int ans = 0;
        while(!Q.empty()){
            ans++;
            int x = Q.front().first,y = Q.front().second; Q.pop();
            for(int i=0;i<4;i++){
                int nx = x+dx[i],ny = y+dy[i];
                if(in(nx,ny) && F[nx][ny]!='#' && visited[nx][ny]==0){
                    visited[nx][ny] = 1;
                    Q.push({nx,ny});
                }
            }
        }
        cout << ans << endl;
    }
}