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++: /*
 * red_and_black.cpp
 *
 *  Created on: 2017/02/10
 *      Author: Nishio
 */

#include <bits/stdc++.h>
#define REP(i,n,N) for(int i=n;i<N;i++)
#define p(S) cout<<S<<endl
#define mp(a,b) make_pair(a,b)
using namespace std;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};

int main(){
	int H,W;

	while(cin>>W>>H&&W){
		queue<pair<int,int>> q;
		bool dp[21][21]={false};
		int nowx,nowy,nextx,nexty,count=1;
		char c[21][21];
		REP(i,0,H){
			REP(j,0,W){
				cin>>c[i][j];
				if(c[i][j]=='@'){
					dp[i][j]=true;

					q.push(mp(i,j));
				}
			}
		}
		while(!q.empty()){
			nowy=q.front().first;
			nowx=q.front().second;
			q.pop();
			REP(i,0,4){
				nextx=nowx+dx[i];
				nexty=nowy+dy[i];
				if(0<=nextx && nextx<W && 0<=nexty &&nexty<H){
					if(!dp[nexty][nextx]&&c[nexty][nextx]=='.'){
						dp[nexty][nextx]=true;
						q.push(mp(nexty,nextx));
						count++;
					}
				}
			}
		}
		p(count);
	}
	return 0;
}

C++: #include<iostream>
#include<queue>
using namespace std;

int point(int x,int y,int w,int h){
  int s;
  s= y*w+x;
  return s;
}

int point_x(int x,int w){
  int s;
  s=x%w;
  return s;
}

int point_y(int x,int w){
  int s;
  s=x/w;
  return s;
}

int main(){
  int w,h;
  cin>>w>>h;
  while(w!=0){
    int a[21][21]={};
    int sx,sy;
    for(int j=0;j<h;j++){
      for(int i=0;i<w;i++){
	char s;
	cin>>s;
	if(s=='.'){
	  a[i][j]=1;
	}else if(s=='#'){
	  a[i][j]=-1;
	}else{
	  a[i][j]=2;
	  sx=i;
	  sy=j;
	}
      }
    }

    int d[21][21];
    for(int i=0;i<21;i++){
      for(int j=0;j<21;j++){
	d[i][j]=-1;
      }
    }
    queue<int>q;
    d[sx][sy]=0;
    int str;
    str=point(sx,sy,w,h);
    q.push(str);
    while(! q.empty()){
      int cur =q.front();
      q.pop();
      int x,y;
      x=point_x(cur,w);
      y=point_y(cur,w);
      if(x>=1){
	if(a[x-1][y]==1&&d[x-1][y]==-1){
	  d[x-1][y]=0;
	  q.push(point(x-1,y,w,h));
	}
      }
      if(x<=w-2){
	if(a[x+1][y]==1&&d[x+1][y]==-1){
	  d[x+1][y]=0;
	  q.push(point(x+1,y,w,h));
	}
      }
      if(y>=1){
	if(a[x][y-1]==1&&d[x][y-1]==-1){
	  d[x][y-1]=0;
	  q.push(point(x,y-1,w,h));
	}
      }
      if(y<=h-2){
	if(a[x][y+1]==1&&d[x][y+1]==-1){
	  d[x][y+1]=0;
	  q.push(point(x,y+1,w,h));
	}
      }
    }

    int t=0;
    for(int i=0;i<w;i++){
      for(int j=0;j<h;j++){
	if(d[i][j]==0){
	  t+=1;
	}
      }
    }
    cout <<t<< endl;
    cin>>w>>h;
  }
}

C++: #include<iostream>
using namespace std;

int W, H;
char tile[25][25];
int cnt;
int x, y;

void dfs(int y, int x){
	tile[y][x] = '#';
	cnt++;
	int dy[4] = { 0, 1, 0, -1 };
	int dx[4] = { 1, 0, -1, 0 };
	for (int i = 0; i < 4; i++){
		int Y = y + dy[i];
		int X = x + dx[i];
		if (0 <= Y&&Y < H && 0 <= X&&X < W&&tile[Y][X] != '#'){
			dfs(Y, X);
		}
	}
}

int main(){

	while (cin >> W >> H, W != 0 && H != 0){
		for (int i = 0; i < H; i++){
			for (int j = 0; j < W; j++){
				cin >> tile[i][j];
				if (tile[i][j] == '@'){
					y = i;
					x = j;
				}
			}
		}
		dfs(y, x);
		cout << cnt << endl;
		cnt = 0;
	}

	return 0;
}

C++: #include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define REP(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define rer(i, a, b) for (int i = (int)a; i <= (int)b; ++i)
#define each(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define all(v) v.begin(), v.end()
#define mset(a, n) memset(a, n, sizeof(a))

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vector<int> > vvi;
typedef vector<pair<int, int> > vpii;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;

const int inf = 1000000000;
const int mod = 1000000007;
const double eps = 1e-9;
const int dx[] = { -1, 0, 1, 0};
const int dy[] = { 0, -1, 0, 1};

int W, H;
int ans;
string field[100];

void dfs(int y, int x) {
	if (x < 0 || y < 0 || x == W || y == H) return;
	if (field[y][x] == '#') return;
	ans++;
	field[y][x] = '#';
	rep(i, 4) dfs(y + dy[i], x + dx[i]);
}

int main() {
	while (cin >> W >> H, W) {
		ans = 0;
		rep(i, H) cin >> field[i];
		rep(i, H) rep(j, W) {
			if (field[i][j] == '@') dfs(i, j);
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include<iostream>
using namespace std;

int W, H;
char tile[25][25];
int cnt;

void dfs(int y, int x){
	cnt++;
	tile[y][x] = '#';
	int dy[4] = { 0, 1, 0, -1 };
	int dx[4] = { 1, 0, -1, 0 };
	for (int i = 0; i < 4; i++){
		int Y = y + dy[i];
		int X = x + dx[i];
		if (0 <= Y&&Y < H && 0 <= X&&X < W&&tile[Y][X] != '#')dfs(Y, X);
	}
}

int main(){
	
	
	while (cin >> W >> H,W != 0 && H != 0){
		cnt = 0;

		int y, x;
		for (int i = 0; i < H; i++){
			for (int j = 0; j < W; j++){
				cin >> tile[i][j];
				if (tile[i][j] == '@'){
					y = i;
					x = j;
				}
			}
		}
		dfs(y, x);

		cout << cnt << endl;

	}
return 0;
}