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 <bits/stdc++.h>
using namespace std;

#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define abs(a) max((a),-(a))
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define repe(i,n) rep(i,(n)+1)
#define per(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define pere(i,n) rep(i,(n)+1)
#define all(x) (x).begin(),(x).end()
#define SP <<" "<<
#define RET return 0
#define MOD 1000000007
#define INF 1000000000000000000

typedef long long LL;
typedef long double LD;

int main(){
  while(1){
    int h,w;
    cin >> w >> h;
    if(w==0) return 0;
    vector<vector<int>> field(h,vector<int>(w));
    vector<vector<bool>> searched(h,vector<bool>(w,false));
    char c;
    int sx,sy;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
        cin >> c;
        if(c=='.'){
          field[i][j]=0;
        }else if(c=='#'){
          field[i][j]=1;
        }else{
          sx=j;
          sy=i;
          field[i][j]=0;
        }
      }
    }
    searched[sy][sx]=true;
    queue<pair<int,int>> bfs;
    bfs.push({sx,sy});
    int nx,ny,houkou[5]={0,1,0,-1,0};
    int count=1;
    while(!bfs.empty()){
      pair<int,int> now=bfs.front();
      bfs.pop();
      for(int i=0;i<4;i++){
        nx=now.first+houkou[i];
        ny=now.second+houkou[i+1];
        if(0<=nx&&nx<w&&0<=ny&&ny<h){
          if(!searched[ny][nx]&&field[ny][nx]==0){
            searched[ny][nx]=true;
            bfs.push({nx,ny});
            count++;
          }
        }
      }
    }
    cout << count << endl;
  }
  return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<map>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;

int h, w;
char mp[22][22];

const int nx[] = { 1, 0,-1, 0};
const int ny[] = { 0, 1, 0,-1};

int dfs(int y, int x);

int main(){
  ios_base::sync_with_stdio(false);

  int sx, sy;

  while(true){

    cin >> w >> h;

    if(w == 0 && h == 0){
      break;
    }

    for(int i=0;i<h;i++){
      cin >> mp[i];
      for(int j=0;j<w;j++){
        if(mp[i][j] == '@'){
          sy = i;
          sx = j; 
        } 
      }
    }

    cout << dfs(sy, sx) << endl;

  }
  return 0;
}

int dfs(int y, int x){
  if(y >= 0 && x >= 0 && y < h && x < w && mp[y][x] != '#'){
    mp[y][x] = '#';
    int t = 0;
    for(int i=0;i<4;i++){
      t += dfs(y + ny[i], x + nx[i]);
    }
    return t+1;
  }
  return 0;
}

C++:     #include<iostream>
    #include<stdio.h>
    //#include <bits/stdc++.h>
    #include<vector>
    #include<float.h>
    #include<iomanip>
    #include<algorithm>
    #include<string>
    #include<cstring>
    #include<math.h>
    #include<cmath>
    #include<sstream>
    #include<set>
    #include<map>
    #include<queue>
    #include<cassert>
    #include<cmath>
    #include<cstdint>
using namespace std;

bool seen[55][55];
vector<string> s(55);
int w, h;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
int res;

void dfs(int y, int x){
    seen[y][x] = true;

    for(int i = 0; i < 4; i++){
        
            
            int ny = y + dy[i], nx = x + dx[i];
            
            if(ny < 0 || ny >= h || nx < 0 || nx >= w)continue;
            if(seen[ny][nx])continue;
            if(s[ny][nx] == '#' || s[ny][nx] == '@')continue;
            res++;
            dfs(ny, nx);
        
    }
}

int main(){
    while(cin >> w >> h){
      if(w == 0 && h == 0)return 0;
      res = 0;
    int sy, sx;
    for(int i = 0; i < h; i++)cin >> s[i];
    for(int i = 0; i < h; i++)for(int j = 0; j < w; j++){
        if(s[i][j] == '@')sy = i, sx = j;
        seen[i][j] = false;
    }
    dfs(sy, sx);
    cout << res + 1 << endl;
    }
}

C++: int main()
{
  int W, H, i, j, f, d, count;
  int dx[4] = {0, 1, 0, -1};
  int dy[4] = {1, 0, -1, 0};
  char map[32][32];
  char s[32];

  while (1) {
    gets(s);
    sscanf(s, "%d %d", &W, &H); if (W == 0 && H == 0) { break; }
    for (i = 0; i < 30; i++) {
      strcpy(map[i], "##############################");
    }
    
    for (i = 0; i < H; i++) {
      gets(&map[i+1][1]);
      map[i+1][W+1] = '#';
      map[i+1][W+2] = '\0';
    }

    count = 1;
    while (1) {
      f = 0;
      for (i = 1; i <= H; i++) {
        for (j = 1; j <= W; j++) {
          if (map[i][j] == '@') {
            for (d = 0; d < 4; d++) {
              if (map[i+dy[d]][j+dx[d]] == '.') {
                map[i+dy[d]][j+dx[d]] = '@';
                count++;
                f = 1;
              }
            }
          }
        }
      }
      if (!f) {
        break;
      }
    }
    printf("%d\n", count);
  }
  return 0;
}

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

typedef pair<int, int> P;
int W, H;
char X[20][20];
bool V[20][20];
int DX[4] = {0, -1, 1, 0};
int DY[4] = {-1, 0, 0, 1};

int main() {
  while (cin >> W >> H) {
    if (W == 0 && H == 0) break;
    int sx = -1, sy = -1;
    for (int i=0; i<H; i++) {
      for (int j=0; j<W; j++) {
        cin >> X[j][i];
        V[j][i] = false;
        if (X[j][i] == '@') sx = j, sy = i;
      }
    }

    int c = 0;
    queue<P> q;
    q.push(P(sx, sy));

    while (!q.empty()) {
      int x = q.front().first,
          y = q.front().second; q.pop();
      if (V[x][y]) continue;
      V[x][y] = true;
      c++;

      for (int k=0; k<4; k++) {
        int dx = x + DX[k], dy = y + DY[k];
        if (dx < 0 || dx >= W || dy < 0 || dy >= H) continue;
        if (X[dx][dy] == '.') q.push(P(dx, dy));
      }
    }
    cout << c << "\n";
  }
  return 0;
}