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 <bits/stdc++.h>
using namespace std;
/*{{{*/  //template
#define rep(i,n) for(int i=0;i<n;i++)
constexpr int INF = numeric_limits<int>::max()/2;
constexpr long long LINF = numeric_limits<long long>::max()/3;
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
#define sz(x) (int)(x).size()
#define debug(x) cerr<<#x<<":"<<x<<endl
#define debug2(x,y) cerr<<#x<<","<<#y":"<<x<<","<<y<<endl
//struct fin{ fin(){ cin.tie(0); ios::sync_with_stdio(false); } } fin_;
struct Double{ double d; explicit Double(double x) : d(x){} };
ostream& operator<<(ostream& os,const Double x){ os << fixed << setprecision(20) << x.d; return os; }
template<typename T> ostream& operator<<(ostream& os,const vector<T>& vec){ os << "["; for(const auto& v : vec){ os << v << ","; } os << "]"; return os; }
template<typename T,typename U> ostream& operator<<(ostream& os,const pair<T,U>& p){ os << "(" << p.first << ","<< p.second <<")"; return os; }
template<typename T> ostream& operator<<(ostream& os,const set<T>& st){ os<<"{"; for(T v:st) os<<v<<","; os <<"}"; return os; }
template<typename T,typename U> inline void chmax(T &x,U y){ if(y>x) x = y; }
template<typename T,typename U> inline void chmin(T &x,U y){ if(y<x) x = y; }
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
ll gcd(ll a,ll b){ if(b==0) return a; else return gcd(b,a%b); }
//constexpr double eps = 1e-14; 
constexpr double eps = 1e-10; 
constexpr ll mod = 1e9+7;
const int dx[]={1,0,-1,0} ,dy[] = {0,1,0,-1};
/*}}}*/

int w,h;
int sx,sy;

void solve(){
    vector<string> t(h);
    rep(i,h) cin >> t[i];

    rep(i,h) rep(j,w) if(t[i][j]=='@'){
        sx = i;
        sy = j;
        break;
    }

    bool visited[32][32];
    rep(i,32) rep(j,32) visited[i][j]=false;
    visited[sx][sy]=true;
    queue<pii> que;
    que.push(pii(sx,sy));
    while(!que.empty()){
        auto p = que.front();
        que.pop();

        int x = p.fi;
        int y = p.se;

        rep(i,4){
            int nx = x + dx[i];
            int ny = y + dy[i];
            if(nx<0 or ny<0 or nx>=h or ny>=w) continue;
            if(t[nx][ny]=='#') continue;
            if(visited[nx][ny]) continue;

            visited[nx][ny] = true;
            que.push(pii(nx,ny));
        }
    }

    int ans = 0;
    rep(i,h) rep(j,w) if(visited[i][j]) ans++;
    cout << ans << endl;
}

int main(){
    while(1){
        cin >> w >> h;
        if(w==0 and h==0) break;
        solve();
    }
}

C++: #include<iostream>
#include<queue>
#include<cstring>
// #include<stdio.h>
// 

/*
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
 */

using namespace std;

int W,H;
char c[100][101]; // c[H][W]

bool visited[100][100]; // <----

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

void compute() {

  memset(visited,false,sizeof visited); //memset(???????????????????????????1bit???????????????????????????) ?????????1bit????????§?????????

  queue<int> que;
  int i,j;
  for(i=0;i<H;++i) {
    for(j=0;j<W;++j) {
      if( c[i][j] == '@' ) {
	que.push(j+i*W);
	visited[i][j] = true;
	//break;
	goto Skip;
      }
    }
  }
 Skip:;

  int answer = 1; // <---???????????????
  while( !que.empty() ) { // !0 => 1  !1 => 0
    int cur = que.front(); que.pop(); // que -> [1,2,3,4], front -> 1, pop -> [2,3,4]
    int x = cur % W, y = cur / W; //???????????§?¨?
    
    for(i=0;i<4;++i) {
      int nx = x + dx[i], ny = y + dy[i]; //?¬??????§?¨?????¨?????????§?¨?

      if( !( 0 <= nx && nx < W && 0 <= ny && ny < H ) ) continue; // 0 && 0 => 0, 1 && 0 => 0, 0 && 1 => 0, 1 && 1 => 1
      if( c[ny][nx] == '#' || visited[ny][nx] ) continue;         // 0 || 0 => 0, 1 || 0 => 1, 0 || 1 => 1, 1 || 1 => 1
      
      que.push(nx+ny*W);
      visited[ny][nx] = true;
      ++answer;// <---???????????????

    }
    
  }

  cout << answer << endl;// <---???????????????

}

int main() {
  while( scanf("%d %d",&W,&H), W | H ) {
    //while( scanf("%d %d",&W,&H), W == 0 && H == 0 ) {
    int i;
    for(i=0;i<H;++i) {
      scanf(" %s",&c[i][0]);  // c[y][x]
    }
    compute();
  }
  return 0;
}

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

int w,h;
string s[100];
int ans;

void solve(int y,int x){
  int dx[] = {0,0,1,-1};
  int dy[] = {1,-1,0,0};

  if(x < 0 || y < 0 || x == w || y == h) return;

  if(s[y][x] == '#') return;

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

  ans++;

  for(int i=0;i<4;i++) solve(y+dy[i],x+dx[i]);

}

int main(){

  while(true){
    cin >> w >> h;
    if(!w && !h) break;
    ans = 0;

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

    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	if(s[i][j] == '@') solve(i,j);
      }
    }

    cout << ans << endl;

  }
}