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 <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <queue>
#include <string>
using namespace std;

#define rep(i,n) for(int i=0; i<n; i++)
#define repa(i,s,e) for(int i=s; i<=e; i++)
#define repd(i,s,e) for(int i=s; i>=e; i--)

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pii;

int W, H, x, y;
string f[20];
char v[20][20];

int ofs[4][2] = { {1, 0}, {-1, 0}, {0, 1}, {0, -1} };

int bfs() {
	int n = 0;
	memset(&v[0][0], 0, sizeof(char) * 20 * 20);

	queue<pii> que;
	que.push(pii(x, y));
	while(!que.empty()) {
		pii p = que.front();
		que.pop();
		if(v[p.second][p.first] == 0) {
			v[p.second][p.first] = 1;
			n++;
		} else {
			continue;
		}

		rep(i,4) {
			int nx = p.first + ofs[i][0];
			int ny = p.second + ofs[i][1];
			if(nx >= 0 && ny >= 0 && nx < W && ny < H && v[ny][nx] == 0 && f[ny][nx] == '.') {
				que.push(pii(nx, ny));
			}
		}
	}
	return n;
}

int main() {
	while(cin>>W>>H, W|H) {
		rep(i,H) {
			cin >> f[i];
			int j = f[i].find_first_of('@');
			if(j != string::npos) {
				x = j;
				y = i;
			}
		}
		cout << bfs() << endl;
	}
}

C++: #include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<functional>
#include<string>
#include<cstring>
#include<cmath>
using namespace std;
int cnt;
int w,h;
char map[24][24];
void func(int y,int x)
{
    cnt++;
    map[y][x]='#';
    int ny,nx;
    int dx[]={0,0,1,-1};
    int dy[]={1,-1,0,0};
    for(int i=0;i<4;i++)
    {
        ny=y+dy[i];
        nx=x+dx[i];
        if(map[ny][nx]=='.'&&0<=ny&&0<=nx&&ny<h&&nx<w)func(ny,nx);
    }
    return;
}
int main()
{
    while(cin>>w>>h,w)
    {
        cnt=0;
        int x,y;
        for(int i=0;i<24;i++)
        {
            for(int j=0;j<24;j++)
            {
                map[i][j]='#';
            }
        }
        for(int i=0;i<h;i++)
        {
            for(int j=0;j<w;j++)
            {
                cin>>map[i][j];
                if(map[i][j]=='@')
                {
                    y=i;
                    x=j;
                }
            }
        }
        func(y,x);
        cout<<cnt<<endl;
    }
    return 0;
}

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

#define rep(i, n) for(int i = 0 ; i < n ; i++)
using namespace std;
void dfs(int x, int y);

int cnt = 0;
string data[20];
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
int w, h;

int main()
{
    while(true){
      cin >> w >> h;
      if(w == 0 && h == 0) break;
      
      int sx, sy;
      for(int i = 0 ; i < h ; i++){
	cin >> data[i];
	for(int j = 0 ; j < w ; j++){
	  if(data[i][j] == '@'){
	    sx = j;
	    sy = i;
	  }
	}
      }
      dfs(sx, sy);
      cout << cnt << '\n';
      cnt = 0;
  }
}

void dfs(int x, int y)
{
  data[y][x] = '#';
  cnt++;

  rep(i, 4){
    int nx = x + dx[i], ny = y + dy[i];
    if(0 <= nx && nx < w && 0 <= ny && ny < h && data[ny][nx] == '.'){
      dfs(nx, ny);
    }
      }
}

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


int map[50][50] = {};
int w,h;
int ans;
int dx[4] = {0,-1,0,1};
int dy[4] = {-1,0,1,0};

void search(int y, int x){

  map[y][x] = 0;
  for(int t = 0; t < 4 ; t++){ 
    int ny = y + dy[t], nx = x + dx[t]; 
    if(nx >= 0 && ny >= 0 && nx < w && ny < h && map[ny][nx] == 1){
      //cout << "IN ";
      ans++;
      search(ny,nx);
    }
  }
}


int main(){
  
  while(1){
    for(int j = 0; j < 50 ; j++){
      for(int i = 0; i < 50 ; i++){
	map[j][i] = 0;
      }
    }
    
    ans = 1;

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

    int stx,sty;
    for(int k = 0; k < h ; k++){
      string s;
      cin >> s;
      for(int i = 0; i < w ; i++){
	if(s[i] == '.') map[k][i] = 1;
	if(s[i] == '#') map[k][i] = 0;
	if(s[i] == '@') {
	  map[k][i] = 2;
	  stx = i;
	  sty = k;
	}
      }
    }


 
    //竪臓即巽造尊
    /*
    for(int j = 0; j < h ; j++){
      for(int i = 0; i < w ; i++){
	cout << map[j][i];
      }
      cout << endl;
    }
    cout << endl;
    */
    search(sty,stx);
    cout << ans << endl;
	  
  }
  return 0;
}

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

// #define ll long long
#define vvi vector< vector<int> >
#define All(X) X.begin(),X.end()
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define pb push_back

int w,h,fx,fy;
char ban[30][30];
int dx[4] = {-1,0,1,0};
int dy[4] = {0,-1,0,1};

bool inrange(int px,int py){
  if(px<0||py<0||px>=h||py>=w) return false;
  else return true;
}
int bsearch(int px,int py){
  int cnt = 0;
  REP(i,4){
    int nx = px+ dx[i]; int ny = py+dy[i];
    if(!inrange(nx,ny)) continue;
    if(ban[nx][ny]=='#') continue;
    //cnt++;
    //cout << nx << " " <<ny<<endl;
    if(ban[nx][ny]=='.'){
      ban[nx][ny]='!';
      cnt++;
      cnt += bsearch(nx,ny);
    }
  }
  return cnt;
}

int main(){
  while(1){
    cin >> w >> h;
    if((w==0)&&(h==0)) break;
    REP(i,h){
      char buf[21];
      cin >> buf;
      REP(j,w) {
        ban[i][j] = buf[j];
        if(ban[i][j] == '@'){
          fx = i; fy = j;
        }
      }
    } 
    cout << bsearch(fx,fy)+1 << endl;
    REP(i,h){
      REP(j,w){
        cout << ban[i][j];
      }
      cout << endl;
    }
  }
  return 0;
}