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;
int w,h;
int tx[4] = {0,-1,0,1};
int ty[4] = {-1,0,1,0};
char map[21][21];

int slv(int x,int y){
    map[y][x]='#';
    int ans=0;
    for(int i=0;i<4;++i)
        if( x+tx[i]!=-1 || y+ty[i]!=-1 || x+tx[i]!=w || y+ty[i]!=h)
            if( map[y+ty[i]][x+tx[i]] == '.' )
                ans+=slv( x+tx[i], y+ty[i] ) + 1 ;
    return ans;
}

int main()
{
    int ix,iy;
    while(1){
        cin >> w >> h;
        if(w==0&&h==0)break;
        for(int i=0;i<h;++i)
            for(int j=0;j<w;++j){
                cin >> map[i][j];
                if(map[i][j]=='@'){ix=j;iy=i;}
            }
        cout << slv( ix , iy ) + 1 << endl;
    }
    return 0;
}

C++: #include<bits/stdc++.h>
#define LL long long
#define REP(i,n) for(int i=0;i<(n);++i)
#define REPA(i,n) for(int i=1;i<(n);++i)
#define foreach(i, n) for(auto &i:(n))
#define PII pair<int,int>
#define PLI pair<long long, int>
#define PLL pair<long long, long long>
#define PDD pair<double, double>
#define MOD ((int)1e9 + 7)
#define INF ((int)1e9)
#define INFLL ((LL)1e18)
#define ALL(x) (x).begin(),(x).end()
#define ctoi(x) (x - 'a') 
#define CTOI(x) (x - 'A')
#define BIT(x) (1 << (x))
using namespace std;

LL modinv(LL a){
    LL b = MOD, u = 1, v = 0;
    while(b){
        LL t = a / b;
        a -= t * b;swap(a, b);
        u -= t * v;swap(u, v);
    }
    u%=MOD;
    if(u < 0) u += MOD;
    return u;

}

LL exp(LL a, LL b){
    LL sum = a;
    LL res = 1;
    while(b){
        if(b&1){
            res = (res * sum)%MOD;
        }
        sum = (sum * sum) % MOD;
        b>>=1;
    }
    return res;
}

int N, M;
vector<string> tail;
int mx[]={1, 0, -1, 0}, my[]={0, 1, 0, -1};

int main(){
    while(cin>>N>>M&&N){
        tail.assign(M, "");
        foreach(i, tail)cin>>i;
        int fx, fy;
        REP(i, M){
            REP(j, N){
                if(tail[i][j]=='@'){
                    fy=i;
                    fx=j;
                    tail[i][j]='#';
                    break;
                }
            }
        }
        int ans = 1;
        queue<PII> que;
        que.push(PII(fy, fx));
        while(!que.empty()){
            PII d = que.front();
            que.pop();
            REP(i, 4){
                int ny = d.first + my[i];
                int nx = d.second + mx[i];
                if(nx==-1||ny==-1||nx==N||ny==M||tail[ny][nx]=='#')continue;
                ++ans;
                tail[ny][nx]='#';
                que.push(PII(ny, nx));
            }
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int w,h;
char ma[30][30];
int b[30][30];
int cnt;
int dfs(int sy,int sx){
    for(int i=0;i<4;i++){
        int ny=sy+dy[i],nx=sx+dx[i];
        if(ny < 0 || ny > h - 1 || nx < 0 || nx > w - 1 || b[ny][nx]==1 || ma[ny][nx]=='#'){continue;}
        b[ny][nx]=1;
        cnt++;
        dfs(ny,nx);
    }
    return cnt;
}
int main(){
    while(1){
        cin>>w>>h;
        if(w==0 && h==0){
            break;
        }
        int sx,sy;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                cin>>ma[i][j];
                if(ma[i][j]=='@'){
                    sy=i;
                    sx=j;
                }
            }
        }
        cnt=1;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                b[i][j]=0;
            }
        }
        b[sy][sx]=1;
        cout<<dfs(sy,sx)<<endl;
    }
    return 0;
}

C++: #include<iostream>
using namespace std;
int W,H;
int t[20][20];
int c;
char ch;

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

bool inField(int y,int x){
  if( 0<=y && y<H && 0<=x && x<W ){
    return true;
  }else{
    return false;
  }
}

void visit(int y,int x){
  c++;
  t[y][x]=1;
  for(int i=0;i<4;i++){
    int ny=y+dy[i];
    int nx=x+dx[i];
    if( inField(ny,nx) && t[ny][nx]==0){
      visit(ny,nx);
    }
  }
}
int main(){
  while(1){
    cin>>W>>H;
    if(W==0&&H==0)break;
    int sy,sx;
    for(int i=0;i<H;i++){
      for(int j=0;j<W;j++){
        cin>>ch;
        if(ch=='.'){
          t[i][j]=0;
        }
        if(ch=='@'){
          sy=i;
          sx=j;
        }
        if(ch=='#'){
          t[i][j]=-1;
        }
      }
    }
    c=0;
    visit(sy,sx);
    cout<<c<<endl;
  }
  return 0; 
}