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<cstdio>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;
//Global
int x, y;
const int MAX_N = 20;
char maze[MAX_N][MAX_N+1];
const int INF = 1000000;
int X, Y;//"@"???????????????
int memo[MAX_N][MAX_N];
int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,1,0,-1 };
int counter;

void bsd(int a, int b) {
	maze[b][a] = '#';
	memo[b][a] = INF;
	counter++;
	for (int i = 0; i < 4; i++) {
		int nx = a + dx[i];
		int ny = b + dy[i];
		if (memo[ny][nx] == 0 && maze[ny][nx] == '.' && 0 <= nx && nx < x && 0 <= ny && ny < y) {
			bsd(nx, ny);
		}
	}

}



//solve
//??±???????????¢?´¢??§"@"??¨???????????£?????????"."????????°????±???????
void solve()
{
	//memo????????????
	memset(memo.0, sizeof(memo));
	//for (int i = 0; i < y; i++) {
		//for (int j = 0; j < x; j++) {
			//memo[i][j] = 0;
	//	}
	//}

	//"@"??¨??£??\????????????  ??????  INF??§??????  ??????  ????????????????????£?????????????????????"."??§??????
	counter = 0;//"@"??????
	bsd(X, Y);


	cout << counter << endl;

}


//main
int main()
{
	while (true) {
		cin >> x >> y;
		if (x == 0 && y == 0)break;
		//"W"??¨"."??¨"@"?????\???
		for (int i = 0; i < y; i++) {
			for (int j = 0; j < x; j++) {
				cin >> maze[i][j];
				if (maze[i][j] == '@') {
					X = j; Y = i;//@???????????????????¨????
				}
			}
		}
		solve();
	}
    return 0;
}

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

int moved = 0;
int w, h;
char myMap[20][20];

bool safe(int x, int y){
    return x < h && y < w;
}

void move(int x, int y){
    myMap[x][y] = '#';
    moved++;
    if (myMap[x+1][y] == '.' && safe(x+1, y)) move(x+1, y);
    if (myMap[x-1][y] == '.' && safe(x-1, y)) move(x-1, y);
    if (myMap[x][y+1] == '.' && safe(x, y+1)) move(x, y+1);
    if (myMap[x][y-1] == '.' && safe(x, y-1)) move(x, y-1);
    return;
}

int main(){
    while (1){
	cin >> w >> h;
	if (w == 0 && h == 0) break;
	moved = 0;
	int initX, initY;
	
	for (int i = 0; i < h; i++){
	    for (int j = 0; j < w; j++){
		cin >> myMap[i][j];
		if (myMap[i][j] == '@'){
		    initX = i; initY = j;
		}
	    }
	}
	
	move(initX, initY);
	
	cout << moved << endl;
    }
}

C++: #include <iostream>
using namespace std;
char mas[25][25];
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};
int w,h,sx,sy,cnt;
void dfs(int,int);

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

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

C++: #include <iostream>
#include <string>
#include <utility>
#include <stack>
#include <vector>
#include <queue>
#include <algorithm>
#include <map>
#include <climits>
#include <set>
#include <numeric>
#include <iomanip>
#include <cmath>
#include <cstring>
using namespace std;
string grid[20];
bool used[20][20];
int move_h[4] = {-1, 1, 0, 0};
int move_w[4] = {0, 0, -1, 1};

void dfs(int cur_h, int cur_w, int h, int w){
  used[cur_h][cur_w] = true;
  for(int i = 0; i < 4; i++){
    int next_h = cur_h + move_h[i];
    int next_w = cur_w + move_w[i];
    if(0 <= next_h && next_h < h && 0 <= next_w && next_w < w
        && !used[next_h][next_w] && grid[next_h][next_w] != '#'){
      dfs(next_h, next_w, h, w);
    }
  }
}

int main(){
  while(true){
    int W;
    int H;
    cin >> W >> H;
    if(W == 0 && H == 0){
      break;
    }
    for(int i = 0; i < H; i ++){
      cin >> grid[i];  
    }

    int start_h = 0;
    int start_w = 0;
    for(int i = 0; i < H; i++){
      for(int j = 0; j < W; j++){
        used[i][j] = false;
        if(grid[i][j] == '@'){
          start_h = i;
          start_w = j;
        }
      }
    }

    dfs(start_h, start_w, H, W);

    int ans = 0;
    for(int i = 0; i < H; i++){
      for(int j = 0; j < W; j++){
        if(used[i][j]){
          ans ++;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}