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 <algorithm>
#include <cctype>
#include <cstdio>
#include <iostream>
#include <locale>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

using namespace std;

int solve(int w, int h) {
    vector<string> s(h);
    queue<int> queX;
    queue<int> queY;
    for (int i = 0; i < h; i++) {
        cin >> s[i];
    }
    int x, y;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (s[i][j] == '@') {
                queX.push(i);
                queY.push(j);
                s[i][j] = '#';
                break;
            }
        }
    }
    int xmove[4] = {0, 0, -1, 1};
    int ymove[4] = {1, -1, 0, 0};
    int ans      = 0;
    while (!queX.empty() && !queY.empty()) {
        x = queX.front();
        y = queY.front();
        queX.pop();
        queY.pop();
        ans++;

        for (int i = 0; i < 4; i++) {
            if (x + xmove[i] >= 0 && x + xmove[i] < h && y + ymove[i] >= 0 && y + ymove[i] < w) {
                if (s[x + xmove[i]][y + ymove[i]] == '.') {
                    queX.push(x + xmove[i]);
                    queY.push(y + ymove[i]);
                    s[x + xmove[i]][y + ymove[i]] = '#';
                }
            }
        }
    }
    return ans;
}

int main() {
    int w, h;
    vector<int> ans;
    while (1) {
        cin >> w >> h;
        if (!w && !h)
            break;
        ans.push_back(solve(w, h));
    }
    for (int a : ans) cout << a << endl;
}

C++: #include<iostream>
#include<vector>
#include<string>
#include<queue>
#include<map>
#define INF 1000000000
using namespace std;
typedef pair<int,int> P;
int dx[]={0,0,1,-1},dy[]={1,-1,0,0};
int h,w,n;
vector<string> s;
bool inf(int y,int x){return(y>=0&&y<h&&x>=0&&x<w)?1:0;}
int bfs(int sy,int sx){
	int ret=0;
	queue<P> que;
	que.push(P(sy,sx));
	vector<vector<int> > d(h,vector<int>(w,INF));
	d[sy][sx]=0;
	while(!que.empty()){
		P t=que.front();
		que.pop();
		int y=t.first,x=t.second;
		
		
		for(int i=0;i<4;i++){
			int ny=dy[i]+y,nx=dx[i]+x;
			if(inf(ny,nx)&&d[ny][nx]>d[y][x]+1&&s[ny][nx]!='#'){
				que.push(P(ny,nx));
				d[ny][nx]=d[y][x]+1;
			}
		}
	}
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			if(d[i][j]!=INF)
				ret++;
		}
	}
	return ret;
}
int main(){
	while(cin>>w>>h,h||w||n){
		s=vector<string>(h);
		for(int i=0;i<h;i++){
			cin>>s[i];
		}
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(s[i][j]=='@'){
					cout<<bfs(i,j)<<endl;
				}
			}
		}
	}
	return 0;
}

C++: #include<algorithm>
#include<functional>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<sstream>
#include<iostream>
#include<iomanip>
#include<vector>
#include<list>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<bitset>

#define all(c) (c).begin(), (c).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second

typedef unsigned long long ull;
typedef long long ll;

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

using namespace std;

typedef pair<int ,int > P;

int ans;
int w,h;
char maze[27][27];

void dfs(int x,int y)
{
	//printf("[debug]:%d,%d\n",x,y);
	maze[x][y]='#';
	rep(i,4)
	{
		int nx=dx[i]+x;
		int ny=dy[i]+y;
		if(maze[nx][ny]=='.')
		{
			ans++;
			dfs(nx,ny);
		}
	}

}

void solve()
{
	/*
	rep(i,25)
	{
		rep(j,25)
		{
			cout<<maze[i][j];
		}
		cout<<endl;
	}
	*/
	rep(i,h) rep(j,w)
	{
		if(maze[i][j]=='@')
		{
			dfs(i,j);
			cout<<ans+1<<endl;
			return;
		}
	}


}

int main()
{
	rep(i,25) rep(j,25) maze[i][j]='#';
	
	while(cin>>w>>h)
	{
		if(w==0&&h==0) return 0;
		rep(i,h) rep(j,w) 
		{
			cin>>maze[i][j];
		}
		solve();
		ans=0;
		rep(i,25) rep(j,25) maze[i][j]='#';
	}


	return 0;
}

C++: #include <string>
#include <iostream>
#include <string.h>

using namespace std;

int w, h;
bool b[20][20];
bool ok[20][20];

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

void dfs(int x, int y){
	for(int i=0;i<4;i++){
		if(0<=x+dx[i]&&x+dx[i]<w&&0<=y+dy[i]&&y+dy[i]<h){
			if(!b[y+dy[i]][x+dx[i]]) continue;
			if(ok[y+dy[i]][x+dx[i]]) continue;
			ok[y+dy[i]][x+dx[i]] = true;
			dfs(x+dx[i],y+dy[i]);
		}
	}
}

int main(){
	while(cin >> w >> h){
		if(!w) break;
		int x, y;
		string s;
		memset(b,false,sizeof(b));
		for(int i=0;i<h;i++){
			cin >> s;
			for(int j=0;j<w;j++){
				b[i][j] = (s[j] !='#');
				if(s[j]=='@') x = j, y = i;
			}
		}
		memset(ok,false,sizeof(ok));
		ok[y][x] = true;
		dfs(x,y);
		int count = 0;
		for(int i=0;i<h;i++)
			for(int j=0;j<w;j++)
				count += ok[i][j];
		cout << count << endl;
	}
}

C++: //C++14 (Clang 3.8.0)

#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string>
#define rp(i,a,b) for(int (i)=(int)(a);i<(int)(b);++i)

typedef long long ll;

int mx[]={-1,1,0,0};
int my[]={0,0,-1,1};
using namespace std;
int n=0;
void dfs(int startx,int starty,vector<vector<bool> > &map){
    for(int i=0;i<4;i++){
        int x=startx+mx[i],y=starty+my[i];
        if(x>=0 and y>=0 and y<map.size() and x<map[0].size() and map[y][x]==true){
        map[y][x]=false;
        n++;
        dfs(x,y,map);}
        
    }
    
}

int main(){
    while(1){
        int x,y,startx,starty;
        vector<vector<bool> > map;
        cin>>x>>y;

        if(x==0 and y==0) return 0;
        for(int i=0;i<y;i++){
            vector<bool> tmp;
            string s;
            cin>>s;
            for(int j=0;j<x;j++){

                if(s[j]=='@') {startx=j;starty=i;tmp.push_back(true);}
                else if(s[j]=='.') tmp.push_back(true);
                else tmp.push_back(false);    
            }
            map.push_back((tmp));
        }
        n=0;
        dfs(startx,starty,map);
        cout<<n<<endl;
        
    }

    return 0;
};