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 reps(v, f, l) for (int v = (f), v##_ = (l); v < v##_; ++v)
#define rep(v, n) reps(v, 0, n)
#define lep(v, n) reps(v, 1, n + 1)

using namespace std;

typedef long long int lint;

static inline int in(){ int x; scanf("%d", &x); return x; }
static inline lint inl(){ lint x; scanf("%lld", &x); return x; }
template<typename T> void show(T& a, char del='\n', char last='\n'){ rep(i, a.size() - 1) cout << a[i] << del; cout << a[a.size() - 1] << last; }

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

struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) {}
  bool merge(int x, int y){
    x = root(x), y = root(y);
    if (x != y){
      if (data[x] > data[y]) swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  bool isSame(int x, int y){
    return root(x) != root(y);
  }
  int size(int x){
    return -data[root(x)];
  }
  int root(int x){
    return data[x] < 0 ? x : data[x] = root(data[x]);
  }
};

int main()
{
  int w, h;
  while (w = in(), h = in(), w){
    UnionFind uf(w * h);
    vector<string> field;
    rep(i, h){
      string s;
      cin >> s;
      field.push_back(s);
    }
    int sx, sy;
    rep(y, h){
      rep(x, w){
        char cell = field[y][x];
        if (cell == '#') continue;
        if (cell == '@') {
          sx = x;
          sy = y;
        }
        rep(d, 4){
          int nx = x + dx[d];
          int ny = y + dy[d];
          if (nx < 0 || nx >= w || ny < 0 || ny >= h) continue;
          if (field[ny][nx] != '#') uf.merge(y * w + x, ny * w + nx);
        }
      }
    }
    printf("%d\n", uf.size(sy * w + sx));
  }
  return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<string>
#include<cstdlib>
#include<map>
#include<iomanip>
#include<sstream>
#include<vector>
#include<stack>
#include<math.h>
#include<queue>
#include<complex>
#include<random>
#include<ctime>
#include<set>
using namespace std;


const long long int mod=1000000007;
const long long int INF=99999999999999999;


long long int h,w;
string str[25];
int main() {
	cout << fixed << setprecision(18);
	cin>>w>>h;
	while(w!=0){
		for(int i=0;i<h;i++){
			cin>>str[i];
		}
		long long int start_h,start_w;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(str[i][j]=='@'){
					start_h=i;
					start_w=j;
				}
			}
		}
		long long int num=1;
		queue<pair<long long int,long long int> >task;
		task.push({start_h,start_w});
		bool used[25][25]={};
		used[start_h][start_w]=true;
		while(!task.empty()){
			long long int hnow=task.front().first;
			long long int wnow=task.front().second;
			if(hnow!=0&&str[hnow-1][wnow]=='.'&&!used[hnow-1][wnow]){
				used[hnow-1][wnow]=true;
				num++;
				task.push({hnow-1,wnow});
			}
			if(hnow!=h-1&&str[hnow+1][wnow]=='.'&&!used[hnow+1][wnow]){
				used[hnow+1][wnow]=true;
				num++;
				task.push({hnow+1,wnow});
			}
			if(wnow!=0&&str[hnow][wnow-1]=='.'&&!used[hnow][wnow-1]){
				used[hnow][wnow-1]=true;
				num++;
				task.push({hnow,wnow-1});
			}
			if(wnow!=w-1&&str[hnow][wnow+1]=='.'&&!used[hnow][wnow+1]){
				used[hnow][wnow+1]=true;
				num++;
				task.push({hnow,wnow+1});
			}
			//cout<<"DEBUG"<<hnow<<" "<<wnow<<endl;
			task.pop();
		}
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				//cout<<used[i][j]<<" ";
			}
			//cout<<endl;
		}
		cout<<num<<endl;
		cin>>w>>h;
	}
}

C++: #include<iostream>
using namespace std;
#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n)   REP(i,0,n)

const int N = 500;
bool vis[N][N];
char m[N][N];
const int dx[]={0,0,1,-1};
const int dy[]={1,-1,0,0};
int cnt;

void dfs(int r,int c,int y,int x){
  if (vis[y][x])return;
  vis[y][x]=true;
  cnt++;
  rep(i,4){
    int nex=x+dx[i],ney=y+dy[i];
    if (nex == -1 || ney == -1 || nex == c || ney == r ||
	m[ney][nex] == '#')continue;
    dfs(r,c,ney,nex);
  }


}

main(){
  int c,r;
  while(cin>>c>>r){
    if (c == 0 && r == 0)break;
    int sy=0,sx=0;
    cnt=0;
    rep(i,r)rep(j,c)vis[i][j]=false;
    rep(i,r){
      cin>>m[i];
      rep(j,c)if (m[i][j] == '@')sy=i,sx=j;
    }
    dfs(r,c,sy,sx);
    cout << cnt << endl;
  }
  return false;
}