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 <algorithm>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <map>

#define FOR(i,k,n) for(int (i) = (k); (i) < (n); ++(i))
#define rep(i,n) FOR(i,0,n)

using namespace std;
typedef pair<int, int> i_i;

int a,w,h;
bool t[22][22],s[22][22];

void dfs(i_i p) {
    s[p.first][p.second] = 1;
    a += 1;
    rep(j,2){
        if(t[p.first][p.second+(int)pow(-1,j)] && s[p.first][p.second+(int)pow(-1,j)] == 0) {
            dfs(make_pair(p.first,p.second+(int)pow(-1,j)));
        }
    }
    rep(i,2) {
        if(t[p.first+(int)pow(-1,i)][p.second] && s[p.first+(int)pow(-1,i)][p.second] == 0) {
            dfs(make_pair(p.first+(int)pow(-1,i),p.second));
        }
    }
}

int main() {
    while(1) {
        i_i m;
        scanf("%d %d",&w,&h);
        if(w == 0) break;
        FOR(i,1,h+1){
            char a[20];
            scanf("%s",a);
            FOR(j,1,w+1) {
                if(a[j-1] == '.') t[i][j] = 1; 
                if(a[j-1] == '@') m = make_pair(i,j);
            }
        }
        dfs(m);
        printf("%d\n",a);
        a = 0;
        rep(i,22) {
            rep(j,22) {
                t[i][j] = 0,s[i][j] =0;
            }
        }
    }
    return 0;
}

C++: // PKU 1979
#include<cstdio>
#define rep( i, a ) for( int i = 0; i != (a); ++i )

int W, H;
char fld[20][20];
int ans;

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

void dfs( int x, int y )
{
	++ans;
	fld[y][x] = ' ';

	rep( i, 4 )
	{
		int nx = x + dx[i], ny = y + dy[i];
		
		if( nx >= 0 && nx < W && ny >= 0 && ny < H && fld[ny][nx] == '.' )
			dfs( nx, ny );
	}

	return;
}

int main()
{
	while( scanf( "%d%d", &W, &H ), W | H )
	{
		ans = 0;
		int sx, sy;

		rep( i, H )
		{
			getchar();

			rep( j, W )
			{
				fld[i][j] = getchar();
			
				if( fld[i][j] == '@' )
					sx = j, sy = i;
			}
		}

		dfs( sx, sy );

		printf( "%d\n", ans );
	}

	return 0;
}

C++: #include<iostream>
#include<queue>
using namespace std;
int w,h;
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
main()
{
	while(cin>>w>>h,w)
	{
		queue<pair<int,int> >P;
		string s[22];
		int cnt=1;
		bool used[22][22]={};
		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]=='@')
			{
				P.push(make_pair(i,j));
				used[i][j]=1;
			}
		}
		while(!P.empty())
		{
			int x=P.front().first,y=P.front().second;P.pop();
			for(int r=0;r<4;r++)
			{
				int tx=dx[r]+x,ty=y+dy[r];
				if(tx<0||ty<0||tx>=h||ty>=w||s[tx][ty]=='#'||used[tx][ty])continue;
				used[tx][ty]=1;
				P.push(make_pair(tx,ty));
				cnt++;
			}
		}
		cout<<cnt<<endl;
	}
}

C++: #include <iostream>

int H, W;
int map[20][20];
int c;
using namespace std;

void printArray();
void check(int, int);

int main() {
  cin  >> W >> H;
  int sy, sx;
  for (int i = 0; i < H; i++) {
    string line;
    cin >> line;
    for (int j = 0; j < W; j++) {
      if (line[j] == '@') {
        sy = i;
        sx = j;
        map[i][j] = 0;
      } else if (line[j] == '.') {
        map[i][j] = 0;
      } else if (line[j] == '#') {
        map[i][j] = 1;
      }
    }
  }
  // printArray();
  c = 0;
  check(sy, sx);
  // printArray();
  cout << c << endl;
  return 0;
}

void check(int y, int x) {
  if (y < 0 || x < 0 || y >= H || x >= W || map[y][x] == 1) {
    return;
  }
  map[y][x] = 1;
  c++;
  check(y + 1, x);
  check(y - 1, x);
  check(y, x + 1);
  check(y, x - 1);
}

void printArray() {
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cout << map[i][j];
    }
    cout << endl;
  }
}

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

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

void solve(int x,int y){

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

  if(x+1 < h) solve(x+1,y);
  if(y+1 < w) solve(x,y+1);
  if(x-1 >= 0) solve(x-1,y);
  if(y-1 >= 0) solve(x,y-1);
   
  return;
}


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;
  }
}