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 <algorithm>
#include <vector>
#include <string>

using namespace std;

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

int paint(vector<string> &field, int x, int y) {
    int cnt = 0;

    for(int i = 0; i < 4; ++i) {
        int xx = x + dx[i];
        int yy = y + dy[i];

        if(xx < 0 || field[0].size() <= xx || yy < 0 || field.size() <= yy) continue;

        if(field[yy][xx] == '.') {
            field[yy][xx] = '#';
            cnt += paint(field, xx, yy)+1;
        }
    }
    return cnt;
}

int main() {
    while(true) {
        int W, H;
        cin >> W >> H;

        if(!W && !H) break;

        int sx, sy;
        vector<string> field(H);
        for(int i = 0; i < H; ++i) {
            cin >> field[i];
            for(int x = 0; x < W; ++x) {
                if(field[i][x] == '@') {
                    sx = x;
                    sy = i;
                }
            }
        }

        cout << paint(field, sx, sy)+1 << endl;
    }
    return 0;
}

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


/*int f(int x){

  return x * x;
}

int g(int x){
  if(x == 0) return 1;
  return g(x-1) * x;
}
*/

int H, W;

void  solve(int Y, int X, vector<string> &vec,int &count){

   if(Y<0||Y>=H||X<0||X>=W){
    return;
  }
   else if(vec[Y][X]=='#'){
    return;
  }
  count+=1;
  vec[Y][X]='#';
  solve(Y+1,X,vec, count);
  solve(Y-1,X,vec,count);
  solve(Y,X-1,vec,count);
  solve(Y,X+1,vec,count);

}
int main(){
  //vector<vector<int>>vec(N,vector<int>(N));

  while(1){
    // int W,H
    int count;
  cin >> W >>H;
  if(W==0 &&H==0)break;
  count=0;

  vector<string>vec(H);
  int x,y;

  for(int i =0;i<H;i++){
    cin >> vec.at(i);
    for(int j=0;j<W;j++){
      if(vec[i][j]=='@'){
        x = j;
        y = i;
        break;
      }
           }
  }
  solve(y, x,  vec, count);
  cout << count <<endl;
  }


}

C++: #include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
char stage[22][22];
bool vis[22][22];
int dx[] = {0,1,0,-1};
int dy[] = {1,0,-1,0};
class Data{
	public:
	int x;
	int y;
	int cnt;
	Data(){}
	Data(int x,int y,int cnt):x(x),y(y),cnt(cnt){}
};
int main(){
	int w,h;
	while(scanf("%d%d",&w,&h),(w|h)){
		memset(stage,0,sizeof(stage));
		memset(vis,0,sizeof(vis));
		int sx,sy;
		for(int i = 0; i < h; i++){
			scanf("%s",stage[i]);
			for(int j = 0; j < w; j++){
				if(stage[i][j] == '@'){
					sx = j;
					sy = i;
				}
			}
		}
		
		queue < Data > Q;
		int ans = 0;
		Q.push(Data(sx,sy,1));
		
		while( !Q.empty() ){
			Data d = Q.front();Q.pop();
			if(vis[d.y][d.x]++)continue;
			ans += d.cnt;
			for(int i = 0; i < 4; i++){
				if(dx[i]+d.x < 0 || dx[i]+d.x >= w || dy[i]+d.y < 0 || dy[i]+d.y >= h || stage[dy[i]+d.y][dx[i]+d.x] == '#')continue;
				Q.push(Data(dx[i]+d.x,dy[i]+d.y,d.cnt));
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}

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

using namespace std;

const int dm[] = {-1, 0, 1, 0}, dn[] = {0, -1, 0, 1};
int h, w, count;
vector< vector<int> > field, finished;

bool valid(int m, int n) {
    return 0 <= m && m < h && 0 <= n && n < w && field[m][n] == 1;
}

void dfs(int m, int n) {
    finished[m][n] = 1;
    count++;

    for(int i = 0; i < 4; i++) {
        if(valid(m + dm[i], n + dn[i]) && !finished[m + dm[i]][n + dn[i]]) {
            dfs(m + dm[i], n + dn[i]);
        }
    }
}

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

        field.resize(h); finished.resize(h);
        int m, n;
        for (int i = 0; i < h; i++) {
            string input;
            cin >> input;

            field[i].resize(w); finished[i].resize(w);

            for (int j = 0; j < w; j++) {
                finished[i][j] = 0;

                if(input[j] == '@') {
                    m = i;
                    n = j;
                }
                if(input[j] == '#') {
                    field[i][j] = 0;
                } else {
                    field[i][j] = 1;
                }
            }
        }

        count = 0;
        dfs(m, n);

        cout << count << 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)
bool arrive[25][25];
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};
void dfs(vector<string> &grid, int x, int y){
  REP(r,4){
    if(arrive[y+dy[r]][x+dx[r]]==false&&grid[y+dy[r]][x+dx[r]]=='.'){
      arrive[y+dy[r]][x+dx[r]] = true;
      dfs(grid,x+dx[r],y+dy[r]);
    }
  }
}
int main(void){
  int W,H;
  while(cin>>W>>H,W){
    memset(arrive,0,sizeof(arrive));
    vector<string> grid;
    string tmp;
    REP(i,W+2)tmp+="#";
    grid.push_back(tmp);
    REP(i,H){
      string tmp;
      cin>>tmp;
      tmp = "#"+tmp+"#";
      grid.push_back(tmp);
    }
    grid.push_back(tmp);
    REP(y,grid.size())REP(x,grid[0].size())if(grid[y][x]=='@')dfs(grid,x,y);
    int ans=1;
    REP(y,25)REP(x,25)if(arrive[y][x])ans++;
    cout<<ans<<endl;
  }
  return 0;
}