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

typedef pair<int,int> P;

int w, h, sx, sy;
char t[22][22];

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

void solve(){
  stack<P> open;
  int ans = 0;

  open.push(P(sx, sy));
  t[sy][sx] = '#';

  while(!open.empty()){
    P p = open.top();
    open.pop();
    ans++;

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

      if(nx < 0 || w <= nx || ny < 0 || h <= ny || t[ny][nx] != '.') continue;
      t[ny][nx] = '#';
      open.push(P(nx, ny));
    }
  }

  cout << ans << endl;
}

int main(){
  while(cin >> w >> h, w || h){
    for(int i = 0; i < h; i++){
      cin >> t[i];
      for(int j = 0; j < w; j++){
        if(t[i][j] == '@'){
          sx = j;
          sy = i;
          t[i][j] = '.';
        }
      }
    }
    solve();
  }
}

C++: #include <iostream>

#include <string>
#include <queue>

using namespace std;

int main(){
	int w,h;
	int data[30][30];
	int flag[30][30];
	int dx[4] ={0,1,0,-1};
	int dy[4] ={1,0,-1,0};

	while(1){
		cin>>w>>h;
		if(w==0 && h==0)break;

	for(int i=0;i<30;i++){
		for(int j=0;j<30;j++){
			data[i][j]=-1;
			flag[i][j]=0;
		}
	}

	char tmp;
	int startX;
	int startY;
	int cnt=0;

	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			cin>>tmp;
			if(tmp=='.')data[i][j] = 0;
			else if(tmp=='@'){
				startX = j;
				startY = i;
			}
		}
	}

	queue<int> qX,qY;
	qX.push(startX);
	qY.push(startY);

	while(qX.size()!=0){
		int topX = qX.front();
		int topY = qY.front();
		qX.pop();
		qY.pop();
		cnt++;

		for(int i=0;i<4;i++){
			int nowX = topX + dx[i];
			int nowY = topY + dy[i];

			if(data[nowY][nowX] ==0 && flag[nowY][nowX] == 0){
				qX.push(nowX);
				qY.push(nowY);
				flag[nowY][nowX]=1;
			}
		}
	}

	cout<<cnt<<endl;
	}

	return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <stack>
#include <queue>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
static const double EPS = 1e-5;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
int c=0,W,H;
vector<string> grid;
string tmp;
const int dx[4] = {0,1,0,-1};
const int dy[4] = {1,0,-1,0}; 
void dfs(int x, int y){
  c++;
  grid[y][x]='#';
  REP(r,4){
    if(x+dx[r]>=0&&x+dx[r]<W
        &&y+dy[r]>=0&&y+dy[r]<H
        &&grid[y+dy[r]][x+dx[r]]=='.'){
      dfs(x+dx[r],y+dy[r]);
    }
  }
}

int main(void){
  while(cin>>W>>H){
    if(W==0) break;
    grid.clear();
    c = 0;
    REP(y,H){
      cin>>tmp;
      grid.push_back(tmp);
    }
    REP(y,H)REP(x,W){
      if(grid[y][x]=='@'){
        dfs(x,y);
      }
    }
    cout<<c<<endl;
  }
  return 0;
}

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

#define REP(i, n) for(int i = 0;i < (n);i++)
#define FOR(i, a, b) for(int i = (a);i < (b);i++)
#define all(v) v.begin(),v.end()

using int64 = long long;

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

int main(void){
	int H, W;
	while(cin >> H >> W && H && W){
		vector<string> f(H+2, string(W+2, '#'));
		int sy, sx;
		REP(i, H){
			cin >> f[i+1];
			REP(j, W+2)
				if(f[i+1][j] == '@'){
					sy = i+1;
					sx = j;
				}
			f[i+1][0] = f[i+1][W+1] = '#';
		}
		bool visited[30][30] = {};
		int cnt = 0;
		
		auto dfs = [&](int y, int x){
			visited[y][x] = 1;
			cnt++;
			REP(i, 4){
				int yy = y+dy[i], xx = x+dx[i];
				if(f[yy][xx] != '#' && !visited[yy][xx]){
					dfs(yy, xx);
				}
			}
		};
		dfs(sy, sx);
		cout << cnt << endl;
	}
}'

C++: #include <iostream>
#include <queue>
#include <string>

using namespace std;

int main(){
  int w,h,count;
  queue<int> que;
  int xx[4]={1,0,-1,0};
  int yy[4]={0,1,0,-1};
  int pos;
  string tt;
  while(true){
    cin >> w >> h;
    if(!w&&!h)
      break;
    int mmap[w+2][h+2];
    for(int x=0;x<w+2;x++){
      mmap[x][0] = -1;
      mmap[x][h+1] = -1;
    }
    for(int y=0;y<h+2;y++){
      mmap[0][y] = -1;
      mmap[w+1][y] = -1;
    }
        
    for(int y=0;y<h;y++){
      cin >> tt;
      for(int x=0;x<w;x++){
	switch(tt[x]){
	case '.':
	  mmap[x+1][y+1] = 0;
	  break;
	case '#':
	  mmap[x+1][y+1] = -1;	  
	  break;
	case '@':
	  mmap[x+1][y+1] = 1;
	  que.push(y*w+x);
	  break;
	}
      }
    }
    count = 1;
    while(!que.empty()){
      pos = que.front();
      for(int dir=0;dir<4;dir++){
	if (mmap[pos%w+xx[dir]+1][pos/w+yy[dir]+1]==0){
	  que.push(pos+yy[dir]*w+xx[dir]);
	  mmap[pos%w+xx[dir]+1][pos/w+yy[dir]+1] = 1;
	  count++;
	}
      }
      que.pop();
    }
    cout << count << endl;
  }
  return 0;
}