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>
using namespace std;

int cnt, a[23][23];  //black : 0, red : 1

void dfs(int x, int y){
    cnt++;
    a[x][y] = 1;
    if(!a[x + 1][y]) dfs(x + 1, y);
    if(!a[x - 1][y]) dfs(x - 1, y);
    if(!a[x][y + 1]) dfs(x, y + 1);
    if(!a[x][y - 1]) dfs(x, y - 1);
    return;
}

int main(){
    while(1){
        int i, j, w, h, si, sj;
        char in;
        cin >> w >> h;
        if(w == 0)break;
        
        cnt = 0;
        for(i = 0; i <= h + 1; i++){
            a[i][0] = a[i][w + 1] = 1;
        }
        for(j = 0; j <= w + 1; j++){
            a[0][j] = a[h + 1][j] = 1;
        }
       
        i = 1;
        j = 1;
        while(i <= h){
            while(j <= w){
                cin >> in;
                //cout << "~~" << endl;
                if(in == '.'){
                   a[i][j] = 0;
                   j++; 
                }else if(in == '#'){
                    a[i][j] = 1;
                    j++;
                }else if(in == '@'){
                    si = i;
                    sj = j;
                    a[i][j] = 1;
                    j++;
                }
                //cout << j << endl;
            
            
            
            }
            i++;
            j = 1;
        }
        dfs(si, sj);
        cout << cnt << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<utility>
#include<queue>
using namespace std;
char room[21][21];
int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};
typedef pair<int,int> p;


int main(){
	int x,y;
	queue<p> que;
	int w,h;
	cin>>w>>h;
	if(w+h==0) return;
	for(int a=0;a<h;a++){
		for(int b=0;b<w;b++){
			cin>>room[a][b];
			if(room[a][b]=='@'){
				x=b,y=a;
				que.push(p(b,a));
				room[a][b]='#';
			}
		}
		
	}
	int cnt=1;
	int nx,ny;
	while(que.size()){
		p ap=que.front();
		que.pop;
		for(int i=-1;i<=1;i++){
			nx=ap.first+dx[i];
			ny=ap.second+dy[i];
			if(0<=nx&&nx<=w&&0<=ny&&ny<=h&&room[ny][nx]!='#'){
				room[ny][nx]='#';
				que.push(p(nx,ny));
				cnt++;
			}
		}
	}
	cout<<cnt;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<utility>
#include<map>
#include<set>
#include<queue>
using namespace std;
#define N (1000000000+7)
typedef pair<int,int> P;
typedef long long ll;

int h,w;
char board[30][30];
bool board1[30][30];
bool check(int i,int j){
    return (i>=1)&&(i<=h)&&(j>=1)&&(j<=w)&&((board[i][j]=='.')||(board[i][j]=='@'))&&(!board1[i][j]);
}

int dfs(int i,int j){
    if(check(i,j)){
        board1[i][j]=true;
        return dfs(i+1,j)+dfs(i-1,j)+dfs(i,j+1)+dfs(i,j-1)+1;
    }
    else return 0;
}

int main(void){
    while(true){
        cin>>w>>h;
        int sx=0,sy=0;
        if((h==0)&&(w==0))break;
        for(int i=0;i<30;i++){
            for(int j=0;j<30;j++){
                board1[i][j]=false;
            }
        }
        for(int i=1;i<=h;i++){
            for(int j=1;j<=w;j++){
                cin>>board[i][j];
                if(board[i][j]=='@'){
                    sx=i;
                    sy=j;
                }
            }
        }
        cout<<dfs(sx,sy)<<endl;
    }

    return 0;
}

C++: #include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iterator>
#include <bitset>
#include <unordered_set>
#include <unordered_map>
#include <fstream>
#include <iomanip>
#include <cassert>
//#include <utility>
//#include <memory>
//#include <functional>
//#include <deque>
//#include <cctype>
//#include <ctime>
//#include <numeric>
//#include <list>
//#include <iomanip>

//#if __cplusplus >= 201103L
//#include <array>
//#include <tuple>
//#include <initializer_list>
//#include <forward_list>
//
//#define cauto const auto&
//#else

//#endif

using namespace std;


typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()
#define DEBUG
#ifdef DEBUG
#define dump(x)  cout << #x << " = " << (x) << endl;
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#else
#define dump(x) 
#define debug(x) 
#endif

#define MOD 1000000007LL
#define EPS 1e-8
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
#define maxs(x,y) x=max(x,y)
#define mins(x,y) x=min(x,y)


int a[30][30];
int h,w;

vector<string> v;

bool inside(int a, int b){
	return 0<=a&&a<h&&0<=b&&b<w;
}


void dfs(pii p){
	int dd[]={0,1,0,-1,0};
	if(a[p.F][p.S]) return;
	a[p.F][p.S]=1;
	rep(i,4){
		pii np=p;
		np.F+=dd[i];
		np.S+=dd[i+1];
		if(inside(np.F,np.S)){
			if(v[np.F][np.S]=='#') continue;
			dfs(np);
		}
	}
}

void mainmain(){
	while(cin>>w>>h,w||h){
		v=vector<string>(h);
		rep(i,h) cin>>v[i];
		pii p;
		rep(i,h){
			rep(j,w){
				if(v[i][j]=='@'){
					p=mkp(i,j);
				}
			}
		}
		rep(i,30) rep(j,30) a[i][j]=0;
		dfs(p);
		int ans = 0;
		rep(i,30){
			rep(j,30){
				ans += a[i][j];
			}
		}
		cout<<ans<<endl;
	}
}


signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout<<fixed<<setprecision(20);
    mainmain();
}