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<algorithm>

using namespace std;

char tyle[30][30]={'0'};

int find_tyle(int i,int j){
  if(tyle[i+1][j]=='.'){
    tyle[i+1][j]='a';
    find_tyle(i+1,j);
  }
  if(tyle[i][j+1]=='.'){
    tyle[i][j+1]='a';
    find_tyle(i,j+1);
  }
  if(tyle[i-1][j]=='.'){
    tyle[i-1][j]='a';
    find_tyle(i-1,j);
  }
  if(tyle[i][j-1]=='.'){
    tyle[i][j-1]='a';
    find_tyle(i,j-1);
  }
}

int main(){
  
  int w,h;
  
  while(cin>>w>>h,w){
    int ans=0;
    for(int i=1;i<=h;++i){
      for(int j=1;j<=w;++j){
      cin>>tyle[i][j];
      }
    }
    for(int i=1;i<=h;++i){
      for(int j=1;j<=w;++j){
	if(tyle[i][j]=='@'){
	  find_tyle(i,j);
	  tyle[i][j]='a';
	}
      }
    }
    for(int i=1;i<=h;++i){
      for(int j=1;j<=w;++j){
	if(tyle[i][j]=='a'){
	  ans++;
      }
    }
  }
    cout<<ans<<endl;

   for(int i=0;i<30;++i){
      for(int j=0;j<=30;++j){
      tyle[i][j]='0';
      }
    }
    
  }
}

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

using std::cin;
using std::cout;
using std::cerr;
using std::endl;

const int vx[4] = { 0, 1, 0, -1 }, vy[4] = { 1, 0, -1, 0 };

int W, H;
std::string s[21];
bool is_searched[21][21];

int solve() {
  int sx, sy;

  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      if (s[i][j] == '@') {
        sx = i, sy = j;
        break;
      }
    }
  }

  std::memset(is_searched, false, sizeof is_searched);

  int ans = 1;

  std::queue<std::pair<int, int> > que;
  que.push({ sx, sy });
  is_searched[sx][sy] = true;

  while (!que.empty()) {
    std::pair<int, int> p = que.front();
    que.pop();

    for (int i = 0; i < 4; ++i) {
      int nx = p.first + vx[i], ny = p.second + vy[i];

      if (0 <= nx && nx < H && 0 <= ny && ny < W && !is_searched[nx][ny] && s[nx][ny] == '.') {
        ans++;
        is_searched[nx][ny] = true;
        que.push({ nx, ny });
      }
    }
  }

  return ans;
}

int main() {
  while (cin >> W >> H, H | W) {
    for (int i = 0; i < H; ++i) {
      cin >> s[i];
    }

    int ans = solve();

    cout << ans << endl;
  }

  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

#define REP(i,n) for(int i=0;i<(n);i++)
#define ALL(v) (v).begin(),(v).end()
#define pb push_back
typedef long long ll;
typedef pair<int,int> P;

template<typename A,typename B> inline void chmin(A &a,B b){if(a>b) a=b;}
template<typename A,typename B> inline void chmax(A &a,B b){if(a<b) a=b;}

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

int w,h;
char M[25][25];

int dfs(int y,int x)
{
    int res=1;
    M[y][x]='#';
    REP(i,4){
        int ny=y+dy[i],nx=x+dx[i];
        if(0<=ny && ny<h && 0<=nx && nx<w && M[ny][nx]=='.'){
            res+=dfs(ny,nx);
        }
    }
    return res;
}
int main()
{   
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    while(cin>>w>>h,w|h)
    {   
        REP(i,h) cin>>M[i];
        REP(i,h) REP(j,w){
            if(M[i][j]=='@'){
                cout<<dfs(i,j)<<endl;
                i=h,j=w;
            }
        }
    }
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<cmath>
#include<iomanip>
#include<set>
#include<numeric>
#include<sstream>
#include<random>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, st, n) for (int i = st; i < n; ++i)
using pii = pair<int, int>;
const int inf = 1e9 + 7;
int dy[] = {0, 0, -1, 1};
int dx[] = {1, -1, 0, 0};

void datasets(int h, int w) {
    
    char table[h][w];
    rep(i, h) rep(j, w) cin >> table[i][j];
    queue<pii> q;
    int sum = 1;
    rep(i, h) rep(j, w) if (table[i][j] == '@') {q.push({i, j}); table[i][j] = 'r';}
    while (!q.empty()) {
        int y = q.front().first;
        int x = q.front().second;
        q.pop();
        rep(i, 4) {
            int ny = y + dy[i];
            int nx = x + dx[i];
            if (ny < 0 || ny >= h || nx < 0 || nx >= w) continue;
            if (table[ny][nx] == '#' || table[ny][nx] == 'r') continue;
            q.push({ny, nx});
            table[ny][nx] = 'r';
            sum++;
        }
    }/*
    rep(i, h) {
        rep(j, w) cout << table[i][j] << " ";
        cout << endl;
    }*/
    cout << sum << endl;
    
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int h, w;
    while (cin >> w >> h && w) {
        datasets(h, w);
    }
}