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<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<stack>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<iostream>
#include<set>
#include<map>

using namespace std;
typedef long long ll;
int mod(ll a){
    int c=a%1000000007;
    if(c>=0)return c;
    else return c+1000000007;
}
typedef pair<int,int> i_i;
typedef pair<ll,ll> l_l;
#define inf 100000000/*10^8*/

/////////////////////////////////

int w,h;
int si,sj;

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

int room[25][25];
int ans=0;
int dfs(int x,int y){
    room[x][y]=2;ans++;
    for(int i=0;i<=3;i++){
        int xx=x+dx[i],yy=y+dy[i];
        if(0<=xx&&xx<=h-1&&0<=yy&&yy<=w-1&&room[xx][yy]==0)dfs(xx,yy);
    }
    return ans;
}


int main(){
    while(1){
        cin>>w>>h;
        if(w==0)break;
        ans=0;
        for(int i=0;i<=h-1;i++){
            char s[w+1];cin>>s;
            for(int j=0;j<=w-1;j++){
                if(s[j]=='.')room[i][j]=0;
                else if(s[j]=='#')room[i][j]=1;
                else if(s[j]=='@')room[i][j]=2,si=i,sj=j;
            }
        }
       /* cout<<dfs(si,sj)<<endl;*/
    }
    return 0;
}

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

using P = pair<ll, ll>;

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

int dfs(vector<string> const& v, vector<vector<bool>>& visited, int y, int x) {
    visited[y][x] = true;
    int res = 1;
    for(int i=0; i<4; ++i) {
        int ny = y + dy[i], nx = x + dx[i];
        if(ny < 0 || v.size() <= ny || nx < 0 || v[0].size() <= nx || v[ny][nx] == '#' || visited[ny][nx]) {
            continue;
        }
        res += dfs(v, visited, ny, nx);
    }
    return res;
}


int main() {
    int w, h;
    while(cin >> w >> h, w) {
        vector<string> v(h);
        for(int i=0; i<h; ++i) {
            cin >> v[i];
        }
        int sy, sx;
        for(int i=0; i<h; ++i) {
            for(int j=0; j<w; ++j) {
                if(v[i][j] == '@') {
                    sy = i;
                    sx = j;
                }
            }
        }
        vector<vector<bool>> visited(h, vector<bool>(w));
        cout << dfs(v, visited, sy, sx) << endl;
    }
}

C++: #include<iostream>
#include<map>
#include<string>
#include<algorithm>
using namespace std;
int sum = 1;
void solve(bool a[21][21], int i, int j,int n,int m) {
	if (i + 1 < n&&a[i + 1][j]) {
		sum++;
		a[i][j] = false;
		solve(a,i+1,j, n, m);
	}
	if (i - 1 >= 0 && a[i - 1][j]) {
		sum++;
		a[i][j] = false;
		solve(a, i-1, j, n, m);
	}
	if (j + 1 < m&&a[i][j + 1]) {
		sum++;
		a[i][j] = false;
		solve(a, i, j+1, n, m);
	}
	if (j - 1 >= 0 && a[i][j - 1]) {
		sum++;
		a[i][j] = false;
		solve(a, i, j-1, n, m);
	}
}
int main() {
	int n, m;
	while (cin >> m >> n&&n != 0) {
		bool a[21][21];
		int b[2];
		for (int i = 0; i < n; i++) {
			string u;
			cin >> u;
			for (int j = 0; j < m; j++) {
				if (u[j] == '@') {
					a[i][j] = true;
					b[0] = i;
					b[1] = j;
				}
				else if (u[j] = '.')
					a[i][j] = true;
				else
					a[i][j] = false;
			}
		}
		sum = 1;
		solve(a, b[0], b[1],n,m);
		cout << sum << endl;
	}
}

C++: #include<iostream>
#include<string>
using namespace std;
int ans;
int w,h;
string map[40];
int vec[4][2]={{0,1},{0,-1},{-1,0},{1,0}};
void move(int x,int y){
	ans++;
	map[x][y]='#';
	for(int i=0;i<4;i++){
		if((x+vec[i][0]>=0)&&(y+vec[i][1]>=0)&&(x+vec[i][0]<h)&&(y+vec[i][1]<w)){
			if(map[x+vec[i][0]][y+vec[i][1]]=='.'){
				move(x+vec[i][0],y+vec[i][1]);
			}
		}
	}
	return;
}

int main(){
	while(cin>>w>>h,w){
		ans=0;
		int fx,fy;
		for(int i=0;i<h;i++){
			cin>>map[i];
		}
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(map[i][j]=='@'){
					fx=i;
					fy=j;
					break;
				}
			}
		}
		move(fx,fy);
		cout<<ans<<endl;
		
	}
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)n; i++)
#define PI 3.14159265358979323846
#define vll vector<long long>
#define vvll vector<vector<long long>>
using namespace std;
using ll = long long;
using ld = long double;

// ----本問について----
// 問題：Red and Black
// 問題文URL：https://onlinejudge.u-aizu.ac.jp/problems/1130
// 所要時間：
// 学んだこと：
// ------------------

// ----ここから本問のソースコード----
const ll dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

// グローバル変数（入力）
ll w, h, state[25][25], tiles[25][25];
pair<ll, ll> start;

// 入力
void input() {
  rep(y, h) {
    string s;
    cin >> s;
    rep(x, w) {
      if (s[x] == '.') {
        tiles[y][x] = 0;
      } else if (s[x] == '#') {
        tiles[y][x] = 1;
      } else {
        tiles[y][x] = 0;
        start = make_pair(x, y);
      }
      // cout << tiles[y][x];
      // if (x != w-1){cout << " ";}
    }
  }
}

// (x,y)にいけるかどうか
bool valid(ll x, ll y) {
  if ((0 <= x && x < w) && (0 <= y && y < h)) {
    if (tiles[y][x] == 0) { return true; }
  }
  return false;
}

// 深さ優先探索
void dfs(ll x, ll y) {
  state[y][x] = 1; // 訪問済み
  // cout << "x:" << x << " y:" << y << endl;
  rep(i, 4) {
    ll x2 = x + dx[i];
    ll y2 = y + dy[i];
    if (valid(x2, y2) && state[y2][x2]==0) { 
      dfs(x2, y2); 
    }
  }
}

void solve() {
  // state初期化
  rep(i, h) {
    rep(j, w) { state[i][j] = 0; }
  }

  // 始点から探索
  dfs(start.first, start.second);

  // 出力
  ll ans = 0;
  rep(i, h) {
    rep(j, w) {
      if (state[i][j] == 1) { ans++; }
    }
  }
  cout << ans << endl;
}

int main() {
  while (cin >> w >> h && w > 0) {
    input();
    solve();
  }
  return 0;
}