Go: package main

import (
	"bufio"
	"encoding/json"
	"fmt"
	"os"
	"strconv"
	"strings"
)

var (
	sc = bufio.NewScanner(os.Stdin)
)

type Color int

const (
	ColorBlack Color = iota
	ColorRed
)

var strByColor = map[Color]string{
	ColorBlack: "black",
	ColorRed:   "red",
}

func (c Color) String() string {
	return strByColor[c]
}

type Node struct {
	ID      int
	X, Y    int
	Color   Color
	NextIDs map[int]struct{}
}

func (n *Node) String() string {
	d, _ := json.Marshal(n)
	return string(d)
}

type Map struct {
	Width, Height int
	Nodes         map[int]*Node
}

func (m *Map) GetByID(id int) *Node {
	return m.Nodes[id]
}

func (m *Map) Get(x, y int) *Node {
	return m.Nodes[m.getID(x, y)]
}

func (m *Map) SetColor(x, y int, c Color) {
	m.Nodes[m.getID(x, y)].Color = c
}

func (m *Map) getID(x, y int) int {
	return x + y*m.Width
}

func (m *Map) init() {
	for x := 1; x <= m.Width; x++ {
		for y := 1; y <= m.Height; y++ {
			id := m.getID(x, y)
			m.Nodes[id] = &Node{ID: id, X: x, Y: y}
		}
	}
	for x := 1; x <= m.Width; x++ {
		for y := 1; y <= m.Height; y++ {
			nextIDs := map[int]struct{}{}
			if x > 1 {
				nextIDs[m.getID(x-1, y)] = struct{}{}
			}
			if x < m.Width {
				nextIDs[m.getID(x+1, y)] = struct{}{}
			}
			if y > 1 {
				nextIDs[m.getID(x, y-1)] = struct{}{}
			}
			if y < m.Height {
				nextIDs[m.getID(x, y+1)] = struct{}{}
			}
			m.Get(x, y).NextIDs = nextIDs
		}
	}
}

func NewMap(w, h int) *Map {
	m := &Map{Width: w, Height: h, Nodes: map[int]*Node{}}
	m.init()
	return m
}

type Searcher struct {
	Map        *Map
	queue      []*Node
	touchedIDs map[int]struct{}
	visitedIDs map[int]struct{}
}

func NewSearcher(m *Map) *Searcher {
	return &Searcher{
		Map:        m,
		queue:      []*Node{},
		touchedIDs: map[int]struct{}{},
		visitedIDs: map[int]struct{}{},
	}
}

func (s *Searcher) Search(start *Node) {
	s.enqueue(start)

	for {
		if len(s.queue) == 0 {
			break
		}
		for nextID := range s.dequeue().NextIDs {
			n := s.Map.GetByID(nextID)
			if n.Color == ColorRed || s.hasTouched(n) {
				continue
			}
			s.enqueue(n)
		}
	}
}

func (s *Searcher) VisitedNodeCount() int {
	return len(s.visitedIDs)
}

func (s *Searcher) hasTouched(n *Node) bool {
	_, ok := s.touchedIDs[n.ID]
	return ok
}

func (s *Searcher) enqueue(n *Node) {
	s.queue = append(s.queue, n)
	s.touchedIDs[n.ID] = struct{}{}
}

func (s *Searcher) dequeue() *Node {
	n := s.queue[0]
	s.queue = s.queue[1:]
	s.visitedIDs[n.ID] = struct{}{}
	return n
}

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])

		m := NewMap(w, h)

		// Set colors
		var start *Node
		for y := 1; y <= h; y++ {
			sc.Scan()
			for i, c := range sc.Text() {
				switch c {
				case '.', '@':
					m.SetColor(i+1, y, ColorBlack)
					if c == '@' {
						start = m.Get(i+1, y)
					}
				case '#':
					m.SetColor(i+1, y, ColorRed)
				}
			}
		}

		// Search
		s := NewSearcher(m)
		s.Search(start)
		fmt.Println(s.VisitedNodeCount())
	}
}

C++: #include <iostream>

using namespace std;

int width, height;
int ans = 0;
char map[20][20];
int checked[20][20];

