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<queue>
#include <utility>
#include <functional>
//#include<>
using namespace std;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
//#define int long long;
signed main(){
  int w, h;
  while(cin >> w >> h, w){
    int cnt{};
    vector<string> v(h);
    for(int i = 0; i < h; ++i){
      cin >> v[i];
    }
    //initialize
    pair<int, int> p;
    queue<pair<int, int>> q;	
    for(int i = 0; i < h; ++i){
      for(int j = 0; j < w; ++j){
	if(v[i][j] == '@'){
	  p = make_pair(i,j);
	  q.push(p);
	}
      }
    }
    while(!q.empty()){
      pair<int, int> p;
      p = q.front();
      q.pop();

      for(int k = 0; k < 4; ++k){
	int x = p.first + dx[k]; int y = p.second + dy[k];
	if(0 > x || h <= x || 0 > y || w <= y)continue;
	if('.' == v[x][y]){
	  v[x][y] = '@';
	  ++cnt;
	  q.push(make_pair(x, y));
	}
      }
    }
    cout << cnt+1 << endl;
  }
  return 0;
}

C++: #include <cstdio>
using namespace std;

int W, H;
const int SIZE = 20;
char f[SIZE + 1][SIZE + 1];
bool c[SIZE][SIZE];

int dfs(int x, int y){
	if(x < 0 || y < 0 || x >= W || y >= H) return 0;
	if(c[y][x] || f[y][x] == '#') return 0;
	c[y][x] = true;
	
	return 1 + dfs(x + 1, y) + dfs(x, y + 1) + dfs(x - 1, y) + dfs(x, y - 1);
}

int main(){
	int sx, sy;
	
	while(true){
		scanf("%d%d", &W, &H);
		if(H == 0 && W == 0) break;
		for(int i = 0; i < H; i++){
			scanf("%s", f[i]);
		}
		
		for(int y = 0; y < H; y++){
			for(int x = 0; x < W; x++){
				c[y][x] = false;
				if(f[y][x] == '@'){
					sx = x; sy = y;
				}
			}
		}
		
		printf("%d\n", dfs(sx, sy));
	}
	
	return 0;
}

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

using std::cin;
using std::cout;
using std::endl;

int main(void) {
	int W,H;
	cin >> W;
	cin >> H;

	while (W!=0||H!=0) {
		int count = 0;
		char c;
		std::stack<int> st_x;
		std::stack<int> st_y;
		int xn,yn;
		std::vector< std::vector<bool> > tile(W+2, std::vector<bool>(H+2));
		for (int i = 0; i < H+2; i++) {
			for (int j = 0; j < W+2; j++) {
				if (i==0||i==H+1||j==0||j==W+1) {
					tile[j][i] = false;
				} else {
					cin >> c;
					if (c=='.') {
						tile[j][i] = true;
					}
					if (c=='#') {
						tile[j][i] = false;
					}
					if (c=='@') {
						tile[j][i] = false;
						st_x.push(j);
						st_y.push(i);
						count++;
					}
				}
			}
		}
		while (!st_x.empty()) {
			xn = st_x.top();
			yn = st_y.top();
			st_x.pop();
			st_y.pop();

			if (tile[xn+1][yn]) {
				st_x.push(xn+1);
				st_y.push(yn);
				tile[xn+1][yn] = false;
				count++;
			}
			if (tile[xn-1][yn]) {
				st_x.push(xn-1);
				st_y.push(yn);
				tile[xn-1][yn] = false;
				count++;
			}
			if (tile[xn][yn+1]) {
				st_x.push(xn);
				st_y.push(yn+1);
				tile[xn][yn+1] = false;
				count++;
			}
			if (tile[xn][yn-1]) {
				st_x.push(xn);
				st_y.push(yn-1);
				tile[xn][yn-1] = false;
				count++;
			}
		}
		cout << count << endl;

		cin >> W;
		cin >> H;
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <array>
#include <vector>
#include <math.h>
#include <algorithm>
#include <set>
#include <deque>
#include <utility>
#include <map>

#define ll long long int
#define MAX_W 20
#define MAX_H 20

using namespace std;

char field[MAX_W][MAX_H];
bool reach[MAX_W][MAX_H];
int h, w;
int cou = 0;


void search(int x, int y)
{
	if (x < 0 || w <= x || y < 0 || h <= y || field[x][y] == '#') {
		return;
	}

	if (reach[x][y] == true) {
		return;
	}

	reach[x][y] = true;


	cou++;



	search(x - 1, y);
	search(x + 1, y);
	search(x, y + 1);
	search(x, y - 1);

}

int main(void)
{
	vector <string> s;
	vector <int> ans;
	int i, j;

	int dx, dy; //start

	string temp;


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

		for (i = 0; i < h; i++) {
			cin >> temp;
			s.push_back(temp);
		}

		for (i = 0; i < h; i++) {
			for (j = 0; j < w; j++) {
				field[j][i] = s[i][j];
				if (s[i][j] == '@') {
					dx = j;
					dy = i;
				}
			}
		}




		search(dx, dy);

		ans.push_back(cou);
		cou = 0;

		s.clear();

		for (i = 0; i < MAX_W; i++) {
			
			for (j = 0; j < MAX_H; j++) {


				field[i][j] = '#';
				reach[i][j] = false;
			}
		}


	}
	for (i = 0; i < ans.size(); i++) {
		cout << ans[i] << endl;
	}

	return (0);
}

C++: #include <bits/stdc++.h>
using namespace std;
vector<string>v;
vector<int>ansl;
string s;
int ary[100005] = {0};
char mp[21][21];
int cnt = 0;
int d_x[] = {0,1,0,-1};
int d_y[] = {1,0,-1,0};
int W,H;

void dfs(int x,int y){
  mp[y][x] = '#';
  cnt++;
  for(int d = 0;d<4;d++){
    int nx = x+d_x[d],ny = y+d_y[d];
      if((nx>=0)&&(ny>=0)&&(ny<H)&&(nx<W)&&(mp[ny][nx] == '.')){
        dfs(nx,ny);
      }
  }
}
int main(){
  cin >>W>>H;

  while((W!=0)&&(H!=0)){
    char mp[21][21] = {'0'};
    cnt = 0;
    for(int i = 0;i<H;i++){
      for(int j = 0;j<W;j++){
        cin >>mp[i][j];
      }
    }
    for (int y = 0; y < H; y++) {
            for (int x = 0; x < W; x++) {
                if (mp[y][x] == '@') {
                    dfs(x, y);
                }
            }
        }
    cout <<cnt<<endl;
    cin >>W>>H;
  }
  return 0;
}

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

int tile[22][22];
int sum;

void search(int w,int h)
{
  if(tile[w][h]){
    tile[w][h]=0; sum++;
    search(w,h-1);
    search(w-1,h);
    search(w+1,h);
    search(w,h+1);
  }
}

int main()
{
  int w,h,i,j,sx,sy;
  char c;
  
  for(;;){
    cin>>w>>h;
    if(!(w|h)) break;

    sum=0;
    for(i=0;i<22;i++)
      for(j=0;j<22;j++)
	tile[i][j]=0;
    for(i=1;i<h+1;i++){
      for(j=1;j<w+1;j++){
	cin>>c;
	if(c=='#') tile[j][i]=0;
	else if(c=='.') tile[j][i]=1;
	else if(c=='@'){
	  tile[j][i]=1; sx=j; sy=i;
	}
      }
    }
    search(sx,sy);
    cout<<sum<<endl;
  }
}