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>
using namespace std;

#define ll long long
#define vvi vector<vector<int>>
#define vec vector
#define pq priority_queue
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (int (i) = (0); (i) < (n); ++(i))
#define repp(i, m, n) for (int (i) = (m); (i) < (n); ++(i))
#define debug(x) cerr << #x << ": " << x << endl;
#define debug2(x, y) cerr<<"("<<#x<<", "<<#y<<") = "<<"("<<x<<", "<<y<<")"<<endl;
#define debug3(x, y, z) cerr<<"("<<#x<<", "<<#y<<", "<<#z<<") = "<<"("<<x<<", "<<y<<", "<<z<<")"<<endl;
#define debugB(x, y) cerr<<#x<<": "<<bitset<y>(x) << endl;
#define line() cerr << "---------------" << endl;

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

template<typename T>
void printA(vector<T> &printArray, char between = ' ') {
   int paSize = printArray.size();
   for (int i = 0; i < paSize; i++) { cerr << printArray[i] << between;}
   if (between != '\n') {cerr << endl;}
}

// ------------------------------------------------------------------------------------------

int W, H;
char B[30][30];

int dfs(int y, int x) {
   int ret = 1;
   B[y][x] = '#';
   rep (i, 4) {
      int nx = x + dx[i], ny = y + dy[i];
      if (nx < 0 || nx >= W || ny < 0 || ny >= H || B[ny][nx] == '#') continue;
      ret += dfs(ny, nx);
   }
   return ret;
}

void solve() {
   rep (i, H) {
      cin >> B[i];
   }
   rep (i, H) {
      rep (j, W) {
         if (B[i][j] == '@') {
            cout << dfs(i, j) << endl;
            return;
         }
      }
   }
}

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

   return 0;
}

C++: //Shunji Lin 08-144505
#include <iostream>
#include <string>

using namespace std;

int reachable = 0;

void DFS(int grid[20][20], int visited[20][20],  int row_pos, int col_pos, int H, int W);

void DFS_aux(int grid[20][20], int visited[20][20], int row_pos, int col_pos, int H, int W) {
  if (grid[row_pos][col_pos] && !visited[row_pos][col_pos]) {
    DFS(grid, visited, row_pos, col_pos, H, W);
  }
    
}
	 
void DFS(int grid[20][20], int visited[20][20], int row_pos, int col_pos, int H, int W) {
  visited[row_pos][col_pos] = true;
  reachable += 1;
  if (!(row_pos == 0)) {
    DFS_aux(grid, visited, row_pos-1, col_pos, H, W); //up
  }
  if (!(col_pos == 0)) {
    DFS_aux(grid, visited, row_pos, col_pos-1, H, W); //left
  }
  if (!(row_pos == H-1)) {
    DFS_aux(grid, visited, row_pos + 1, col_pos, H, W); //down
  }
  if (!(col_pos == W-1)) {
    DFS_aux(grid, visited, row_pos, col_pos + 1, H , W); //right
  }
}
      
    


int main() {
  while (true) {
    int grid[20][20] = {{false}};
    int visited[20][20] = {{false}};
    reachable = 0;
    int man_row = -1, man_col = -1;
    int W, H;
    cin >> W >> H;
    if (W == 0 && H == 0) break;
    for (int i = 0; i < H; i++) {
      string line;
      cin >> line;
      for (int j = 0; j < W; j++) {
	if (line[j] == '@') {
	  grid[i][j] = true;
	  man_row = i;
	  man_col = j;
	} else if (line[j] == '.') {
	  grid[i][j] = true;
	}
      }
    }
    /*
    //print out grid
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
	if (grid[i][j] == true) {
	  cout << 'b';
	} else if (grid[i][j] == false) {
	  cout << 'r';
	} 
      }
      cout << endl;
    }
    */
    
    DFS(grid, visited,  man_row, man_col, H, W );
    cout << reachable << endl;
  }
}

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

//#define var auto
#define ll long long

using namespace std;

int ans = 0;
int h, w;
vector<string> grid;
vector<vector<bool>> arrived;
void dfs(int y, int x) {
	if (y < 0 || h <= y || x < 0 || w <= x) return;
	if (grid[y][x] == '#') return;
	if (arrived[y][x])return;
	arrived[y][x] = true;
	ans++;
	dfs(y - 1, x);
	dfs(y, x + 1);
	dfs(y + 1, x);
	dfs(y, x - 1);
}

int main() {
	while (true)
	{
		cin >> w >> h;
		if (w == 0) break;
		grid = vector<string>(h);
		arrived = vector<vector<bool>>(20, vector<bool>(20, false));
		ans = 0;
		for (int i = 0; i < h; i++) {
			cin >> grid[i];
		}
		int sy, sx;
		for (int i = 0; i < h; i++) {;
			for (int j = 0; j < w; j++) {
				if (grid[i][j] == '@') {
					sy = i;
					sx = j;
				}
			}
		}
		dfs(sy, sx);
		cout << ans << endl;
	}
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <functional>
using namespace std;

#define Rep(b, e, i) for(int i = b; i <= e; i++)
#define Repr(e, b, i) for(int i = e; i >= b; i--)
#define rep(n, i) Rep(0, n-1, i)
#define repr(n, i) Repr(n-1, 0, i)
#define all(v) (v).begin(), (v).end()
#define pb(x) push_back(x)
#define uniq(x) (x).erase(unique(all(x)),(x).end())

typedef long long ll;
typedef pair<ll, ll> llP;
typedef pair<int, int> intP;
typedef std::priority_queue<int> IntPrioQueue; //Z->A
typedef std::priority_queue<int, std::vector<int>, std::greater<int> > IntReversePrioQueue; //A->Z

const int MAX = 32;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = 1<<29;
const int MOD = 1000000007;

int w, h, sx, sy, cnt;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int maze[MAX][MAX];

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

void dfs(int x, int y){
    maze[y][x] = 0;
    cnt++;
    rep(4, d) {
        if (range(x+dx[d], y+dy[d]) && maze[y+dy[d]][x+dx[d]]) {
            dfs(x+dx[d], y+dy[d]);
        }
    }
    return;
}

void solve(void){
    while (1) {
        cin >> w >> h;
        if (w == 0 && h == 0) break;
        cnt = 0;
        rep(h, y) rep(w, x) {
            char c; cin >> c;
            if (c == '#') maze[y][x] = 0;
            else maze[y][x] = 1;
            if (c == '@') {
                sx = x; sy = y;
            }
        }
        dfs(sx, sy);
        printf("%d\n", cnt);
    }
}

int main(void){
  solve();
  return 0;
}