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 "bits/stdc++.h"
using namespace std;

int COUNT=0;

void dfs(vector<string> map,int state[20][20],int x,int y,int W,int H){
  int dx[4]={-1,0,1,0};
  int dy[4]={0,-1,0,1};
  if(x<0 || x==H || y<0 || y==W)return; //????????????
  if(state[x][y]!=0 || map[x][y]=='#')return; //?????§????????£??????????????????????????????
  ++COUNT;
  state[x][y]=1;
  for(int i=0;i<4;++i)dfs(map,state,x+dx[i],y+dy[i],W,H); //??????????????¨??¢?´¢
}

int main(){
  int W,H,x,y;
  vector<string> map;
  string a;
  while(true){
    COUNT=0;
    cin >> W >>H;
  if(!W && !H)break;
  int state[20][20]={0}; //?????§????????£???????????????
  for(int i=0;i<H;++i){ //???????????\???
    cin >> a;
    map.push_back(a);
  }
  for(int i=0;i<H;++i){//????????????????????¢?´¢
    if(0<map[i].find('@') && map[i].find('@')<W){
      y=map[i].find('@');
      x=i;
      break;
    }
  }
    dfs(map,state,x,y,W,H);
    cout << COUNT << endl;
    map.clear();
  }
}

C++: #include <cstdio>
#include <algorithm>
using namespace std;

int W,H,map[20][20];
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};

void dfs(int x,int y) {
	map[x][y]=0;
	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&&map[nx][ny]=='.') dfs(nx,ny);
	}
}

int main() {
	int x,y;
	while(scanf("%d %d",&W,&H),W||H) {
		getchar();
		for(int j=0;j<H;j++) {
			for(int i=0;i<W;i++) {
				map[i][j]=getchar();
				if(map[i][j]=='@') {
					map[i][j]='.';
					x=i,y=j;
				}
			}
			getchar();
		}
		dfs(x,y);
		int ans=0;
		for(int j=0;j<H;j++) {
			for(int i=0;i<W;i++) {
				ans+=!map[i][j];
			}
		}
		printf("%d\n",ans);
	}
}

C++: #if 0
08-162015?????????????????°???????????????????????????
pair???
#endif

#include <iostream>
#include <queue>
using namespace std;

int w,h;
int tot=1; //?????£????????????(?????????????????????)
const int dx[]={1,0,-1,0}, dy[]={0,-1,0,1}; //??????????§??????¨
char P[30][30]; //??§?¨?????????????????????¨???

bool valid(int x,int y){ //??????????????????
	return (x>=0)&&(x<w)&&(y>=0)&&(y<h)&&(P[x][y]!='#');
}

void bfs(int x,int y){ //???????????¢?´¢
	queue<pair<int,int> > q;
	pair<int,int> p;
	p = make_pair(x,y); //pair??????????????¨??????????????????
	q.push(p);
	while(!q.empty()){
		pair<int,int> cur=q.front(); //?????¨??°?????????????????????
		q.pop();
		for(int i=0;i<4;++i){
			if(valid(cur.first+dx[i],cur.second+dy[i])){
				q.push(make_pair(cur.first+dx[i],cur.second+dy[i]));
				tot++;
				P[cur.first+dx[i]][cur.second+dy[i]]='#'; //???????????£?????´???????°???????
			}
		}
	}
}

int main(){
	int x1,y1;
	while((cin>>w>>h)&&(w!=0)){
		for(int j=0;j<h;++j){
			for(int i=0;i<w;++i){
				cin>>P[i][j];
				if(P[i][j]=='@')
					x1=i; y1=j; 
			}
		}
		bfs(x1,y1); //??????????????°????????£??\????????¢?´¢????§?
	}
	cout << tot <<endl;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>

#define rep(i, n) for(int i = 0; i < n; i ++)
#define COUNT(i, n) for(int i = 1; i <= n; i ++)
#define ALL(T) T.begin(), T.end()
#define mp make_pair
#define pb push_back
#define ITER(c) __typeof((c).begin())
#define each(c, it) for(ITER(c) it = (c).begin(); it != (c).end(); it++)
#define X first
#define Y second

using namespace std;

typedef pair<int, int> pii;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<string> vs;

const int INF = 1 << 24;
const int N = 20;

int w, h, cnt;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
vs m(N);

void dfs(pii);

int main(void){
    for(; cin >> w >> h, w; cout << cnt << endl){
        cnt = 0;
        pii p;

        rep(y, h){
            cin >> m[y];
            rep(x, w) if(m[y][x] == '@') m[y][x] = '.', p = mp(x, y);
        }
        
        dfs(p);
    }

    return 0;
}

void dfs(pii p){
    if(p.X < 0 || w <= p.X ||
       p.Y < 0 || h <= p.Y || m[p.Y][p.X] == '#') return;

    cnt++;
    m[p.Y][p.X] = '#';
    
    rep(i, 4) dfs(mp(p.X + dx[i], p.Y + dy[i]));
}

C++: #include<iostream>
#include<cstdio>
#include<string.h>
#define rep(i,n) for(int i =0;i<n;i++)
using namespace std;

string t[21];
int w,h;

void dfs(int x,int y){
int dx[4] = {1,-1, 0, 0};
int dy[4] = {0, 0, 1, -1};


	if(((x<0||y<0)||x>=h||y>=w)||t[x][y]!='.') return;
	t[x][y]='@';	
	rep(k,4){
	dfs(x+dx[k],y+dy[k]);
}
return;
}


int main(void){
	int sx=-1,sy=-1,cnt;
	
while(true){
scanf("%d %d/n",&w,&h);
if(!w&&!h) {break;}
rep(i,h){
cin >> t[i];
}

rep(i,h)rep(j,w){

	if(t[i][j]=='@'){	
	sx =i;
	sy =j;

t[i][j]='.';
	}

}

dfs(sx,sy);

cnt =0;

rep(i,h)rep(j,w)if(t[i][j]=='@') cnt++;	

cout<<cnt<<endl;;


}

return 0;
}