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>

typedef long long LL;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define DECIM8  fixed<<setprecision(8) 
#define SZ(a) int((a).size())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);

//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))

using namespace std;
int field[32][32];
int dfs(int x,int y){
  int dx[4]={-1,1,0,0},dy[4]={0,0,-1,1};
  if(field[x][y]==0) return 0;
  field[x][y]=0;
  int answer=1;
  REP(i,4) answer += dfs(x+dx[i],y+dy[i]);
  return answer;
}
int main(void)
{
  for(;;){
    int w,h;
    cin >> w >> h;
    if(!w&&!h) return 0;
    REP(i,32) REP(j,32) field[i][j]=0;
    vector<string> fi;
    fi.resize(h);
    REP(i,h) cin >> fi[i];
    REP(i,h) REP(j,w) if(fi[i][j]!='#') field[i+5][j+5]=1;
    REP(i,h) REP(j,w) if(fi[i][j]=='@')
      cout << dfs(i+5,j+5) << endl;
  }
}

C++: #include <iostream>
using namespace std;
char m[21][21];
int w,h,ans;
int dx[]={0,1,0,-1},dy[]={1,0,-1,0};

void solve(int x,int y) {
  m[y][x]='#';
  ans++;
  
  for(int i=0;i<4;i++) {
    int nx=x+dx[i],ny=y+dy[i];
    if(nx>=0 && nx<w && ny>=0 && ny<h && m[ny][nx]!='#') {
	solve(nx,ny);
    }
  }
}

int main() {
  int sx,sy;
  while(cin>>w>>h,w||h) {
    ans=0;
    for(int y=0;y<h;y++) {
      for(int x=0;x<w;x++) {
	cin>>m[y][x];
	if(m[y][x]=='@') {
	  sx=x;sy=y;
	}
      }
    }
    solve(sx,sy);
    cout<<ans<<endl;
  }

  return 0;
}

C++: //C++14 (Clang 3.8.0)

#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string>
#define rp(i,a,b) for(int (i)=(int)(a);i<(int)(b);++i)

typedef long long ll;

int mx[]={-1,1,0,0};
int my[]={0,0,-1,1};
using namespace std;
int n=0;
void dfs(int startx,int starty,vector<vector<bool> > &map){
    for(int i=0;i<4;i++){
        int x=startx+mx[i],y=starty+my[i];
        if(x>=0 and y>=0 and y<map.size() and x<map[0].size() and map[y][x]==true){
        map[y][x]=false;
        n++;
        dfs(x,y,map);}
        
    }
    
}

int main(){
    while(1){
        int x,y,startx,starty;
        vector<vector<bool> > map;
        cin>>x>>y;

        if(x==0 and y==0) return 0;
        for(int i=0;i<y;i++){
            vector<bool> tmp;
            string s;
            cin>>s;
            for(int j=0;j<x;j++){

                if(s[j]=='@') {startx=j;starty=i;tmp.push_back(true);}
                else if(s[j]=='.') tmp.push_back(true);
                else tmp.push_back(false);    
            }
            map.push_back((tmp));
        }
        n=0;
        dfs(startx,starty,map);
        cout<<n<<endl;
        
    }

    return 0;
};

C++: #include <bits/stdc++.h>

using namespace std;

int W, H;
int start_x = 0, start_y = 0;
int sum;

vector<vector<char>> makeroom(int w, int h) {
	vector<vector<char>> room(h);
	for(int i = 0; i < h; i++) {
		for(int j = 0; j < w; j++) {
			char tmp;
			cin >> tmp;
			if(tmp == '@') {
				start_x = j;
				start_y = i;
			}
			room[i].push_back(tmp);
		}
	}
	return room;
}

void dfs_run(vector<vector<char>>& room, vector<vector<bool>>& checker, int x, int y) {
	
	if(x != 0) {
		if(room[y][x - 1] == '.' and checker[y][x - 1]) {
			sum++;
			checker[y][x - 1] = false;
			dfs_run(room, checker, x - 1, y);
		}
	}
	if(x != W - 1) {
		if(room[y][x + 1] == '.' and checker[y][x + 1]) {
			sum++;
			checker[y][x + 1] = false;
			dfs_run(room, checker, x + 1, y);
		}
	}
	if(y != 0) {
		if(room[y - 1][x] == '.' and checker[y - 1][x]) {
			sum++;
			checker[y - 1][x] = false;
			dfs_run(room, checker, x, y - 1);
		}
	}
	if(y != H - 1) {
		if(room[y + 1][x] == '.' and checker[y + 1][x]) {
			sum++;
			checker[y + 1][x] = false;
			dfs_run(room, checker, x, y + 1);
		}
	}
}

void dfs(vector<vector<char>>& room) {
	vector<vector<bool>> checker;
	checker = vector<vector<bool>>(room.size(), vector<bool>(room[0].size(), true));

	dfs_run(room, checker, start_x, start_y);
}


int main(void) {
	while(1) {
		cin >> W >> H;
		if(W == 0 and H ==0) {
			break;
		}

		vector<vector<char>> room = makeroom(W, H);
		sum = 0;
		dfs(room);

		cout << sum + 1 << endl;
	}
	return 0;
}

C++: #include<iostream>

using namespace std;
using P = pair<int, int>;

int map[22][22];

P dirs[] = {P(1, 0), P(-1, 0), P(0, 1), P(0, -1)};

int dfs(int h, int w) {
    if (!map[h][w]) return 0;
    map[h][w] = 0;
    int ans = 0;
    for (P dir : dirs) {
        ans += dfs(h + dir.first, w + dir.second);
    }
    return ans + 1;
}

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

        for (int i = 0; i < 22; ++i)
            for (int j = 0; j < 22; ++j)
                map[i][j] = 0;
        
        int pw, ph;
        for (int i = 1; i <= h; ++i) {
            for (int j = 1; j <= w; ++j) {
                char c;
                cin >> c;
                if (c == '.') map[i][j] = 1;
                if (c == '@') { 
                    map[i][j] = 1;
                    ph = i;
                    pw = j;
                }
            }
        }
        cout << dfs(ph, pw) << endl;
    }
}