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[i].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 <iostream>

using namespace std;

#define min(a, b) ((a)<(b)? (a):(b))
#define max(a, b) ((a)>(b)? (a):(b))
#define rep(i, n) REP(i, 0, (n))
#define REP(i, a, n) for(typeof(n) i=(a); i<(n); i++)

int H, W;
char t[30][30];
bool visited[30][30];

int dfs(int i, int j, int cnt) {
    if(i < 0 || j < 0 || i >= H || j >= W) return 0;
    if(visited[i][j]) return 0;
    if(t[i][j] == '#') return 0;
    
    visited[i][j] = true;
    
    return 1 + dfs(i+1, j, cnt) + dfs(i-1, j, cnt) + dfs(i, j+1, cnt) + dfs(i, j-1, cnt);
}

int main() {
	while(true) {
        cin >> W >> H;
        if(W == 0 && H == 0) break;
        
        int p, q;
        rep(i, H) {
            rep(j, W) {
                cin >> t[i][j];
                visited[i][j] = false;
                if(t[i][j] == '@') {p = i; q = j;}
            }
        }
        cout << dfs(p, q, 0) << endl;
	}
}

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

int w, h, black;
char tile[24][24];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

void dfs(int x, int y){
    black++;
    tile[x][y] = '#';
    for(int i = 0; i < 4; i++){
        if( x+dx[i] >= 0 && x+dx[i] < h &&
           y+dy[i] >= 0 && y+dy[i] < w &&
           tile[ x+dx[i] ][ y+dy[i] ] == '.' ){
            dfs( x+dx[i], y+dy[i] );
        }
    }
}

int main(){
    int i, j;
    while( cin >> w >> h, w||h ){
        for(i=0;i<w;i++){
            for(j = 0; j < h; j++){
                cin >> tile[i][j];
            }
        }
        black = 0;
        for(i = 0; i < h && !black; i++){
            for(j = 0; j < w && !black; j++){
                if( tile[i][j] == '@' ){
                    dfs(i, j);
                }
            }
        }
        cout << black << endl;
    }
    return 0;
}

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

bool map[20][20];//true??????
int W,H,X,Y;
int ct;
const int dx[] = {1,0,-1,0};
const int dy[] = {0,1,0,-1};

bool valid(int x,int y) {
	return 0 <= x && x < W && 0 <= y && y < H && map[x][y];
}

void dfs(int x,int y) {
	map[x][y] = false;
	ct++;
	for(int i = 0;i < 4;i++) {
		if(valid(x + dx[i],y + dy[i])) dfs(x + dx[i],y + dy[i]);
	}
}

int main() {
	while(cin >> W >> H && !(W == 0 && H == 0)) {
		ct = 0;
		for(int y = 0;y < H;y++) {
			char c;
			for(int x = 0;x < W;x++) {
				cin >> c;
				map[x][y] = !(c == '#');
				if(c == '@') {
					X = x;
					Y = y;
				}
			}
		}
		dfs(X,Y);
		cout << ct << endl;
	}
}

C++: #include <iostream>
#include <queue>
#include <array>

const int kSize = 20 + 2;

std::array<int, 4> ways = {-1, -kSize, +1, +kSize};

inline int xy2pos(const int x, const int y){
    return x + y * kSize;
}

std::array<bool, kSize*kSize> Bfs(const std::array<bool, kSize*kSize> &tiles, int pos){
    std::array<bool, kSize*kSize> arrived;
    std::queue<int> q;
    q.push(pos);
    std::fill(arrived.begin(), arrived.end(), false);
    while(!q.empty()){
        pos = q.front();
        q.pop();
        arrived[pos] = true;
        for(int way : ways)
            if(tiles.at(pos + way) && !arrived.at(pos + way))
                q.push(pos + way);
    }
    return arrived;
}

int main(){
    int w,h;
    std::array<bool,kSize*kSize> tiles; ///< true if black
    while((std::cin >> w >> h) && w != 0){
        std::fill(tiles.begin(), tiles.end(), false);
        int pos;
        for(int y=1; y<=h; ++y){
            for(int x=1; x<=w; ++x){
                char c;
                std::cin >> c;
                tiles[xy2pos(x,y)] = (c == '.');
                if(c == '@'){
                    pos = xy2pos(x,y);
                    tiles[pos] = true;
                }
            }
        }
        auto arrived = Bfs(tiles, pos);
        int sum = 0;
        for(bool v : arrived)
            sum += (v) ? 1 : 0;
        std::cout << sum << std::endl;
    }
}

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

using ll=long long;
#define int ll

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

int W,H;
char data[30][30];
bool come[30][30];
int human_h,human_w;

int read(){
	int i;
	scanf("%lld",&i);
	return i;
}

int canGo(int w,int h){
	if(w < 0 || h < 0 || w >= W || h >= H) return 0;
	if(data[h][w] == '#') return 0;
	if(come[h][w]) return 0;
	come[h][w] = true;
	int ret = 1;
	ret += canGo(w+1,h);
	ret += canGo(w-1,h);
	ret += canGo(w,h+1);
	ret += canGo(w,h-1);
	return ret;
}

signed main(){
	// your code goes here
	while(true){
		W=read(),H=read();
		if(W==0 || H==0) break;
		REP(i,H){
			REP(j,W){
				come[i][j] = false;
				cin >> data[i][j];
				if(data[i][j] == '@'){
					human_h = i;
					human_w = j;
				}
			}
		}
		cout <<	canGo(human_w,human_h) << endl;
	}

}