void search(int x, int y);
int main(){
	while (1) {
		ans = 0;
		for (int y=0; y<20; y++) {
			for (int x=0; x<20; x++) {
				map[y][x] = '#';
				checked[y][x] = 0;
			}
		}
		cin >> width >> height;
		if (width==0 && height==0) {
			break;
		}

		int startX, startY;
		for (int y=0; y<height; ++y) {
			for (int x=0; x<width; ++x) {
				cin >> map[y][x];
				if (map[y][x] == '@') {
					map[y][x] = '.';
					startX = x;
					startY = y;
				}
			}
		}
		search(startX, startY);
		cout << ans << endl;
	}
	return 0;
}
void search(int x, int y) {
	ans++;
	static const int diffX[] = {0,-1,1,0};
	static const int diffY[] = {-1,0,0,1};
	checked[y][x] = 1;
	for (int ite=0; ite<4; ++ite) {
		int nowX = x + diffX[ite];
		int nowY = y + diffY[ite];
		if (nowX < 0 || nowX >= 20 || nowY < 0 || nowY >= 20) continue;
		if (checked[nowY][nowX] == 0 && map[nowY][nowX] == '.') {
			search(nowX, nowY);
		}
	}
}

C++: #include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<string>
#include<cmath>
//#include<>
using namespace std;

typedef long long int64;
typedef int int32;
typedef char int8;
typedef int32 int__;
typedef pair<int,int> pii;
#define rep(i,j) for(int__ i=0;i<j;i++)
#define repeat(i,j,k) for(int__ i=(j);i<(k);i++)
#define repeat_eq(i,j,k) for(int__ i=(j);i<=(k);i++)
#define all(v) (v).begin(),(v).end()

const int INF=1<<30;//=10E10

int main()
{
  int w,h;
  while(cin>>w>>h && w && h){
    char tile[20][20];
    int fx,fy;
    rep(i,h)rep(j,w){
      cin>>tile[i][j];
      if(tile[i][j]=='@'){
	fx=j;fy=i;
      }
    }
    
    bool visited[20][20]={};
    stack<pii> p;
    int ans=0;
    p.push(pii(fy,fx));
    while(!p.empty()){
      pii now=p.top();p.pop();
      int nx=now.second,ny=now.first;
      if(!visited[ny][nx]){
	visited[ny][nx]=true;
	ans++;
	if(ny>0 && tile[ny-1][nx]=='.')p.push(pii(ny-1,nx));
	if(ny+1<h && tile[ny+1][nx]=='.')p.push(pii(ny+1,nx));
	if(nx>0 && tile[ny][nx-1]=='.')p.push(pii(ny,nx-1));
	if(nx+1<w && tile[ny][nx+1]=='.')p.push(pii(ny,nx+1));
      }
    }
    cout<<ans<<endl;
  }

  return 0;
}

C++: // clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
// #define main int main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
#define stlice(from, to) substr(from, (to) - (from) + 1)
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
typedef vector<vb> vvb;
typedef vector<vpii> vvpii;
const int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
const int MOD = 1000000007;
const double PI = acos(-1);
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
bool odd(const int &n) { return n & 1; }
bool even(const int &n) { return !odd(n); }
void solve();
main { solve(); return 0; }
// clang-format on

void solve() {
  int w, h;
  while (cin >> w >> h, w) {
    vs v(h);
    int si, sj;
    rep(i, h) {
      cin >> v[i];
      rep(j, v[i].size()) if (v[i][j] == '@') {
        si = i;
        sj = j;
      }
    }
    queue<pii> q;
    q.emplace(si, sj);
    int c = 0;
    while (q.size()) {
      int i, j;
      tie(i, j) = q.front();
      q.pop();
      if (i < 0 || j < 0 || i >= h || j >= w || v[i][j] == '#') continue;
      c++;
      v[i][j] = '#';
      vi di = {0, 1, 0, -1}, dj = {1, 0, -1, 0};
      rep(k, 4) q.emplace(i + di[k], j + dj[k]);
    }
    cout << c << endl;
  }
}

C++: #include<stdio.h>

int m,n,ans;
char ice[22][22];

void saiki(int w,int h){
	ice[w+1][h]='*';
	
	if(ice[w+2][h]=='.')saiki(w+1,h);
	if(ice[w+1][h+1]=='.')saiki(w,h+1);
	if(ice[w][h]=='.')saiki(w-1,h);
	if(h!=0)if(ice[w+1][h-1]=='.')saiki(w,h-1);
}

int main(void){

	for(;;){
		scanf("%d %d",&m,&n);
		if(!m||!n)break;
		
		ans=0;
		
		for(int i=0;i<n;i++){
			scanf("%s",&ice[i+1]);
		}
		
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				if(ice[i+1][j]=='@')saiki(i,j);
			}
		}
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				if(ice[i+1][j]=='*')ans++;
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}