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>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <stack>
#include <queue>
//ranker
using namespace std;

#define REPS(i, a, n) for (int i = (a); i < (n); ++i)
#define REP(i, n) REPS(i, 0, n)
#define RREP(i, n) REPS(i, 1, n + 1)
#define DEPS(i, a, n) for (int i = (a); i >= n; --i)
#define DEP(i, n) DEPS(i, n, 0)

using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<int, int>;
using pis = pair<int, string>;
using psi = pair<string, int>;
using D = double;

void merge (int *first1, int *last1, int *first2, int *last2){
    int size1 = last1 - first1;
    int size2 = last2 - first2;
    int *result = new int[size1+size2];
    int i = 0;
    int j = 0;
    int index = 0;
    while (true){
        if (i < size1 && (first1[i] <= first2[j] || j >= size2)){
            result[index] = first1[i];
            ++i; ++index;
        }
        if (j < size2 && (first1[i] > first2[j] || i >= size1)){
            result[index] = first2[j];
            ++j; ++index;
        }
        if (i==size1 && j==size2){
            for (i=0; i<size1; ++i) first1[i] = result[i];
            for (j=0; j<size2; ++j) first2[j] = result[j+size1];
            delete[] result;
            return;
        }
    }
}
void MergeSort (int *first, int *last){
    int size = last - first;
    if(size <= 1) return;
    MergeSort(first, first+size/2);
    MergeSort(first+size/2, last);
    merge(first, first+size/2, first+size/2, last);
}


int gcd(int a, int b)
{
    int c;

    if (a < b) {
        a+=b; b=a-b; a-=b;
    }

    while (b != 0) {
        c = a % b;
        a = b;
        b = c;
    }

    return a;
}

int dx[3]={-1, 0, 1};
int dy[3]={-1, 0, 1};
int W, H;
char masu[20][20];
int cnt;

int dfs(int a, int b){
  masu[a][b] = '#';
  cnt++;
  REP(i, 3)REP(j, 3) if(masu[a+dx[i]][b+dy[j]]=='.' && abs(dx[i]+dy[j])==1) dfs(a+dx[i], b+dy[j]);
  return cnt;
}


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

  while(1){
    cin >> W >> H;
    if(W == 0 && H == 0) break;
    REP(j, H)REP(i, W) cin >> masu[i][j];

    int i0, j0;
    REP(j, H)REP(i, W) if(masu[i][j] == '@'){
      i0=i;
      j0=j;
      break;
    }

    cnt = 0;
    cnt = dfs(i0, j0);
    cout << cnt << endl;
  }
    return 0;
}

C++: #include<stdio.h>
//#include<windows.h>

int masu[22][22];
int ans=0;
int i,j;

void saiki(int x,int y){
	/*
	for(i=0;i<22;i++){
		for(j=0;j<22;j++){
			if(masu[i][j]==0)printf(" ");
			if(masu[i][j]==1)printf("¡");
		}
		puts("");
	}
	printf("%d",ans);
	Sleep(100);
	system("cls");
	*/
	masu[x][y]=1;
	ans++;
	if(masu[x][y-1]==0)saiki(x,y-1);
	if(masu[x+1][y]==0)saiki(x+1,y);
	if(masu[x][y+1]==0)saiki(x,y+1);
	if(masu[x-1][y]==0)saiki(x-1,y);
}

int main(){
	for(;;){
	int W,H;
	int x,y;
	char str[22];
	
	for(i=0;i<22;i++){
		for(j=0;j<22;j++){
			masu[i][j]=1;
		}
	}
	
	scanf("%d%d", &W, &H);
	if(W == 0){
		break;
	}
	for(i=0;i<H;i++){
		scanf("%s",str);
		for(j=0;j<W;j++){
			if(str[j]=='.')masu[i+1][j+1]=0;
			if(str[j]=='@'){
				x=i+1;
				y=j+1;
				masu[i+1][j+1]=0;
			}
		}
	}
	/*
	for(i=0;i<22;i++){
		for(j=0;j<22;j++){
			if(masu[i][j]==0)printf(" ");
			if(masu[i][j]==1)printf("¡");
		}
		puts("");
	}
	printf("%d\n",ans);
	system("cls");
	*/
	saiki(x,y);
	/*
	for(i=0;i<22;i++){
		for(j=0;j<22;j++){
			if(masu[i][j]==0)printf(" ");
			if(masu[i][j]==1)printf("¡");
		}
		puts("");
	}
	*/
	printf("%d\n",ans);
	}
	return 0;
}

C++: #include <stdio.h>

void init(int data[22][22],int w,int h){
	for(int i = 0;i < w+2;i++){
		data[0][i] = 1;
		data[h+1][i] = 1;
	}
	for(int i = 0;i < h;i++){
		data[i+1][0] = 1;
		data[i+1][w+1] = 1;
	}
}

void show(int data[22][22],int w,int h){
	for(int i = 0;i < h+2;i++){
		for(int j = 0;j < w+2;j++){
			printf("%d",data[i][j]);
		}
		printf("\n");
	}
}

int ans;
int dx[4] = {1,0,-1,0},dy[4] = {0,-1,0,1};

void count(int data[22][22],int sx,int sy){
	for(int i = 0;i < 4;i++){
		if(data[sy+dy[i]][sx+dx[i]] == 0){
			ans++;
			data[sy+dy[i]][sx+dx[i]] = 2;
			count(data,sx+dx[i],sy+dy[i]);
		}
	}
}

int main(){
	int W,H,sx,sy;
	int data[22][22];
	char str[21];

	while(1){
		scanf("%d %d",&W,&H);
		if(W == 0 && H == 0){
			break;
		}

		init(data,W,H);
		for(int i = 0;i < H;i++){
			scanf("%s",str);
			for(int j = 0;j < W;j++){
				if(str[j] == '.'){
					data[i+1][j+1] = 0;
				}else if(str[j] == '#'){
					data[i+1][j+1] = 1;
				}else if(str[j] == '@'){
					data[i+1][j+1] = 2;
					sy = i+1;
					sx = j+1;
				}
			}
		}
		//show(data,W,H);

		ans = 1;
		count(data,sx,sy);

		printf("%d\n",ans);
	}

	return 0;
}