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<queue>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
using namespace std;

class place{
public:
	int x;
	int y;
};

int main(){
	int W,H;
	while(1){
		cin>>W>>H;
		if(W==0 && H==0)break;
		bool dt[H][W];
		int startx,starty;
		rep(i,H){
			rep(j,W){
				char c;
				cin>>c;
				if(c=='.'){
					dt[i][j]=false;
				}else if(c=='#'){
					dt[i][j]=true;
				}else{
					startx=j;
					starty=i;
					dt[i][j]=false;
				}
			}
		}
		queue<place> que;
		place p;
		p.x=startx;
		p.y=starty;
		que.push(p);
		int cnt=0;
		int dirx[4]={1,0,-1,0};
		int diry[4]={0,1,0,-1};
		while(!que.empty()){
			p=que.front();
			que.pop();
			if(!dt[p.y][p.x])cnt++;
			dt[p.y][p.x]=true;
			rep(k,4){
				int nx=p.x+dirx[k];
				int ny=p.y+diry[k];
				if(nx>=0 && ny>=0 && nx<W && ny<H && !dt[ny][nx]){
					place pp;
					pp.x=nx;
					pp.y=ny;
					que.push(pp);
				}
			}
		}
		cout<<cnt<<endl;
	}
	return 0;
}

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

int w,h;
int mcnt=0;
char maps[22][22];
int dy[]={-1,0,1,0};
int dx[]={0,-1,0,1};

void bfs(int y,int x,int nowcnt){
  mcnt++;
  maps[y][x]='#';
  for(int i=0;i<4;i++){
    int ny=y+dy[i];
    int nx=x+dx[i];
    if(ny>=0 && ny<h && nx>=0 && nx<w && maps[ny][nx]!='#' ){
      bfs(ny,nx,nowcnt);
      //    maps[ny][nx]='.';
    }
  }
  return;
}
int main(){
  int sy,sx;
  while(1){
    mcnt=0;
    cin>>w>>h;
    if(!(w || h ))break;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	cin>>maps[i][j];
	if(maps[i][j]=='@'){
	  sy=i;
	sx=j;
	}
      }
    }
    bfs(sy,sx,0);
    cout<<mcnt<<endl;
  }
}

C++: // 2005à\IB Red and Black

#include <iostream>
#include <string>
#include <queue>

using namespace std;

int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};

int main(){
	int w, h;
	string b[20];
	while(cin >> w >> h, w){
		for(int i=0;i<h;i++) cin >> b[i];
		int sx, sy;
		for(int i=0;i<h;i++)
			for(int j=0;j<w;j++)
				if(b[i][j]=='@') sx = i, sy = j;
		queue< pair<int, int> > qu; qu.push(make_pair(sx,sy));
		int res = 0;
		while(!qu.empty()){
			pair<int, int> pr = qu.front(); qu.pop();
			int x = pr.first, y = pr.second;
			res++;
			for(int i=0;i<4;i++){
				int nx = x+dx[i], ny = y+dy[i];
				if(nx<0||h<=nx||ny<0||w<=ny||b[nx][ny]!='.') continue;
				b[nx][ny] = '#';
				qu.push(make_pair(nx,ny));
			}
		}
		cout << res << endl;
	}
}

C++: #include <cstdio>
#include <cstring>
#include <iostream>

int dxy[]={1,0,-1,0,1};
int ans=0;

char tile[21][21];
int H,W;

void dfs(int x, int y){
	if(tile[x][y]=='.'){
		ans++;
		tile[x][y]='#';
	}
	for(int i = 0; i < 4; ++i){
		int nx = dxy[i] + x, ny = y + dxy[i + 1];
		if(nx < H && nx >= 0 && ny >= 0 && ny < W){
			if(tile[nx][ny] == '.')dfs(nx, ny);
		}
	}
}

int main(){
	while(std::cin>>W>>H && W+H){
		ans = 0;
		memset(tile, '#', sizeof(tile));
		int sx,sy;
		for(int i = 0; i < H; ++i){
			for(int j = 0; j < W; ++j){
				char c;
				std::cin>>c;
				tile[i][j]=c;
				if(tile[i][j] == '@')sx = i, sy = j;
			}
		}
		tile[sx][sy] = '.';
		dfs(sx,sy);
		printf("%d\n",ans);
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
char tile[20][21];
bool vis[20][20];
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
int W,H;
int dfs(int x,int y) {
    int sum=1;
    vis[y][x]=1;
    for(int i=0;i<4;i++) {
        int nx=x+dx[i],ny=y+dy[i];
        if(0>nx||nx>=W||0>ny||ny>=H||tile[ny][nx]=='#'||vis[ny][nx]) continue;
        sum+=dfs(nx,ny);
    }
    return sum;
}
int main() {
    while(scanf("%d %d",&W,&H),W) {
        for(int i=0;i<H;i++) scanf("%s",tile[i]);
        int sx,sy;
        for(int i=0;i<H;i++) {
            for(int j=0;j<W;j++) if(tile[i][j]=='@') {sx=j;sy=i;tile[i][j]='.';}
        }
        memset(vis,0,sizeof(vis));
        printf("%d\n",dfs(sx,sy));
    }
}

C++: #include<bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define REP(i,n) for(int i=0;i<(int)(n);i++)

using namespace std;
bool check[22][22];
char c[22][22];
int w,h;

int recursion(pair<int,int> pos){
  if(c[pos.first][pos.second]=='#'){return 0;}
  if(check[pos.first][pos.second]){return 0;}
  check[pos.first][pos.second]=true;
  int count=1;
  if(pos.first>0){
    count+=recursion(make_pair(pos.first-1,pos.second));
  }
  if(pos.second>0){
    count+=recursion(make_pair(pos.first,pos.second-1));
  }
  if(pos.first<w-1){
    count+=recursion(make_pair(pos.first+1,pos.second));
  }
  if(pos.second<h-1){
    count+=recursion(make_pair(pos.first,pos.second+1));
  }
  return count;
}

int main(){ _;
  pair<int,int> pos;
  while(cin>>w>>h,!(w==0&&h==0)){
    REP(y,h)REP(x,w){
      cin>>c[x][y];
      check[x][y]=false;
      if(c[x][y]=='@')
	pos=make_pair(x,y);
    }
    cout<<recursion(pos)<<endl;
  }
}