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>
using namespace std;

struct Point
{
  int i;
  int j;
};

static Point minus_point = { -1, -1 };

class Map
{
  public:
    Map(int height, int width);
    ~Map();

  public:
    int Can_go_num(Point p = minus_point);

  private:
    bool CheckIndex(const Point& p);
    bool isBlack(const Point& p);
    bool CanMove(const Point& p);

  private:
    char** m_map;   //  '.' = black, '#' = red, '@' = start, 'x' = already
    int m_height;
    int m_width;
    Point m_start;
};

Map::Map(int height, int width)
{
  m_height = height;
  m_width = width;

  m_map = new char*[m_height];
  for (int i = 0; i < m_height; ++i)
  {
    m_map[i] = new char[m_width];
  }

  for (int i = 0; i < m_height; ++i)
  {
    for (int j = 0; j < m_width; ++j)
    {
      cin >> m_map[i][j];
      if (m_map[i][j] == '@')
      {
        m_start.i = i;
        m_start.j = j;
      }
    }
  }
}

Map::~Map()
{
  for (int i = 0; i < m_height; ++i)
  {
    delete[] m_map[i];
  }
  delete[] m_map;
}

int Map::Can_go_num(Point p)
{
  int result = 1;
  if (p.i == -1)
  {
    p = m_start;
  }
  m_map[p.i][p.j] = 'x';
  Point buf_ps[4] = {
    { p.i, p.j-1 },
    { p.i, p.j+1 },
    { p.i-1, p.j },
    { p.i+1, p.j },
  };
  for (Point buf_p : buf_ps)
  {
    if (CanMove(buf_p) )
    {
      result += Can_go_num(buf_p);
    }
  }

  return result;
}

inline bool Map::CheckIndex(const Point& p)
{
  return (0 <= p.i and p.i < m_height) and (0 <= p.j and p.j < m_width);
}

inline bool Map::isBlack(const Point& p)
{
  return m_map[p.i][p.j] == '.';
}

inline bool Map::CanMove(const Point& p)
{
  return CheckIndex(p) && isBlack(p);
}

int main(void)
{
  int height, width;
  while (true)
  {
    cin >> width >> height;
    if (height == 0 && width == 0) break;
    Map map(height, width);
    cout << map.Can_go_num() << endl;
  }
  return 0;
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
typedef unsigned int ui;
int main(){
	int w, h;
	string s;
	int i, j;
	while (cin >> w >> h) {
		if (w == 0)break;
		char tile[22][22]; int used[22][22];
		for (i = 0; i < 22; i++) {
			tile[i][0] = '#';
			tile[i][h + 1] = '#';
		}
		for (j = 0; j < 22; j++) {
			tile[0][j] = '#';
			tile[w + 1][j] = '#';
		}
		for (i = 0; i < 22; i++) {
			for (j = 0; j < 22; j++) {
				used[i][j] = 0;
			}
		}
		vector<int> q1,q2;
		for (j = 1; j <= h; j++) {
			cin >> s;
			for (i = 1; i <= w; i++) {
				tile[i][j] = s[i - 1];
				if (s[i - 1] == '@') {
					q1.push_back(i); q2.push_back(j);
				}
			}
		}
		while (!q1.empty()) {
			int x = q1[0]; int y = q2[0]; used[x][y]=1;
			q1.erase(q1.begin() + 0); q2.erase(q2.begin() + 0);
			if (used[x - 1][y] == 0 && tile[x - 1][y]=='.') {
				q1.push_back(x - 1); q2.push_back(y);
			}
			if (used[x + 1][y] == 0 && tile[x + 1][y] == '.') {
				q1.push_back(x + 1); q2.push_back(y);
			}
			if (used[x][y-1] == 0 && tile[x][y-1] == '.') {
				q1.push_back(x); q2.push_back(y-1);
			}
			if (used[x][y+1] == 0 && tile[x][y+1] == '.') {
				q1.push_back(x); q2.push_back(y+1);
			}
		}
		int count = 0;
		for (i = 0; i < 22; i++) {
			for (j = 0; j < 22; j++) {
				if (used[i][j] == 1)count++;
			}
		}
		cout << count << endl;
	}
	return 0;
}

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

vector<string> f;
void init(){
  f.clear();
}

int input(){
  int w,h;
  cin>>w>>h;
  if(w==0&&h==0)return false;
  string s;
  for(int i=0;i<h;i++){
    cin>>s;
    f.push_back(s);
  }
  return true;
}

int vx[] = {0,0,1,-1};
int vy[] = {1,-1,0,0};

int rec(int x,int y){
  if(x<0||f.size()<=x)return 0;
  if(y<0||f[x].size()<=y)return 0;
  if(f[x][y]!='.')return 0;
  f[x][y]='#';
  int res = 1;
  for(int i=0;i<4;i++){
    res += rec(x+vx[i],y+vy[i]);
  }
  return res;
}

int solve(){
  int x,y;
  for(int i=0;i<f.size();i++){
    for(int j=0;j<f[i].size();j++){
      if(f[i][j]=='@'){
        x = i;
        y = j;
        f[i][j] = '.';
      }
    }
  }
  return rec(x,y);
}

int main(){
  while(init(),input()){
    cout<<solve()<<endl;
  }
}

C++: #include <bits/stdc++.h>
using namespace std;
char maze[21][21];
#define r(z,x) for(int z=0;z<x;z++)
int ans;
void dfs(int x,int y){
	int dx[4]={0,-1,0,1};
	int dy[4]={1,0,-1,0};
	maze[x][y]='#';
	ans++;
	r(i,4)if(maze[x+dx[i]][y+dy[i]]=='.')dfs(x+dx[i],y+dy[i]);
}
int main(){
	int a,b;
	while(cin>>a>>b,a||b){
		ans=0;
		fill(maze[0],maze[20],'#');
		r(i,b)r(j,a)cin>>maze[i][j];
		r(i,b)r(j,a)if(maze[i][j]=='@')dfs(i,j),cout<<ans<<endl;
	}
}

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;
}