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>
#define rep(i,n) for(ll i=0;i<n;++i)
#define rrep(i,n) for(int i=n-1;i>=0;--i)
#define yesno(flg) if(flg){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}
#define MAX_N 1002
#define i197 1000000007
#define Vi vector<int>
#define Vl vector<long long>
#define pb(a) push_back(a)

using namespace std;

typedef long long ll;
typedef pair<ll,ll> P1;
typedef pair<int,int> Pi;
typedef pair<double,Pi> Pdi;
typedef pair<ll,int>Pli;
typedef pair<P1,ll> P2;
const ll INF=10000000000000000001;
int dy[]={0, 0, 1, -1};
int dx[]={1, -1, 0, 0};
struct Road{double cost;int a,b;};
struct edge{int to,cost;};
struct pos{
	int x,y;
};

int main() {

	while(1){
		int h,w;
		char c[21][21]={};
		int dp[21][21]={},ans=0;
		cin>>h>>w;
		if(h==0&&w==0)return 0;
		queue<pos> que;
		int si=0,sj=0;
		rep(i,w){
			rep(j,h){
				cin>>c[j][i];
				if(c[j][i]=='@'){
					dp[j][i]=1;
					sj=j;
					si=i;
				}
			}
		}
		pos pos1={sj,si};
		que.push(pos1);
		while(!que.empty()){
			pos pos2=que.front();que.pop();
			ans++;
			if(c[pos2.x+1][pos2.y]=='.'&&dp[pos2.x+1][pos2.y]==0){
				pos pos3={pos2.x+1,pos2.y};
				que.push(pos3);
				dp[pos2.x+1][pos2.y]=1;
			}
			if(c[pos2.x-1][pos2.y]=='.'&&dp[pos2.x-1][pos2.y]==0){
				pos pos4={pos2.x-1,pos2.y};
				que.push(pos4);
				dp[pos2.x-1][pos2.y]=1;
			}
			if(c[pos2.x][pos2.y+1]=='.'&&dp[pos2.x][pos2.y+1]==0){
				pos pos5={pos2.x,pos2.y+1};
				que.push(pos5);
				dp[pos2.x][pos2.y+1]=1;
			}
			if(c[pos2.x][pos2.y-1]=='.'&&dp[pos2.x][pos2.y-1]==0){
				pos pos6={pos2.x,pos2.y-1};
				que.push(pos6);
				dp[pos2.x][pos2.y-1]=1;
			}
		}
		cout<<ans<<endl;
	}

	return 0;
}

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

#define rep(i,N) for(int i=0;i<(int)(N);i++)
#define REP(i,N,k) for(int i=k;i<(int)(N);i++)

#define vi vector<int>
#define pb push_back

#define pii pair<int,int>
#define fi first
#define se second
int main(){


//    ifstream in("b_in.txt");
//    cin.rdbuf(in.rdbuf());

    int w,h;
    while(cin >> w >> h,w){
        int sx,sy;
        string s[30];
        rep(i,h) cin >> s[i];
        rep(y,h)rep(x,w) if(s[y][x]=='@') s[y][x]='.',sx=x,sy=y;

        queue<pii> que;
        que.push(pii(sx,sy));
        bool u[30][30] = {};
        u[sy][sx] = true;
        int cnt = 1;
        while(!que.empty()){
            int x,y;
            tie(x,y) = que.front();
            que.pop();
            rep(i,4){
                const int dx[] = {0,1,0,-1,0};
                int nx = x + dx[i];
                int ny = y + dx[i+1];
                if(nx < 0 || w <= nx || ny < 0 || h <= ny) continue;
                if(s[ny][nx] == '#') continue;
                if(u[ny][nx]) continue;
                u[ny][nx] = true;
                cnt += 1;
                que.push(pii(nx,ny));
            }
        }
        cout << cnt << endl;
    }

    return 0;
}

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

string s[21];
int w, h, vx[4] = {1, 0, -1, 0}, vy[4] = {0, 1, 0, -1};
bool is_search[21][21];

int dfs(int x, int y) {
  //cout << x << " " << y << endl;
  is_search[x][y] = true;
  int ret = 0, cnt = 0;
  for(int i = 0; i < 4; ++i) {
    if(0 <= x+vx[i] && 0 <= y+vy[i] && x+vx[i] < h && y+vy[i] < w && s[x+vx[i]][y+vy[i]] != '#' && is_search[x+vx[i]][y+vy[i]] == false) {
      ret += dfs(x+vx[i], y+vy[i]);
      cnt++;
    }
  }
  //cout << ret+cnt << endl;
  return ret + cnt;
}

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

  int ans, sx, sy;
  bool flag;

  while(cin >> w >> h, w | h) {
    ans = 0; flag = false;
    for(int i = 0; i < h; ++i) fill(is_search[i], is_search[i] + w, false);
    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] == '@') {
          sx = i; sy = j;
          flag = true;
          break;
        }
      }
      if(flag) break;
    }

    ans = dfs(sx, sy) + 1;

    cout << ans << endl;
  }
  return 0;
}

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

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

int W, H;
vector<string> field;

int dfs(int y, int x) {
    int ans = 1; field[y][x] = '#';
    for (int k = 0; k < 4; k++) {
        int ny = y + dy[k], nx = x + dx[k];
        if (0 <= ny && ny < H && 0 <= nx && nx < W) {
            if (field[ny][nx] == '.') ans += dfs(ny, nx);
        }
    }
    return ans;
}

int main() {
    while (cin >> W >> H, W) {
        field.resize(H);
        for (int i = 0; i < H; i++) cin >> field[i];
        for (int y = 0; y < H; y++) for (int x = 0; x < W; x++) {
            if (field[y][x] == '@') cout << dfs(y, x) << endl;
        }
    }
    return 0;
}