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 <string>
#include <queue>

using namespace std;

int main(){
	int w,h;
	int data[30][30];
	int flag[30][30];
	int dx[4] ={0,1,0,-1};
	int dy[4] ={1,0,-1,0};

	while(1){
		cin>>w>>h;
		if(w==0 && h==0)break;

	for(int i=0;i<30;i++){
		for(int j=0;j<30;j++){
			data[i][j]=-1;
			flag[i][j]=0;
		}
	}

	char tmp;
	int startX;
	int startY;
	int cnt=0;

	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			cin>>tmp;
			if(tmp=='.')data[i][j] = 0;
			else if(tmp=='@'){
				startX = j;
				startY = i;
			}
		}
	}

	queue<int> qX,qY;
	qX.push(startX);
	qY.push(startY);

	while(qX.size()!=0){
		int topX = qX.front();
		int topY = qY.front();
		qX.pop();
		qY.pop();
		cnt++;

		for(int i=0;i<4;i++){
			int nowX = topX + dx[i];
			int nowY = topY + dy[i];

			if(data[nowY][nowX] ==0 && flag[nowY][nowX] == 0){
				qX.push(nowX);
				qY.push(nowY);
				flag[nowY][nowX]=1;
			}
		}
	}

	cout<<cnt<<endl;
	}

	return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <stack>
#include <queue>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
static const double EPS = 1e-5;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
int c=0,W,H;
vector<string> grid;
string tmp;
const int dx[4] = {0,1,0,-1};
const int dy[4] = {1,0,-1,0}; 
void dfs(int x, int y){
  c++;
  grid[y][x]='#';
  REP(r,4){
    if(x+dx[r]>=0&&x+dx[r]<W
        &&y+dy[r]>=0&&y+dy[r]<H
        &&grid[y+dy[r]][x+dx[r]]=='.'){
      dfs(x+dx[r],y+dy[r]);
    }
  }
}

int main(void){
  while(cin>>W>>H){
    if(W==0) break;
    grid.clear();
    c = 0;
    REP(y,H){
      cin>>tmp;
      grid.push_back(tmp);
    }
    REP(y,H)REP(x,W){
      if(grid[y][x]=='@'){
        dfs(x,y);
      }
    }
    cout<<c<<endl;
  }
  return 0;
}

C++: '#include <bits/stdc++.h>
using namespace std;

#define REP(i, n) for(int i = 0;i < (n);i++)
#define FOR(i, a, b) for(int i = (a);i < (b);i++)
#define all(v) v.begin(),v.end()

using int64 = long long;

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

int main(void){
	int H, W;
	while(cin >> H >> W && H && W){
		vector<string> f(H+2, string(W+2, '#'));
		int sy, sx;
		REP(i, H){
			cin >> f[i+1];
			REP(j, W+2)
				if(f[i+1][j] == '@'){
					sy = i+1;
					sx = j;
				}
			f[i+1][0] = f[i+1][W+1] = '#';
		}
		bool visited[30][30] = {};
		int cnt = 0;
		
		auto dfs = [&](int y, int x){
			visited[y][x] = 1;
			cnt++;
			REP(i, 4){
				int yy = y+dy[i], xx = x+dx[i];
				if(f[yy][xx] != '#' && !visited[yy][xx]){
					dfs(yy, xx);
				}
			}
		};
		dfs(sy, sx);
		cout << cnt << endl;
	}
}'

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

using namespace std;

int main(){
  int w,h,count;
  queue<int> que;
  int xx[4]={1,0,-1,0};
  int yy[4]={0,1,0,-1};
  int pos;
  string tt;
  while(true){
    cin >> w >> h;
    if(!w&&!h)
      break;
    int mmap[w+2][h+2];
    for(int x=0;x<w+2;x++){
      mmap[x][0] = -1;
      mmap[x][h+1] = -1;
    }
    for(int y=0;y<h+2;y++){
      mmap[0][y] = -1;
      mmap[w+1][y] = -1;
    }
        
    for(int y=0;y<h;y++){
      cin >> tt;
      for(int x=0;x<w;x++){
	switch(tt[x]){
	case '.':
	  mmap[x+1][y+1] = 0;
	  break;
	case '#':
	  mmap[x+1][y+1] = -1;	  
	  break;
	case '@':
	  mmap[x+1][y+1] = 1;
	  que.push(y*w+x);
	  break;
	}
      }
    }
    count = 1;
    while(!que.empty()){
      pos = que.front();
      for(int dir=0;dir<4;dir++){
	if (mmap[pos%w+xx[dir]+1][pos/w+yy[dir]+1]==0){
	  que.push(pos+yy[dir]*w+xx[dir]);
	  mmap[pos%w+xx[dir]+1][pos/w+yy[dir]+1] = 1;
	  count++;
	}
      }
      que.pop();
    }
    cout << count << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <cfloat>
using namespace std;

int solve(vector<string>& tile)
{
    int h = tile.size();
    int w = tile[0].size();
    int sy, sx;
    for(int i=0; i<h; ++i){
        for(int j=0; j<w; ++j){
            if(tile[i][j] == '@'){
                sy = i;
                sx = j;
            }
        }
    }

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

    vector<vector<bool> > check(h, vector<bool>(w, false));
    check[sy][sx] = true;
    queue<pair<int, int> > q;
    q.push(make_pair(sy, sx));

    int ret = 0;
    while(!q.empty()){
        int y = q.front().first;
        int x = q.front().second;
        q.pop();
        ++ ret;
        for(int i=0; i<4; ++i){
            int y0 = y + dy[i];
            int x0 = x + dx[i];
            if(tile[y0][x0] == '.' && !check[y0][x0]){
                q.push(make_pair(y0, x0));
                check[y0][x0] = true;
            }
        }
    }
    return ret;
}

int main()
{
    for(;;){
        int w, h;
        cin >> w >> h;
        if(w == 0)
            break;

        vector<string> tile(h+2);
        tile[0] = tile[h+1] = string(w+2, '#');
        for(int i=0; i<h; ++i){
            cin >> tile[i+1];
            tile[i+1] = '#' + tile[i+1] + '#';
        }

        cout << solve(tile) << endl;
    }
}