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 <string>
#include <algorithm>
#include <vector>

using namespace std;

// int count(int a, int b, int ans, int sikaku){
//   for(int i=-1;i<1;i++){
//     for(int j=-1;j<1;j++){
//       if(sikaku[a+i][b+j] == 1){
// 	sikaku[a+i][b+j] = 10;
// 	sikaku = count(a+i,b+j,ans,sikaku);
//       }
//     }
//   }

//   return sikaku;
// }

int main(){

  int w,h;
  int ans;
  char aaa;
  int a,b;

  // while(cin >> w >> h){
  //   if(w==0&&h==0)
  //     break;
  //   ans = 1;

  //   int space[w][h];
  //   for(int i=0;i<w;i++)
  //     for(int j=0;j<h;j++){
  // 	cin >> aaa;
  // 	if(aaa == '.')
  // 	  space[i][j] = 1;
  // 	else if(aaa == '#')
  // 	  space[i][j] = 0;
  // 	else{
  // 	 a = i;
  // 	 b = j;
  // 	}
  //     }

  //   ans = count(a,b,ans,space);
  // }
  ans = 0;
  cout << ans << endl;

  return 0;
}

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

template <class T>
void readVector(int n, vector<T> &v){
	for(int i=0; i<n; ++i){
		T t;
		cin >> t;
		v.push_back(t);
	}
}
typedef pair<long long, long long> P;
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};

void solve(int w, int h){
	vector<string> v;
	readVector(h, v);
	int x = 0, y = 0, ans = 0;
	for(int i=0; i<h; ++i)
		for(int j=0; j<w; ++j)
			if(v[i][j] == '@')
				x = i, y = j;
	queue<P> q;
	q.push(P(x, y));
	v[x][y] = ' ';
	++ans;
	while(!q.empty()){
		P p = q.front();
		q.pop();
		int nx = p.first;
		int ny = p.second;
		for(int i=0; i<4; ++i){
			int a = nx + dx[i];
			int b = ny + dy[i];
			if(0<=a && 0<=b && a<h && b<w && v[a][b] == '.'){
				q.push(P(a, b));
				v[a][b] = ' ';
				++ans;
			}
		}
	}
	cout << ans << "\n";
}

int main(){
//	cin.tie(0);
//	ios::sync_with_stdio(false);
	while(1){
		int w, h;
		cin >> w >> h;
		if(w == 0 && h == 0) return 0;
		solve(w, h);
	}
}

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

using namespace std;

typedef pair<int, int> pii;

int height, width;
int sx, sy;


void bfs(vector<vector<char>>& field)
{
    int ans = 1;
    array<int, 4> dx = {0, 0, 1, -1};
    array<int, 4> dy = {1, -1, 0, 0};

    queue<pii> p;
    p.push({sx, sy});
    
    while (!p.empty()) {
        pii q = p.front(); p.pop();
        
        //??????
        int cx = q.first;
        int cy = q.second;

        for (int i = 0; i < 4; ++i) {
            int nx = cx + dx.at(i);
            int ny = cy + dy.at(i);

            if (nx >= 0 && nx < width && ny >= 0 && ny < height 
                    && field.at(ny).at(nx) == '.') {

                ans++;
                field.at(ny).at(nx) = '#';
                p.push({nx, ny});
            }
        }
    }

    std::cout << ans << std::endl;
}


int main()
{
    while (cin >> width >> height) {
        if (height == 0 && width == 0) break;

        vector<vector<char>> field(height, vector<char>(width));

        for (int y = 0; y < height; ++y) {
            for (int x = 0; x < width; ++x) {
                cin >> field.at(y).at(x);

                if (field.at(y).at(x) == '@') {
                    sx = x; sy = y;
                }
            }
        }

        bfs(field);
    }

    return 0;
}

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

void RB(int x,int y){
    int W,H;
    long count = 0;
    char area[22][22]={};
    char temp;
    using P = pair <int,int>;
    queue <P> que;
    const int direction[4][2] = {{1,0},{-1,0},{0,1},{0,-1}}; 

    W = x;
    H = y;
    for(int i=1;i<=H;i++){
        for(int j=1;j<=W;j++){
            cin >> temp;
            switch (temp){
                case '.':
                    area[i][j] = 1;
                    break;
                case '#':
                    break;
                case '@':
                    area[i][j] = 0;
                    que.push(P(i,j));
                    break;
            }
        }
    }

    while(!que.empty()){
        P temppair = que.front();
        que.pop();
        for(int i=0;i<4;i++){
            int tempx = temppair.first + direction[i][0];
            int tempy = temppair.second + direction[i][1];
            if(area[tempx][tempy]==1){
                que.push(P(tempx,tempy));
                area[tempx][tempy] = 0;
            }
        }
        count++;
    }

    cout << count << endl;

}

int main(){
    int x,y;
    while(1){
        cin >> x >> y;
        if(x == 0 && y == 0) break;
        RB(x,y);
    }
    return 0;
}

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

int W,H;

void dfs(string *m, int x, int y){
	if(y-1 >= 0 && m[y-1][x] == '.'){
		m[y-1][x] = '@';
		dfs(m, x, y-1);
	}
	if(y+1 < H && m[y+1][x] == '.'){
		m[y+1][x] = '@';
		dfs(m, x, y+1);
	}
	if(x-1 >= 0 && m[y][x-1] == '.'){
		m[y][x-1] = '@';
		dfs(m, x-1, y);
	}
	if(x+1 < W && m[y][x+1] == '.'){
		m[y][x+1] = '@';
		return dfs(m, x+1, y);
	}
}
int main(){
	cin >> W >> H;
	while(W!=0 || H!=0){
		string m[H];
		int x,y;
		for(int i = 0; i < H; i++){
			string tmp;
			cin >> tmp;
			m[i] = tmp;
			for(int j = 0; j < W; j++){
				if(tmp[j] == '@'){
					x = j, y = i;
				}
			}
		}
		dfs(m,x,y);
		int ans = 0;
		for(int i = 0; i < H; i++){
			for(int j = 0; j < W; j++){
				if(m[i][j] == '@'){
					ans++;
				}
			}
		}
		cout << ans << endl;
		cin >> W >> H;
	}
}

C++: #include <iostream>

using namespace std;

int w, h;
char tile[21][21];

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

int dfs(int x, int y){
	if(x<0 || w<=x || y<0 || h<=y) return 0;
	if(tile[y][x] == '#') return 0;

	tile[y][x] = '#';
	int count = 1;
	for(int i=0; i<4; i++){
		count += dfs(x+dx[i], y+dy[i]);
	}
	return count;
}


int main(void){
	while(1){
		cin >> w >> h;
		if(!w && !h) break;

		int x=0, y=0; //pos
		for(int i=0; i<h; i++){
			for(int j=0; j<w; j++){
				cin >> tile[i][j];
				if(tile[i][j] == '@'){
					x = j;
					y = i;
				}
			}
		}
		cout << dfs(x, y) << endl;
	}
	return 0;
}