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 <algorithm>
#include <functional>
#include <iostream>
#include <numeric>
#include <utility>
#include <vector>

using namespace std;

int main()
{
    while (true) {
        int w, h;
        cin >> w >> h;
        if (w == 0 && h == 0) {
            return 0;
        }
        vector<vector<char>> v(h, vector<char>(w, '\0'));
        pair<int, int> start;
        for (int i = 0; i < h; ++i) {
            for (int j = 0; j < w; ++j) {
                cin >> v[i][j];
                if (v[i][j] == '@') {
                    start = make_pair(i, j);
                }
            }
        }
        constexpr char REACHABLE = '$';
        function<void(int, int)> f = [&v, &f, w, h, REACHABLE](int i, int j) {
            // ?????¢????¶???????
            if (i == -1 || j == -1 || i == h || j == w) {
                return;
            }
            // ?§??????§?????????
            else if (v[i][j] == '#') {
                return;
            }
            // ????????°?????§??????
            else if (v[i][j] == REACHABLE) {
                return;
            }
            else {
                v[i][j] = REACHABLE;
                f(i - 1, j);
                f(i + 1, j);
                f(i, j - 1);
                f(i, j + 1);
            }
        };
        f(start.first, start.second);
        auto r = accumulate(begin(v), end(v), 0,
                            [REACHABLE](int z, const vector<char>& vv) {
                                return z + count(begin(vv), end(vv), REACHABLE);
                            });
        cout << r << endl;
    }
}

C++: #include <iostream>

using namespace std;

struct D
{
  bool movable[20][20] = {};
};

int const dir[][2] = {
  {  1, 0 },
  { -1, 0 },
  { 0,  1 },
  { 0, -1 }
};

void count( D& d, int& num, int const px, int const py )
{
  if ( !d.movable[px][py] )
    return;

  ++num;
  d.movable[px][py] = false;

  for( int i=0; i<4; ++i ) {
    int const npx = px + dir[i][0];
    int const npy = py + dir[i][1];
    if ( npx < 0 || npy < 0 || npx >= 20 || npy >= 20 )
      continue;
    count( d, num, npx, npy );
  }
}

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

    D d;
    int px, py;
    for( int y=0; y<h; ++y ) {
      for( int x=0; x<w; ++x ) {
        char c;
        cin >> c;
        if ( c == '@' ) {
          px = x;
          py = y;
        }

        d.movable[x][y] = ( c != '#' );
      }
    }

    int num = 0;
    count( d, num, px, py );
    cout << num << endl;
  }
}

C++: 

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<queue>
#include<stack>
using namespace std;

int w,h;
char map[21][21];
int dfs(int si,int sj){
    int ans=1;
    if(si<=-1||si>=h)return 0;
    if(sj<=-1||sj>=w)return 0;
    if(map[si][sj]=='#') return 0;
    map[si][sj]='#';
    ans+=dfs(si+1,sj);
    ans+=dfs(si,sj+1);
    ans+=dfs(si-1,sj);
    ans+=dfs(si,sj-1);
    return ans;
}


int main(){
    while(1){
        scanf("%d %d",&w,&h);
        if(w==0 && h==0)break;
        int i,j,si=0,sj=0;
        for(i=0;i<=h-1;i++){
        scanf("%s",map[i]);
        }
        for(i=0;i<=h-1;i++){
            for(j=0;j<=w-1;j++){
                if(map[i][j]=='@'){
                    si=i;
                    sj=j;
                }
            }
        }
        printf("%d\n",dfs(si,sj));
        }
    return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<stack>
#include<queue>
#include<string>
#include<set>
#include<map>
using namespace std;
#define rep(i,n) for(int i=0;(i)<(n);(i)++)
#define FOR(i,m,n) for(int i=(m);(i)<(n);(i)++)
typedef long long ll;

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

int W,H,res;
void dfs(int x,int y,vector<vector<char> >& m){
    if(m[y][x]=='#')return;
    res++;
    m[y][x]='#';
    rep(i,4){
        int nx=x+dx[i],ny=y+dy[i];
        if(nx>=0&&nx<W&&ny>=0&&ny<H){
            dfs(nx,ny,m);
        }
    }
}

int main(){
    while(cin >> W >> H,W|H){
        vector<vector<char> > m(H,vector<char>(W));
        rep(i,H)rep(j,W)cin >> m[i][j];
        res=0;
        rep(i,H)rep(j,W){
            if(m[i][j]=='@')dfs(j,i,m);
        }
        cout << res << endl;
    }
    return 0;
}

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

int solve(int W, int H) {
    vector<vector<char>> field(H + 2, vector<char>(W + 2, '#'));
    vector<vector<int>> cost(H + 2, vector<int>(W + 2));
    queue<pair<int, int>> que;
    for (int i = 1; i <= H; i++) {
        for (int j = 1; j <= W; j++) {
            cin >> field[i][j];
            if (field[i][j] == '@') {
                que.push(make_pair(i, j));
                cost[i][j] = 1;
            }
        }
    }
    int dx[] = {-1, 0, 1, 0};
    int dy[] = {0, 1, 0, -1};
    while (!que.empty()) {
        pair<int, int> p = que.front();
        que.pop();
        for (int i = 0; i < 4; i++) {
            int nx = p.second + dx[i];
            int ny = p.first + dy[i];
            if (field[ny][nx] == '#') continue;
            if (cost[ny][nx]) continue;
            cost[ny][nx] = 1;
            que.push(make_pair(ny, nx));
        }
    }
    int ret = 0;
    for (int i = 0; i < cost.size(); i++) {
        for (int j = 0; j < cost[i].size(); j++) {
            if (cost[i][j]) ret++;
        }
    }
    return ret;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int W, H;
    while (1) {
        cin >> W >> H;
        if (!W & !H) break;
        cout << solve(W, H) << endl;
    }
}