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++: // ??±???????????¢?´¢(DFS:Depth First Search)
// ICPC Domestic 2004
//
#include <iostream>
using namespace std;

#define LIM 20
int w,h,sx,sy,ans;
bool field[LIM][LIM];
char buff[LIM];
bool flg=true;

void input(void);
void dump(void);
void dfs(int, int);
void solve();


int main(void)
{
    while(true){
        input();
        if(!flg)break; // ????????????????????????????¨??????§????????°??????????????????
        //dump();
        dfs(sx,sy);
        solve();
    }
    return 0;
}

void input(void){
    cin >> w >> h;
    if(w==0 && h==0){
        flg=false;
        return;
    }
    for(int i=0;i<h;++i){
        cin >> buff;
        for(int j=0;j<w;++j){
            if(buff[j]=='#')field[i][j]=false; // ????????????
            else field[i][j]=true; // ?????????
            if(buff[j]=='@'){
                sy=i;
                sx=j;
            }
        }
    }
    ans=0; //?§£????????????????????????????????¨????????????
    return;
}

void dump(void){
    cout << "sx:" << sx << " sy:" << sy << endl;
    for(int i=0;i<h;++i){
        for(int j=0;j<w;++j){
            cout << field[i][j];
        }
        cout << endl;
    }
}

void dfs(int x,int y){
    if(!field[y][x] || x<0 || y<0 || x>=w || y>=h) //????????¢?????????????????´????????´???
        return;
    else{
        field[y][x]=false;
        ++ans;
        dfs(x-1,y); //???
        dfs(x,y-1); //???
        dfs(x+1,y); //???
        dfs(x,y+1); //???
    }
    return;
}

void solve(){
    cout << ans << endl;
}

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

using namespace std;

#define all(c) (c).begin(), (c).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

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

int W, H;
string s[30];
using P = pair<int, int>;
void solve() {
    P init;
    rep(i, H) rep(j, W) if (s[i][j] == '@') init = P(i, j);
    set<P> visited;
    queue<P> que;
    que.push(init);
    visited.insert(init);

    while (que.size()) {
        auto p = que.front();
        que.pop();
        rep(k, 4) {
            auto np = p;
            np.first += dy[k];
            np.second += dx[k];
            if (np.first < 0 || np.second < 0) continue;
            if (H <= np.first || W <= np.second) continue;
            if (visited.count(np)) continue;
            if (s[np.first][np.second] == '#') continue;

            que.push(np);
            visited.insert(np);
        }
    }
    cout << visited.size() << endl;
}
int main() {
    while (cin >> W >> H, W) {
        rep(i, H) cin >> s[i];
        solve();
    }

    return 0;
}

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

char map[21][21];
int count;
int x, y;

void dfs(int nx, int ny){
  map[ny][nx] = '#';
  count++;
  if (nx < x && map[ny][nx+1] == '.'){
    dfs(nx+1, ny);
  }
  if (nx > 0 && map[ny][nx-1] == '.'){
    dfs(nx-1, ny);
  }
  if (ny < y && map[ny+1][nx] == '.'){
    dfs(nx, ny+1);
  }
  if (ny > 0 && map[ny-1][nx] == '.'){
    dfs(nx, ny-1);
  }
  return;
}

int main(){
  while (1){
    cin >> x >> y;
    if (!(x || y)) break;
    int ix, iy;
    count = 0;
    for (int i = 0; i < y; i++){
      for (int j = 0; j < x; j++){
	cin >> map[i][j];
	if (map[i][j] == '@'){ix = j; iy = i;}
      }
    }
    dfs(ix, iy);
    cout << count << endl;
  }
}

C++: #include<iostream>
#include<iomanip>
#include<string>
#include<cstdlib>
#include<cmath>
#include<algorithm>

using namespace std;

int H, W;
int table[20][20];
int cnt;

void calc(int current_x, int current_y) {
	table[current_x][current_y] = 3;

	if(current_x > 0 && table[current_x-1][current_y] == 1)
		calc(current_x-1, current_y);
	if(current_y > 0 && table[current_x][current_y-1] == 1)
		calc(current_x, current_y-1);
	if(current_x < W-1 && table[current_x+1][current_y] == 1)
		calc(current_x+1, current_y);
	if(current_x < H-1 && table[current_x][current_y+1] == 1)
		calc(current_x, current_y+1);

	cnt++;
}


int main() {
	while(1) {
		cnt = 0;
		cin >> W >> H;
		if(H == 0 && W == 0) break;

		char temp;
		int current[2];
		for(int i=0; i<H; i++) {
			for(int j=0; j<W; j++) {
				cin >> temp;
				if(temp=='.') table[i][j] = 1;
				else if(temp=='#') table[i][j] = 2;
				else if(temp=='@') {
					table[i][j] = 0;
					current[0] = i;
					current[1] = j;
				}
			}
		}

		calc(current[0], current[1]);

		cout << cnt << endl;
	}

	return 0;
}

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

int w, h;
char tiles[20][21];

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

bool isRegion(int x, int y){
    return x >= 0 && x < w && y >= 0 && y < h;
}

int main(){
    while(1){
        cin >> w >> h;
        if(!w && !h) break;
        
        int sx, sy;
        for(int i = 0; i < h; i++){
            cin >> tiles[i];
            for(int j = 0; j < w; j++){
                if(tiles[i][j] == '@'){
                    sx = j; sy = i;
                }
            }
        }
        
        int cnt = 0;
        queue<pair<int, int> > q;
        q.push(make_pair(sx, sy));
        while(!q.empty()){
            cnt++;
            int x = q.front().first, y = q.front().second;
            q.pop();
            
            for(int i = 0; i < 4; i++){
                int nx = x + dx[i], ny = y + dy[i];
                
                if(isRegion(nx, ny) && tiles[ny][nx] == '.'){
                    tiles[ny][nx] = '#';
                    q.push(make_pair(nx, ny));
                }
            }
        }
        cout << cnt << endl;
        
    }
}