Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

var (
	sc = bufio.NewScanner(os.Stdin)
)

type Color int

const (
	ColorBlack Color = iota
	ColorRed
)

type Node struct {
	ID    int
	X, Y  int
	Color Color
	Nexts []*Node
}

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])

		// Initialize map
		m := map[int]map[int]*Node{}
		for x := 1; x <= w; x++ {
			m[x] = map[int]*Node{}
			for y := 1; y <= h; y++ {
				m[x][y] = &Node{ID: x + y*w, X: x, Y: y}
			}
		}
		for x := 1; x <= w; x++ {
			for y := 1; y <= h; y++ {
				nexts := []*Node{}
				if n, ok := m[x-1][y]; ok {
					nexts = append(nexts, n)
				}
				if n, ok := m[x][y-1]; ok {
					nexts = append(nexts, n)
				}
				if n, ok := m[x+1][y]; ok {
					nexts = append(nexts, n)
				}
				if n, ok := m[x][y+1]; ok {
					nexts = append(nexts, n)
				}
				m[x][y].Nexts = nexts
			}
		}

		// Set colors
		queue := []*Node{}
		touchedIDs := map[int]struct{}{}
		for y := 1; y <= h; y++ {
			sc.Scan()
			for i, c := range sc.Text() {
				switch c {
				case '.', '@':
					n := m[i+1][y]
					n.Color = ColorBlack
					if c == '@' {
						queue = append(queue, n)
						touchedIDs[n.ID] = struct{}{}
					}
				case '#':
					m[i+1][y].Color = ColorRed
				}
			}
		}

		// Search
		visitedIDs := map[int]struct{}{}
		for {
			if len(queue) == 0 {
				break
			}
			n := queue[0]
			queue = queue[1:]
			visitedIDs[n.ID] = struct{}{}
			for _, next := range n.Nexts {
				if next.Color == ColorRed {
					continue
				}
				if _, ok := touchedIDs[next.ID]; !ok {
					queue = append(queue, next)
					touchedIDs[n.ID] = struct{}{}
				}
			}
		}
		fmt.Println(len(visitedIDs))
	}
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <bitset>
#include <numeric>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <stack>
#include <tuple>

using namespace std;

#define int long long

#define rep(i,n) for(int i = 0; i<n ; i++)
#define FOR(i,a,n) for(int i=a; i<n; i++)
#define REPSTR(i,s) for(int i=0; (s)[i]; i++)
#define REPITR(i,s) for(auto i=(s).begin(); i!=(s).end();i++)

#define RANGE(a,i,b) ((a)<=(i)&&(i)<=(b))

#define pb push_back
#define mp make_pair

#define ALL(a) begin(a), end(a)

#define EXIST(s,x) ((s).find(x)!=(s).end())

#define SET(a,c) memset(a,c,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))

#ifdef _DEBUG
#define DEBUG(x) cerr<<#x<<":"<<x<<endl
#else
#define DEBUG(x)
#endif

#define PRINTJOIN(x,n) rep(i,n){if(i)cout<<" ";cout<<x[i];}cout<<endl 
#ifdef int
#define INF 1001001001001001001LL
#else
#define INF 1001001001
#endif

typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef stringstream SS;

#define PQ priority_queue

#define FST first
#define SEC second

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

int dfs (int i, int j, const vector<vector<bool>> & field, vector<vector<bool>> & done) {
    if(!field[i][j]) {
        return 0;
    }
    if(done[i][j]) {
        return 0;
    }
    int ans=0;
    done[i][j]=true;
    rep(k,4){
        if(dx[k]+i<0||dx[k]+i>=h||dy[k]+j<0||dy[k]+j>=w) continue;
        ans+=dfs(dx[k]+i, dy[k]+j, field, done);
    }

    return ans+1;
}


signed main(void) {
	cin.tie(0);
	ios::sync_with_stdio(false);
    while(cin>>w>>h,w||h){
        vector<vector<bool>> field(h,vector<bool>(w,false));
        vector<vector<bool>> done(h,vector<bool>(w,false));
        int si, sj;
        rep(i,h)rep(j,w){
            char c;
            cin>>c;
            field[i][j] = c=='#'?false:true;
            if(c == '@') {
                si=i;
                sj=j;
            }
        }

        cout<<dfs(si, sj, field, done)<<endl;
    }

	return 0;
}

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];


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


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


int main(void){

	int w,h,sx,sy,cnt;
	rep(i,20)rep(j,20){
t[i][j]=='#';	
	}

while(true){
scanf("%d %d",&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(sy,sx);
cnt =0;

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

cout <<endl<<cnt;;


}

return 0;
}

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

string t[21];


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


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


int main(void){

	int w,h,sx,sy,cnt;
	rep(i,20)rep(j,21){
t[i][j]=='#';	
	}

while(true){
scanf("%d %d",&w,&h);
if(!w&&!h) return 0;
for(int i=1;i<=h;i++)
{
cin >> t[i];
}	
rep(i,h)rep(j,w){

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

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

}
dfs(sy,sx);
cnt =0;

rep(i,h){
	cout << t[i]<<endl;
	rep(j,w)if(t[i][j]=='@') cnt++;	
}

cout <<cnt<< endl;;


}



return 0;
}

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

#define rep(i, n) for(int i = 0; i < n; i ++)
#define ALL(T) T.begin(), T.end()
#define mp make_pair
#define pb push_back
#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 dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};

int cnt, w, h;
char m[20][20];

void dfs(pii);

int main(void){
    while(cin >> w >> h, w){
        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);
        cout << cnt << endl;
    }

    return 0;
}

void dfs(pii p){
    if(p.X < 0 || w <= p.X ||
            p.Y < 0 || h <= p.Y) return;

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

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();
  }
}