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 <queue>
using namespace std;
class pos
{
	public:
	int x;
	int y;
	pos(int,int);
};

pos::pos(int a,int b)
{
	x=a;y=b;
}

int main()
{
	int w,h;
	int g[21][21];
/*	for(int i=0;i<21;i++)
		for(int j=0;j<21;j++)
			g[i][j]=-1;
*/	while(1)
	{
		cin>>w>>h;
		if(w==0&&h==0) break;
		for(int i=0;i<h;i++)
			for(int j=0;j<w;j++)
				g[i][j]=-1;
		int psx,psy;
		bool visited[21][21];
		for(int i=0;i<h;i++)
			for(int j=0;j<w;j++)
			{
				char s;
				cin>>s;
				switch(s)
				{
					case '.':
						g[i][j]=0;break;
					case '#':
						g[i][j]=1;break;
					case '@':
						g[i][j]=2;psx=i;psy=j;break;
				}
				visited[i][j]=false;
			}

/*		for(int i=0;i<h;i++)
		{
			for(int j=0;j<w;j++)
				cout<<g[i][j];
			cout<<endl;
		}
*/
		int num=0;
		queue<pos> Q;
		Q.push(pos(psx,psy));
		num++;
		visited[psx][psy]=true;
		while(!Q.empty())
		{
			pos now=Q.front();
			Q.pop();
	
			for(int i=-1;i<=1;i+=2)
			{
				int newx=now.x+i;
				int newy=now.y;
				if(newx>=0&&newx<h&&newy>=0&&newy<w&&g[newx][newy]==0&&visited[newx][newy]==false)
				{
					Q.push(pos(newx,newy));		
					num++;
					visited[newx][newy]=true;
				}
			}
			for(int i=-1;i<=1;i+=2)
			{                
				int newx=now.x;
				int newy=now.y+i;
				if(newx>=0&&newx<h&&newy>=0&&newy<w&&g[newx][newy]==0&&visited[newx][newy]==false)
				{
					Q.push(pos(newx,newy));
					num++;
					visited[newx][newy]=true;
				}
			}

		}
		cout<<num<<endl;
								


	}
}

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

int W,H;
int nx[4],ny[4];
string M[128];

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

bool good_cell(int x,int y) {
  return min(x,y) >= 0 && x<W && y<H;
}

bool safe_cell(int x, int y) {
  return good_cell(x,y) && M[y][x] == '.';
}

void neighbor4(int x, int y) {
  for (int d=0; d<4; ++d) {
    nx[d] = x + dx[d], ny[d] = y + dy[d];
  }
}

void walk1() {
  for (int y=0; y<H; ++y) {
    for(int x=0; x<W; ++x) {
      if (M[y][x] == '@') {
	neighbor4(x, y);
	for (int i=0; i<4; ++i) {
	  if (good_cell(nx[i],ny[i]) == 1)
	  if (M[ny[i]][nx[i]] == '.') M[ny[i]][nx[i]] = '@';
	}
      }
    }
  }
}

void print_map() {
  for (int y=0; y<H; ++y) cout << M[y] << endl;
}

int main() {
  while (cin >> W >> H && W > 0) {
    int c=0;
    for (int i=0; i<H; ++i)
      cin >> M[i];
  


for (int y=0; y<H; ++y) {
  for (int x=0; x<W; ++x) {
    if (M[y][x] == '@') {
    }
  }
 }
 for (int i=0; i<W*H; ++i) walk1();
 //print_map();
 for (int y=0; y<H; ++y) {
   for(int x=0; x<W; ++x) {
     if(M[y][x] =='@') {
       c++;
     }
   }
 }
 cout << c << endl;
  }
}

C++: #include<iostream>
#include<cstdio>
#include<array>
#include<vector>

using namespace std;

vector<vector<int>>tile(22, vector<int>(22));

array<int, 4>mx = { -1,0,0,1 };
array<int, 4>my = { 0,-1,1,0 };

void walk(int x, int y) {
	tile[x][y] = 1;
	for (int i = 0; i < 4; i++) {
		if (tile[x + mx[i]][y + my[i]] != 0)continue;
		walk(x + mx[i], y + my[i]);
	}
}

int main() {
	int w, h;
	while (cin >> w >> h, w) {
		int stx, sty;
		for (auto &i : tile) {
			for (auto &j : i) {
				j = -1;
			}
		}
		for (int i = 1; i <= h; i++) {
			for (int j = 1; j <= w; j++) {
				char ti;
				cin >> ti;
				if (ti == '.')tile[j][i] = 0;
				else if (ti == '#')tile[j][i] = -1;
				else {
					tile[j][i] = 1;
					stx = j; sty = i;
				}
			}
		}
		walk(stx, sty);
		int ans = 0;
		for (auto i : tile) {
			for (auto j : i) {
				if (j == 1)ans++;
			}
		}
		printf("%d\n", ans);
	}
}

C++: #include <iostream>
#define rep(i,a,b) for(int i=(a); i<(b); i++)

using namespace std;

int w,h;
char m[22][22];
int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};

int dfs(int y , int x){
  int rtn = 0;
  if(y < 0 || h <= y || x < 0 || w <= x || m[y][x] =='#')
    return rtn;
  m[y][x] = '#';
  rtn++;
  rep(i,0,4){
    rtn += dfs(y + dy[i] , x + dx[i]);
  }
  return rtn;
}

int main(){
  while(cin >> w >> h , w + h){
    int sx,sy;
    rep(i,0,h){
      rep(j,0,w){
        cin >> m[i][j];
        if(m[i][j] == '@'){
          sx = j;
          sy = i;
        }
      }
    }
    cout << dfs(sy , sx) << endl;
  }
  return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <functional>
using namespace std;
#define MOD 1000000007
#define ADD(X,Y) ((X) = ((X) + (Y)%MOD) % MOD)
typedef long long i64; typedef vector<int> ivec; typedef vector<string> svec; typedef pair<int, int> pi;
int dd[] = { 0, 1, 0, -1 };


int main()
{
	while (true)
	{
		int W, H;
		cin >> W >> H;
		if (W == 0 && H == 0) break;

		char C[30][30];
		int B[30][30];
		for (int i = 0; i < H; i++)
		{
			scanf("%s", C[i]);
		}
		int sx = -1; int sy = -1;
		for (int i = 0; i < H; i++)
		{
			for (int j = 0; j < W; j++)
			{
				B[i][j] = 0;
				if (C[i][j] == '@')
				{
					sy = i;
					sx = j;
				}
			}
		}

		queue<pi> Q;
		Q.push(pi(sy, sx));
		B[sy][sx] = 1;
		int ans = 1;
		while (!Q.empty())
		{
			int ty = Q.front().first; int tx = Q.front().second;
			Q.pop();

			for (int r = 0; r < 4; r++)
			{
				int y = ty + dd[r];
				int x = tx + dd[r + 1];
				if (y < 0 || y >= H || x < 0 || x >= W)continue;
				if (C[y][x] == '#')continue;
				if (B[y][x])continue;
				ans++;
				B[y][x] = 1;
				Q.push(pi(y, x));
			}
		}

		cout << ans << endl;
	}
	return 0;
}