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<algorithm>

using namespace std;
int W,H;
void solve(int,int);
char dfs[20][20];
int d_x[]={1,0,-1,0},d_y[]={0,1,0,-1};

int main(){
	
	while(1){
		int ans=0;
		cin>>W>>H;
		if(W==0&&H==0)	break;

		int dx,dy;
		for(int i=0;i<H;i++){
			for(int j=0;j<W;j++){
				cin>>dfs[j][i];
				if(dfs[j][i]=='@'){
					dx=j;
					dy=i;
				}
			}
		}
		solve(dx,dy);
		for(int i=0;i<H;i++)
			for(int j=0;j<W;j++)
				if(dfs[j][i]=='T')
					ans++;
		cout<<ans<<endl;
		memset(dfs,0,sizeof(dfs));
	}
}

void solve(int x,int y){
	dfs[x][y]='T';
	
	for(int j=0;j<4;j++){
		int mx=x+d_x[j],my=y+d_y[j];
		if(mx<0||my<0||mx>W||my>H)	continue;
		if(dfs[mx][my]=='.')
			solve(mx,my);
	}
	return ;
}

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

const int MAXH = 30;
const int MAXW = 30;
int W, H;
char G[MAXH][MAXW];

int dfs(int x, int y) {
  const static int dx[] = {1,0,-1,0};
  const static int dy[] = {0,1,0,-1};
  G[y][x] = '#';
  int res = 1;
  for(int k = 0; k < 4; ++k) {
    int nx = x + dx[k];
    int ny = y + dy[k];
    if(nx < 0 || nx >= W) continue;
    if(ny < 0 || ny >= H) continue;
    if(G[ny][nx] == '#') continue;
    res += dfs(nx, ny);
  }
  return res;
}

int main() {
  for(; cin >> W >> H && (W|H);) {
    int sx, sy;
    for(int i = 0; i < H; ++i) {
      for(int j = 0; j < W; ++j) {
        cin >> G[i][j];
        if(G[i][j] == '@') {
          sx = j;
          sy = i;
        }
      }
    }
    cout << dfs(sx, sy) << endl;
  }
  return 0;
}

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

int count;
int w,h;
int x,y;
char map[20][20];

void tannkenn(int x,int y);

int main()
{
	cin>>w>>h;
	count=0;
	for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                cin >> map[i][j];
				if(map[i][j]=='@'){
					x=i;
					y=j;
				}
			}
	}

	tannkenn(x,y);
	cout<<count<<endl;

	return 0;
}

void tannkenn(int x,int y)
{
	count++;
	map[x][y]='#';

	if(x+1<h && map[x+1][y]=='.')
		tannkenn(x+1,y);
	if(y+1<w && map[x][y+1]=='.')
		tannkenn(x,y+1);
	if(x-1>=0 && map[x-1][y]=='.')
		tannkenn(x-1,y);
	if(y-1>=0 && map[x][y-1]=='.')
		tannkenn(x,y-1);
}

C++: #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
typedef long long ll;
int r[20][20],ans,w,h;
char C[20][20];
int search(int x,int y){
    if(x<0||x>=w||y<0||y>=h||C[y][x]=='#'||r[y][x]) return 0;
    r[y][x]=1;
    ans++;
    search(x+1,y);search(x-1,y);search(x,y+1);search(x,y-1);
}
int main(){
    while(true){
        cin>>w>>h;
        if(w==0) break;
        int sx,sy;
        rep(i,h)rep(j,w){
            cin>>C[i][j];
            if(C[i][j]=='@') sx=j,sy=i;
        }
        rep(i,h)rep(j,w) r[i][j]=0;
        ans=0;
        search(sx,sy);
        cout<<ans<<endl;
    }
}

C++: #include<iostream>
using namespace std;
typedef pair<int, int> pii;
pii dxdy[]={{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
char fld[21][21];
int used[20][20];
int W, H;

int rec(int i, int j){

   if(fld[i][j]=='#') return 0;
   if(used[i][j]) return 0;
   used[i][j]=1;
   int ret=0;
   for(pii dd: dxdy){
      int ni=i+dd.first;
      int nj=j+dd.second;
      if(!(0<=ni&&ni<H&&0<=nj&&nj<W)) continue;
      ret+=rec(ni, nj);
   }

   return ret+1;
}

int main(){

   while(1){
      cin>> W>> H;
      if(!(H||W)) break;
      int sx, sy;
      for(int i=0; i<H; i++){
         for(int j=0; j<W; j++){
            cin>> fld[i][j];
            if(fld[i][j]=='@'){
               sy=i;
               sx=j;
               fld[i][j]='.';
            }
         }
      }

      for(int i=0; i<20; i++)for(int j=0; j<20; j++) used[i][j]=0;
      cout<< rec(sy, sx)<< endl;
   }

   return 0;
}

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

vector<string> f;
void init(){
  f.clear();
}

int input(){
  int w,h;
  cin>>w>>h;
  if(w==0&&h==0)return false;
  string s;
  for(int i=0;i<h;i++){
    cin>>s;
    f.push_back(s);
  }
  return true;
}

int vx[] = {0,0,1,-1};
int vy[] = {1,-1,0,0};

int rec(int x,int y){
  if(f[x][y]!='.')return 0;
  f[x][y]='#';
  int res = 1;
  for(int i=0;i<4;i++){
    res += rec(x+vx[i],y+vy[i]);
  }
  return res;
}

int solve(){
  int x,y;
  for(int i=0;i<f.size();i++){
    for(int j=0;j<f[i].size();j++){
      if(f[i][j]=='@'){
        x = i;
        y = j;
        f[i][j] = '.';
      }
    }
  }
  return rec(x,y);
}

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