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 <cstdio>
#include <string>
#include <sstream>
#include <algorithm>
#include <math.h>
//#include <map>
#include <iomanip>
#include <vector>

using namespace std;

int W, H, sw, sh, cnt;
char tiles[20][20];
int dw[4] = {-1, 0, 0, 1};
int dh[4] = {0, -1, 1, 0};


void dfs(int w, int h)
{
	tiles[h][w] = '#';
	cnt++;
	
	for (int i=0; i<4; i++)
	{
		int nw = w + dw[i];
		int nh = h + dh[i];
		
		if (0<=nw && nw<W && 0<=nh && nh<H && tiles[nh][nw]=='.')
		{
			dfs(nw, nh);
		}
	}
}


int main()
{
	while (cin >> W >> H)
	{
		if (W == 0 && H == 0) break;
		
		//入力
		for (int i=0; i<H; i++)
		{
			cin >> tiles[i];
		}
		
		//スタート地点を探す
		for (int i=0; i<H; i++)
		{
			for (int j=0; j<W; j++)
			{
				if (tiles[i][j] == '@')
				{
					sw = j;
					sh = i;
					break;
				}
			}
		}
		
		cnt = 0;
		dfs(sw, sh);
		cout << cnt << endl;
		
		//cout << "sw = " << sw << " : sh = " << sh << endl;
		
		/*
		//テスト出力
		for (int i=0; i<H; i++)
		{
			cout << tiles[i] << endl;
		}
		*/
	}
	
	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define shosu(n) setprecision(n)
using namespace std;
char s[100][100];
int H, W, stx, sty;
void dfs(int x, int y) {
  if (x < 0 || y < 0 || x >= W || y >= H)
    return;
  if (s[y][x] == '.') {
    s[y][x] = 'x';
  } else
    return;
  dfs(x + 1, y);
  dfs(x - 1, y);
  dfs(x, y + 1);
  dfs(x, y - 1);
}
int main() {
  while (cin >> W >> H) {
    int ans = 0;
    if (H == 0 && W == 0)
      break;
    rep(i, H) rep(j, W) {
      cin >> s[i][j];
      if (s[i][j] == '@') {
        stx = j;
        sty = i;
      }
    }
    s[sty][stx] = '.';
    dfs(stx, sty);
    rep(i, H) rep(j, W) {
      if (s[i][j] == 'x')
        ans++;
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <queue>
#include <map>

using namespace std;


#define F first
#define S second
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define INT(a) int((a)+1e-9)
#define SUPMaxX 22
#define SUPMaxY 22

typedef pair<int,int> Pii;

int DX[4]={1,0,-1,0};
int DY[4]={0,1,0,-1};

int maxX,maxY;
char tizu[SUPMaxX][SUPMaxY];
queue<Pii> ps;

int solve(){
	int re=1;
	while(!ps.empty()){
		Pii p=ps.front();
		ps.pop();
		int px=p.F;
		int py=p.S;
		FOR(i,0,4){int qx=px+DX[i]; int qy=py+DY[i];
			if(tizu[qx][qy]=='.'){
				tizu[qx][qy]='@';
				ps.push(make_pair(qx,qy));
				re++;
			}
		}
	}
	return re;
}


int main(){
	while(true){
		cin >> maxY >> maxX;			if(!maxX) return 0;
		FOR(x,0,maxX+2) FOR(y,0,maxY+2) tizu[x][y]='#';
		FOR(x,1,maxX+1) FOR(y,1,maxY+1){
			cin>>tizu[x][y];
			if(tizu[x][y]=='@') ps.push(make_pair(x,y));
		}
		cout<<solve()<<endl;
	}
}

C++: #include <cstdio>
#include <iostream>
using namespace std;
int counta;
void judge(char d[][20], int x, int y, int limitx, int limity) {
	if (d[y][x] == '@' || d[y][x] == '.') {
		d[y][x] = '#';
		counta++;
		if (x > 0) judge(d, x - 1, y, limitx, limity);
		if (x <= limitx - 2)judge(d, x + 1, y, limitx, limity);
		if (y>0)judge(d, x, y - 1, limitx, limity);
		if (y <= limity-2)judge(d, x, y + 1, limitx, limity);
	}
}

int main() {
	int x, y;
	int manx = 0;
	int many = 0;
	do {

		counta = 0;
		char a[20][20];
		for (int i = 0; i < 20; i++) {
			for (int j = 0; j < 20; j++)
				a[i][j] = '#';
		}
		cin >> x >> y;
		if (x == 0)break;
		for (int i = 0; i < y; i++) {
			for (int j = 0; j < x; j++) {
				cin >> a[i][j];
				if (a[i][j] == '@') { many = i; manx = j; }
			}
		}
		judge(a, manx, many, x, y);
		cout << counta << "\n";

	} while (1);
}

C++: #include <iostream>
#include <string>
#include <queue>

using namespace std;
struct Point{
  int x;int y;
};

int main(){
  int w,h,i,j,k,c=0;
  string str;
  Point p,b;
  cin >> w >> h;
  while(w!=0&&h!=0){
    c=1;
    int dp[21][21]={{}};
    for(i=0;i<h;i++){
      cin >> str;
      for(j=0;j<w;j++){
	if(str[j]=='.') dp[j][i]= 1;
	if(str[j]=='#') dp[j][i]= 2;
	if(str[j]=='@') {
	  dp[j][i]= 3;
	  p.x=j;p.y=i;
	}
      }
    }
    
    queue <Point> q;
    q.push(p);
    while(!q.empty()){
      p = q.front();
      q.pop();
      if((p.x+1)<w&&dp[p.x+1][p.y]==1){
	c++;
	b.x=p.x+1;b.y=p.y;
	dp[p.x+1][p.y]=-1;
	q.push(b);
      }
      if((p.x-1)>=0&&dp[p.x-1][p.y]==1){
	c++;
	b.x=p.x-1;b.y=p.y;
	dp[p.x-1][p.y]=-1;
	q.push(b);
      }
      if((p.y+1)<h&&dp[p.x][p.y+1]==1){
	c++;
	b.x=p.x;b.y=p.y+1;
	dp[p.x][p.y+1]=-1;
	q.push(b);
      }
      if((p.y-1)>=0&&dp[p.x][p.y-1]==1){
	c++;
	b.x=p.x;b.y=p.y-1;
	dp[p.x][p.y-1]=-1;
	q.push(b);
      }
    }
    cout << c << endl;
    cin >> w >> h;
  }
  return 0;
}

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

#define inRange(x,a,b) (a <= x && x < b)

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

int main(){
    int h, w;
    while(cin >> w >> h, w+h){
        char mat[h][w];
        int x, y;
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                cin >> mat[i][j];
                if(mat[i][j] == '@')    y = i, x = j, mat[i][j] = '.';
            }
        }

        stack<pair<int,int>> s;
        s.push({y,x});

        int ans = 0;
        while(!s.empty()){
            pair<int,int> now = s.top();    s.pop();
            if(mat[now.first][now.second] == '#')   continue;
            if(mat[now.first][now.second] == '.')   ans++;
            mat[now.first][now.second] = '#';
            for(int k = 0; k < 4; k++){
                int ny = now.first + dy[k];
                int nx = now.second + dx[k];
                if(inRange(ny, 0, h) && inRange(nx,0,w) && mat[ny][nx] == '.'){
                    s.push({ny, nx});
                }
            }
        }

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