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++: #define _USE_MATH_DEFINES
#include <bits/stdc++.h>

using namespace std;

// repetition
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

// container util
#define all(x) (x).begin(), (x).end()

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                         \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \
       << " " << __FILE__ << endl;

// typedef
typedef long long lint;
typedef unsigned long long ull;
typedef complex<long double> Complex;
typedef pair<int, int> P;
typedef tuple<int, int, int> TP;
typedef vector<int> vec;
typedef vector<vec> mat;

// constant
const int MOD = (int)1e9 + 7;
const int INF = (int)1e18;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const int ddx[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[] = {1, 1, 0, -1, -1, -1, 0, 1};

// conversion
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T>
inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

//
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
//

int sx, sy;

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

  int w, h;

  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0) break;
    char tile[22][22];
    queue<pair<int, int>> que;
    rep(y, h) {
      rep(x, w) {
        cin >> tile[y][x];
        if (tile[y][x] == '@') {
          que.push(make_pair(x, y));
        }
      }
    }

    int ans = 0;

    while (!que.empty()) {
      pair<int, int> p = que.front();
      que.pop();

      rep(i, 4) {
        int nx = p.first + dx[i];
        int ny = p.second + dy[i];
        if (0 <= nx && nx < w && 0 <= ny && ny < h && tile[ny][nx] == '.') {
          ans++;
          tile[ny][nx] = '#';
          que.push(make_pair(nx, ny));
        }
      }
    }

    cout << ans + 1 << "\n";
  }

  return 0;
}

C++: #include <iostream>
#include <string>
#include <sstream>
#include <vector>

struct MapHeader {
	int w;
	int h;
};

MapHeader read_header() {
	std::string line;
	std::getline(std::cin, line);
	std::istringstream line_stream(line);
	MapHeader result;
	line_stream >> result.w >> result.h;
	return result;
}

struct SearchContext {
	MapHeader header;
	std::vector<bool> map;
	size_t count;

	SearchContext(MapHeader header)
		: header(header),
		count(0)
	{
		map.reserve(header.w * header.h);
	}

	size_t index(int x, int y) {
		return (size_t)header.w * y + x;
	}
};

void search(SearchContext& context, int x, int y) {
	if (x < 0 || x >= context.header.w || y < 0 || y >= context.header.h) return;

	size_t index = context.index(x, y);
	if (context.map[index]) return;

	context.map[index] = true;
	context.count++;
	
	search(context, x + 1, y);
	search(context, x - 1, y);
	search(context, x, y + 1);
	search(context, x, y - 1);
}

int main()
{
	while (true) {
		MapHeader header = read_header();
		if (header.w == 0 && header.h == 0) break;

		SearchContext context(header);
		int start_x, start_y;

		std::string line;
		line.reserve(header.w + 2);

		for (int y = 0; y < header.h; y++) {
			line.clear();
			std::getline(std::cin, line);

			for (int x = 0; x < header.w; x++) {
				switch (line[x]) {
				case '.':
					context.map.push_back(false);
					break;
				case '#':
					context.map.push_back(true);
					break;
				case '@':
					start_x = x;
					start_y = y;
					context.map.push_back(false);
					break;
				}
			}
		}

		search(context, start_x, start_y);
		std::cout << context.count << std::endl;
	}

    return 0;
}

C++: #include <iostream>

using namespace std;

int w , h ,c;
char t[26][26];
int dx[] = {1 , -1 , 0 ,0};
int dy[] = {0 , 0 ,1 , -1};
bool input(){
//	cout <<'a';
 	cin >> h >>w;
 	if(w == 0)return false;
 	//cout << 'a';
 	for(int i = 0;i < 25;i++){
		for(int j = 0 ;j < 25 ; j++){
			t[i][j] = 0;
		}
	}
//	cout << 'a';
	for(int i = 0;i < w ;i++){
		for(int j = 0;j < h ;j++){
			cin >> t[i][j];
		}
	}
	
	return true;
}

void dfs(int  x,int y){
	t[x][y] = '&';
	for (int i = 0;i < 4;i++){
		int nx = x + dx[i];
		int ny = y + dy[i];

		if(0 <= nx && nx <= w && 0 <= ny && ny <= h && t[nx][ny] == '.')dfs(nx , ny);
	}
	return;
}

void solve(){
	for(int i = 0;i < w;i++){
		for(int j = 0;j < h;j++){
			if(t[i][j] == '@'){
				dfs(i,j);
			}
		}
	}
	int ans = 0;
	for(int i = 0;i < w;i++){
		for(int j = 0;j < h;j++){
//			cout << t[i][j];
			if(t[i][j] == '&'){
				
				ans++;
			}
		}
//		cout << endl;
	}

	cout << ans << endl;
	return;
}

int main(){
	while(input()){
		solve();
	}

}

C++: #include <iostream>
#include <vector>
#include <queue>

using namespace std;

struct Tile {
	int row,
		col;
};

int main()
{
	int x, y, row, col, c;
	vector<string> v(20);
	queue<Tile> t;

	while (cin >> x >> y, x != 0 && y != 0) {
		c = 1;

		for (int i = 0; i < y; i++) {
			cin >> v.at(i);
			for (int j = 0; j < x; j++) {
				if (v.at(i).at(j) == '@') {
					t.push({ i,j });
					v.at(i).at(j) = '#';
					break;
				}
			}
		}

		while (!t.empty()) {
			row = t.front().row;
			col = t.front().col;
			t.pop();

			if (row - 1 >= 0 && v.at(row-1).at(col) == '.') {
				v.at(row-1).at(col) = '#';
				t.push({row-1,col});
				c++;
			}

			if (row + 1 < y && v.at(row+1).at(col) == '.') {
				v.at(row+1).at(col) = '#';
				t.push({ row+1,col });
				c++;
			}

			if (col - 1 >= 0 && v.at(row).at(col-1) == '.') {
				v.at(row).at(col-1) = '#';
				t.push({ row,col-1 });
				c++;
			}

			if (col + 1 < x && v.at(row).at(col+1) == '.') {
				v.at(row).at(col+1) = '#';
				t.push({ row,col+1 });
				c++;
			}
		}

		cout << c << endl;
	}
}