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 <string>
#include <algorithm>
using namespace std;
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REP(i,b) FOR(i,0,b)
#define INF 1e9

int W,H;
int t_situ[20][20];
int dx[] = {1,0,-1,0}, dy[] = {0,1,0,-1};
int x_start, y_start, Count;

void move(int x_standard, int y_standard){
  if(t_situ[y_standard][x_standard] == 2 && x_standard >=0 && x_standard < W
      && y_standard >=0 && y_standard < H){
    t_situ[y_standard][x_standard] = 1;
    Count++;
    REP(k,4) move(x_standard+dx[k], y_standard+dy[k]);
  }/*else if(t_situ[y_standard][x_standard] == 1 )
      REP(k,4) move(x_standard+dx[k]+dy[k], y_standard+dx[k]+dy[k]);*/
}

void solve(int X, int Y){
  Count = 0;
  char t_type[Y][X];
  REP(i,20) REP(j,20) t_situ[i][j] = 0;
  //0:red, 1:black&gone, 2:black&not_gone

  REP(i,Y){
    REP(j,X){
      cin>>t_type[i][j];
      switch (t_type[i][j]) {
        case '@':
            t_situ[i][j] = 2;
            x_start = j;
            y_start = i;
            break;
        case '.':
            t_situ[i][j] = 2;
            break;
        case '#':
            t_situ[i][j] = 0;
      }
    }
  }

  move(x_start, y_start);

  cout<<Count<<endl;
  /*REP(i,20){
    REP(j,20){
      cout<<t_situ[i][j];
    }
    cout<<endl;
  }*/
}

int main(){

  while(1) {
    cin>>W>>H;
    if(W == 0 && H == 0) break;
    solve(W,H);
  }

  return 0;
}

C++: #include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
#include <iomanip>
#include <iostream>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define REP(i,m,n) for (int i=m;i<(int)(n);i++)

const int INF = 10000000;
int dx[4]={1, 0, -1, 0}, dy[4]={0, 1, 0, -1};
using namespace std;
typedef long long  ll;
typedef pair<int, int> P;

/** Problem 1130 : Red and Black **/
int main()
{
	bool field[20][20];
	bool vis[20][20];
	int W, H, sx, sy; char c;
	
	while (cin >> W >> H, W||H) {
		int ans = 0;
		
		rep(y, H) {
			rep(x, W) {
				cin >> c;
				if (c=='.') {
					field[x][y]=true;
				} else if (c=='@') {
					field[x][y]=true;
					sx = x; sy = y;
				} else {
					field[x][y] = false;
				}
				vis[x][y] = false;
			}
		}
		
		queue<P> Q;
		Q.push(P(sx, sy));
		
		while (Q.size()!= 0) {
			P p = Q.front(); Q.pop();
			if (!vis[p.first][p.second]) {
				ans++;
				vis[p.first][p.second]=true;
			}
			
			rep(i, 4) {
				int nx = p.first+dx[i], ny=p.second+dy[i];
				if (0<= nx && nx < W && 0 <= ny && ny < H && field[nx][ny] && !vis[nx][ny])
					Q.push(P(nx, ny));
			}
		}
		cout << ans << endl;
	}
	return 0;
}

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

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

char field[20][20];
bool flag[20][20];
int W, H, ans;

void dfs(int x, int y) {
  //field[x][y] = '.';
  //cout << x << ' ' << y << endl;
  //cout << flag[x][y] << endl;
  //cout << ans << endl;
  for(int i = 0 ; i < 4 ; i++) {
    int nx = x + dx[i], ny = y + dy[i];
    if(0 <= nx && nx < H && 0 <= ny && ny < W && field[nx][ny] == '.' &&
       flag[nx][ny] == false) {
      ans++; flag[nx][ny] = true; dfs(nx, ny);
      //cout << "OK" << endl;
    }
    //else dfs(nx, ny);
  }
}

int main(void) {
  int x, y;
  int res;
  
  while(cin >> W >> H, W != 0 && H != 0) {
    memset(flag, false, sizeof(flag));
    ans = 1;
    for(int i = 0 ; i < H ; i++) {
      for(int j = 0 ; j < W ; j++) {
	cin >> field[i][j];
	if(field[i][j] == '@') x = i, y = j;
      }
    }
    /*for(int i = 0 ; i < H ; i++) {
      for(int j = 0 ; j < W ; j++) {
	cout << field[i][j];
      }
      cout << endl;
      }*/
    //printf("@x = %d, @y = %d\n", x, y);
    dfs(x, y);
    cout << ans << endl;
  }
  return 0;
}