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 <cstring>
#include <stack>
using namespace std;

class P {
public:
	int x, y;
	P() {}
	P(int _x, int _y) : x(_x), y(_y) {}
};

int w, h;
char map[22][22];

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

int main() {
	while (1) {
		cin >> w >> h;
		if (w == 0 && h == 0) break;
		P start;
		for (int i = 0; i < h; i++) {
			cin >> map[i];
			char* at = strchr(map[i], '@');//文字列から文字を検索する
			if (at != NULL) {//@が存在したらスタート地点なので記録
				start.x = at - map[i];
				start.y = i;
			}
		}
		//深さ優先探索
		stack<P> deq;
		int count = 0;
		deq.push(start);//初期位置をpush
		while (!deq.empty()) {
			P current = deq.top();//pop
			deq.pop();
			count++;
			for (int i = 0; i < 4; i++) {
				int nextX = dx[i] + current.x;
				int nextY = dy[i] + current.y;
				if (0 <= nextX && nextX < w && 0 <= nextY && nextY < h
						&& map[nextY][nextX] == '.') {
					deq.push(P(nextX, nextY));//次のノードをpush
					map[nextY][nextX] = '#';//戻らないように埋める
				}
			}

		}
		cout << count << endl;
	}
	return 0;
}

/*	ポイント
		領域の塗りつぶし問題
		再帰による実装の方が一般的かも
		埋めるタイミングが間違ってて詰まってた
		mapはy,xの順の添字で使うと入力が楽
			でもその分参照は注意
		探索の基礎
			push()
			while (!empty) {
				pop()
				if (ゴール判定) {
					break;
				}
				if (次のノードの条件) {
					push()
				}
			}
			※stackで深さ,queueで幅,priority_queueでダイクストラ(コストがpriority)
*/

C++: #pragma GCC optimize("O3")

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int N=25;
int a[N*N];
char c[N][N];
int root(int x){
	int i=x;
	while(1){
		if(a[x]==x) break;
		x=a[x];
	}
	int j=x;
	while(1){
		if(a[i]==i) break;
		x=a[i],a[i]=j,i=x;
	}
	return j;
}
int main(){
	int m,n;
	while(scanf("%d%d",&n,&m)){
		if(m==0&&n==0) break;
		getchar();
		int i,j;
		for(i=1;i<=m*n;i++) a[i]=i;
		for(i=0;i<m;i++) gets(c[i]);
		for(i=0;i<m;i++){
			for(j=0;j<n;j++){
				if(c[i][j]=='#') continue;
				int r=root(i*n+j+1),rr;
				if(i&&c[i-1][j]!='#'){
					rr=root((i-1)*n+j+1);
					if(r!=rr) a[rr]=r;
				}
				if(i+1<m&&c[i+1][j]!='#'){
					rr=root((i+1)*n+j+1);
					if(r!=rr) a[rr]=r;
				}
				if(j&&c[i][j-1]!='#'){
					rr=root(i*n+j-1+1);
					if(r!=rr) a[rr]=r; 
				}
				if(j+1<n&&c[i][j+1]!='#'){
					rr=root(i*n+j+1+1);
					if(r!=rr) a[rr]=r;
				}
			}
		}
		int s,ans=0;
		for(i=0;i<m;i++){
			for(j=0;j<n;j++){
				if(c[i][j]=='@'){
					s=root(i*n+j+1);
					for(int k=1;k<=m*n;k++){
						if(s==root(k)) ans++;
					}
					printf("%d\n",ans);
					break;
				}
			}
			if(j<n) break;
		}
	}
	
	return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <algorithm>
#include <iostream>
#include <string>

#define REP(i,n) for(int i=0;i<n;++i)
#define REPR(i,n) for(int i=n;i>=0;--i)
#define REPI(itr,v) for(auto itr=v.begin();itr!=v.end();++itr)
#define REPIR(itr,v) for(auto itr=v.rbegin();itr!=v.rend();++itr)
#define FOR(i,a,b) for(int i=a;i<b;++i)
#define SORT(v,n) sort(v, v+n)
#define SORTV(v) sort(v.begin(), v.end())
#define ALL(v) v.begin(),v.end()
#define llong long long
#define INF 999999999
#define SUR 1000000007
#define pb push_back
#define pf push_front
#define MP make_pair

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

using namespace std;

typedef pair<int,int> pii;

int h, w;

int solve(bool used[20][20], char s[21][21], int x, int y){
  int res = 0;
  if(s[y][x] == '#'){
    return 0;
  }else{
    res++;
  }

  REP(i,4){
    int next_x = x + dx[i], next_y = y + dy[i];
    if(next_x >= 0 && next_x < w && next_y >= 0 && next_y < h){
      if(!used[next_y][next_x]){
        used[next_y][next_x] = true;
        res += solve(used, s, next_x, next_y);
      }
    } 
  }
  return res;
}

int main(){

  while(true){
    scanf("%d %d", &w, &h);
    if(h == 0 && w == 0){
      break;
    }

    char s[21][21];
    REP(i,h){
      scanf("%s", s[i]);
    }

    bool found = false;
    REP(i,h){
      REP(j,w){
        if(s[i][j] == '@'){
          bool used[20][20] = {};
          used[i][j] = true;
          int ans = solve(used, s, j, i);
          printf("%d\n", ans);
          break;
          found = true;
        }
      }
      if(found) break;
    }
  }
  return 0;
}