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;


void search(int h, int w, int tairu[22][22], int check[22][22]){
    if(check[h][w]==1)return;
    if(tairu[h][w]==0)return;
    
    check[h][w]=1;
    search(h+1,w,tairu,check);
    search(h-1,w,tairu,check);
    search(h,w+1,tairu,check);
    search(h,w-1,tairu,check);
}






int main()
{

    int H,W;
    while(cin>>W>>H){
        if(W==0&&H==0)break;
        int tairu[22][22]={0};
        int start_w, start_h;
        for(int i=1; i<=H; i++){
            for(int j=1; j<=W; j++){
                char input;
                cin>>input;
                if(input=='.')tairu[i][j]=1;
                if(input=='@'){
                    tairu[i][j]=1;
                    start_w=j;
                    start_h=i;
                }
            }
        }
        //
        
        int check[22][22]={0};
        search(start_h, start_w, tairu, check);
        int ans=0;
        for(int i=1; i<=H; i++){
            for(int j=1; j<=W; j++){
                ans+=check[i][j];
            }
        }
        cout<<ans<<endl;
        
        
        
    }
}

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<queue>
using namespace std;
int w,h;
string map[40];
int vec[4][2]={{0,1},{0,-1},{-1,0},{1,0}};

struct pos{
	pos(int X,int Y){
		x=X,y=Y;
	}
	int x,y;
};

int move(int x,int y){
	int ans=0;
	map[x][y]='#';
	queue<pos> dfs;
	dfs.push(pos(x,y));
	while(!dfs.empty()){
		pos n=dfs.front();dfs.pop();
		x=n.x;
		y=n.y;
		////
		ans++;
		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]]=='.'){
					map[x+vec[i][0]][y+vec[i][1]]='#';
					dfs.push(pos(x+vec[i][0],y+vec[i][1]));
				}
			}
		}
		/////
	}
	
	
	return ans;
}

int main(){
	while(cin>>w>>h,w){
		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;
				}
			}
		}
		int ans=move(fx,fy);
		cout<<ans<<endl;
		
	}
}

C++: /*
ツ個ゥツづ按づェツつスツ姪「ツ妥ィツづ?つキツづ?
'@'ツづ個湘ェツ渉環つゥツづァ, ツ深ツつウツ優ツ静ヲツ探ツ催オツづ?嘉ーツつォツづ慊つオツづ・ツつ、.
 */
#include <iostream>
using namespace std;

int w, h;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
char t[22][22];

int solve(int x, int y){
  int res = 1;

  t[y][x] = '#';

  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[ny][nx] != '#'){
      res += solve(nx, ny);
    }
  }

  return res;
}

int main(){
  while(cin >> w >> h, w || h){
    int sx, sy;

    for(int i = 0; i < h; i++){
      cin >> t[i];

      for(int j = 0; j < w; j++){
        if(t[i][j] == '@'){
          sy = i;
          sx = j;
          t[i][j] = '.';
        }
      }
    }

    cout << solve(sx, sy) << endl;
  }
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>

using namespace std;

int solve(int n,int m){
	int flag[21][21]={};
	int field[21][21]={};
	queue<pair<int,int> >q;
	for(int i=0;i<m;i++){
		string s;
		cin >> s;
		for(int j=0;j<n;j++){
			if(s[j]=='.')field[i][j]=0;
			if(s[j]=='#')field[i][j]=-1;
			if(s[j]=='@'){
				field[i][j]=1;
				flag[i][j]=1;
				q.push(make_pair(i,j));
			}
		}
	}
	while(!q.empty()){
		pair<int,int> p = q.front();
		q.pop();
		int a = p.first;
		int b = p.second;
		if(a>0){
			if(flag[a-1][b]==0&&field[a-1][b]==0){
				q.push(make_pair(a-1,b));
				flag[a-1][b]=1;
			}
		}
		if(a<m-1){
			if(flag[a+1][b]==0&&field[a+1][b]==0){
				q.push(make_pair(a+1,b));
				flag[a+1][b]=1;
			}
		}
		if(b>0){
			if(flag[a][b-1]==0&&field[a][b-1]==0){
				q.push(make_pair(a,b-1));
				flag[a][b-1]=1;
			}
		}
		if(b<n-1){
			if(flag[a][b+1]==0&&field[a][b+1]==0){
				q.push(make_pair(a,b+1));
				flag[a][b+1]=1;
			}
		}
	}
	int ans=0;
	for(int i=0;i<m;i++){
		for(int j=0;j<n;j++){
			if(flag[i][j]==1)ans+=1;
		}
	}
	return ans;
}


int main(){
	int n,m;
	while(1){
		cin >> n >> m;
		if(n==0&&m==0)break;
		cout << solve(n,m) <<endl;
	}
	return 0;
}