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<stdio.h>
//#include<windows.h>

int masu[22][22];
int ans=0;
int i,j;

void saiki(int x,int y){
	/*
	for(i=0;i<22;i++){
		for(j=0;j<22;j++){
			if(masu[i][j]==0)printf(" ");
			if(masu[i][j]==1)printf("¡");
		}
		puts("");
	}
	printf("%d",ans);
	Sleep(100);
	system("cls");
	*/
	masu[x][y]=1;
	ans++;
	if(masu[x][y-1]==0)saiki(x,y-1);
	if(masu[x+1][y]==0)saiki(x+1,y);
	if(masu[x][y+1]==0)saiki(x,y+1);
	if(masu[x-1][y]==0)saiki(x-1,y);
}

int main(){
	for(;;){
		ans = 0;
	int W,H;
	int x,y;
	char str[22];
	
	for(i=0;i<22;i++){
		for(j=0;j<22;j++){
			masu[i][j]=1;
		}
	}
	
	scanf("%d%d", &W, &H);
	if(W == 0){
		break;
	}
	for(i=0;i<H;i++){
		scanf("%s",str);
		for(j=0;j<W;j++){
			if(str[j]=='.')masu[i+1][j+1]=0;
			if(str[j]=='@'){
				x=i+1;
				y=j+1;
				masu[i+1][j+1]=0;
			}
		}
	}
	/*
	for(i=0;i<22;i++){
		for(j=0;j<22;j++){
			if(masu[i][j]==0)printf(" ");
			if(masu[i][j]==1)printf("¡");
		}
		puts("");
	}
	printf("%d\n",ans);
	system("cls");
	*/
	saiki(x,y);
	/*
	for(i=0;i<22;i++){
		for(j=0;j<22;j++){
			if(masu[i][j]==0)printf(" ");
			if(masu[i][j]==1)printf("¡");
		}
		puts("");
	}
	*/
	printf("%d\n",ans);
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <queue>
#include <set>
#include <stdio.h>
using namespace std;
#define ll long long
typedef pair<int, int> P;
char maze[30][30];
int  w, h;
int sx, sy;
int ans = 0;

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

void bfs() {
	queue<P> que;
	que.push(P(sy, sx));

	while (que.size()) {
		P p = que.front();
		que.pop();
		for (int i = 0; i < 4; i++) {
			int nx = p.second + dx[i], ny = p.first + dy[i];


			if (0 <= nx && nx < w && 0 <= ny && ny < h && maze[ny][nx] != '#') {
				que.push(P(ny, nx));
				maze[ny][nx] = '#';
				ans++;
			}
		}
	}
	return;
}

int main() {
	while (1) {
		ans = 0;
		cin >> w >> h;
		if (w == 0 && h == 0) break;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				cin >> maze[i][j];
				if (maze[i][j] == '@') {
					maze[i][j] = '#';
					sx = j;
					sy = i;
					ans++;
				}
			}
		}
		bfs();
		cout << ans << endl;
	}

	return 0;
}

C++: /**
 *    author:  otera    
**/
#include<iostream>
#include<string> 
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm> 
#include<functional>
#include<iomanip>
#include<queue>
#include<deque>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
using namespace std;

//#define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef long double ld;
const int inf=1e9+7;
const ll INF=1LL<<60 ;
const ll mod=1e9+7 ;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<int, int> P;
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
#define pb push_back
#define debug(x)  cerr << #x << " = " << (x) << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

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

int w, h;
vector<vector<char>> c;
int ans;

void dfs(int i, int j) {
    c[i][j] = '#';
    ++ ans;
    rep(k, 4) {
        int ni = i + dx[k], nj = j + dy[k];
        if(0 <= ni and ni < h and 0 <= nj and nj < w and c[ni][nj] == '.') {
            dfs(ni, nj);
        }
    }
}

void solve() {
	while(cin >> w >> h) {
        if(w == 0 and h == 0) break;
        c.assign(h, vector<char>(w));
        int si, sj;
        rep(i, h) {
            rep(j, w) {
                cin >> c[i][j];
                if(c[i][j] == '@') {
                    c[i][j] = '.';
                    si = i, sj = j;
                }
            }
        }
        ans = 0;
        dfs(si, sj);
        cout << ans << endl;
    }
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	//int t; cin >> t; rep(i, t)solve();
	solve();
    return 0;
}

C++: #include<stdio.h>
char a[21][21];
int c;
void dfs(int y,int x){
  c++;
  a[y][x]=0;
  if(y!=0)
    if(a[y-1][x]=='.')
      dfs(y-1,x);
  if(y!=20)
    if(a[y+1][x]=='.')
      dfs(y+1,x);
  if(x!=0)
    if(a[y][x-1]=='.')
      dfs(y,x-1);
  if(x!=20)
    if(a[y][x+1]=='.')
      dfs(y,x+1);
}
int main(){
  int w,h,i,j;
  while(1){
    scanf("%d%d",&w,&h);
    if(w==0&&h==0)break;
    for(i=0;i<21;i++){
      for(j=0;j<21;j++){
	a[i][j]=0;
      }
    }
    for(i=0;i<h;i++){
      for(j=0;j<=w;j++){
	scanf("%c",&a[i][j]);
      }
    }
    c=0;
    for(i=0;i<h;i++){
      for(j=0;j<w;j++){
	if(a[i][j]=='@')dfs(i,j);
      }
    }
    printf("%d\n",c);

   
  }
  return 0;
}

C++: #include<stdio.h>
char a[21][21];
int c;
void dfs(int y,int x){
  c++;
  a[y][x]=0;
  if(y!=0)
    if(a[y-1][x]=='.')
      dfs(y-1,x);
  if(y!=20)
    if(a[y+1][x]=='.')
      dfs(y+1,x);
  if(x!=0)
    if(a[y][x-1]=='.')
      dfs(y,x-1);
  if(x!=20)
    if(a[y][x+1]=='.')
      dfs(y,x+1);
}
int main(){
  int w,h,i,j;
  char b[21];
  while(1){
    scanf("%d%d",&w,&h);
    if(w==0&&h==0)break;
    for(i=0;i<21;i++){
      for(j=0;j<21;j++){
	a[i][j]=0;
      }
    }
    for(i=0;i<h;i++){
	scanf("%s",b);
	for(j=0;b[j]!='\0';j++){
	  a[i][j]=b[j];
	}
    }
    c=0;
    for(i=0;i<h;i++){
      for(j=0;j<w;j++){
	if(a[i][j]=='@')dfs(i,j);
      }
    }
    printf("%d\n",c);

   
  }
  return 0;
}