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

int COUNT=0;

void dfs(vector<string> map,int state[20][20],int x,int y,int W,int H){
  int dx[4]={-1,0,1,0};
  int dy[4]={0,-1,0,1};
  if(x<0 || x>H-1 || y<0 || y>W-1)return; //????????????
  if(state[x][y]!=0 || map[x][y]=='#')return; //?????§????????£??????????????????????????????
  ++COUNT;
  state[x][y]=1;
  for(int i=0;i<4;++i)dfs(map,state,x+dx[i],y+dy[i],W,H); //??????????????¨??¢?´¢
}

int main(){
  int W,H,x,y;
  vector<string> map;
  string a;
  while(true){
    COUNT=0;
    cin >> W >>H;
  if(!W && !H)break;
  int state[20][20]={0}; //?????§????????£???????????????
  for(int i=0;i<H;++i){ //???????????\???
    cin >> a;
    map.push_back(a);
  }
  for(int i=0;i<H;++i){//????????????????????¢?´¢
    if(0<=map[i].find('@') && map[i].find('@')<W){
      y=map[i].find('@');
      x=i;
      break;
    }
  }
    dfs(map,state,x,y,W,H);
    cout << COUNT << endl;
    map.clear();
  }
}

C++: /*
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1130&lang=jp
*/

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const ll MOD = 1000000007;

const int di[4] = {+1, 0, -1, 0};
const int dj[4] = {0, -1, 0, +1};

int w, h;
string tiles[20];

int solve(int si, int sj){
    int res = 0;
    queue<pair<int, int> > Q;
    bool visited[h][w];
    memset(visited, false, sizeof visited);
    visited[si][sj] = true;
    res++;
    Q.push(make_pair(si, sj));
    while(!Q.empty()){
        pair<int, int> p = Q.front();
        Q.pop();
        int pi = p.first, pj = p.second;
        for(int k=0;k<4;k++){
            int ni = pi + di[k];
            int nj = pj + dj[k];
            if(ni < 0 || ni >= h || nj < 0 || nj >= w) continue;
            if(!visited[ni][nj] && tiles[ni][nj] == '.'){
                visited[ni][nj] = true;
                Q.push(make_pair(ni, nj));
                res++;
            }
        }
    }
    return res;
}

int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(false);
    while(1){
        cin >> w >> h;
        if(w == 0 && h == 0) break;
        int si, sj;
        for(int i=0;i<h;i++){
            cin >> tiles[i];
            for(int j=0;j<w;j++){
                if(tiles[i][j] == '@'){
                   si = i;  
                   sj = j;
                }
            }
        }
        int ans = solve(si, sj);
        cout << ans << endl;
    }
}

C++: #include <bits/stdc++.h>

#define FOR(i, a, b) for(long long int i=(a);i<=(b);i++)
#define RFOR(i, a, b) for(long long int i=(a);i>=(b);i--)
#define MOD 1000000007
#define INF 1000000000 //2000000000
#define LLINF 1000000000000000000 //9000000000000000000
#define PI 3.14159265358979

using namespace std;
typedef long long int ll;
typedef pair< long long int, long long  int> P;

vector<string> c(20);

int dfs(int x, int y,int h,int w) {
	int point = 1;
	if (c[y][x] == '#') {
		return 0;
	}
	c[y][x] = '#';
	if (y + 1 <= h - 1) {
		point+=dfs(x, y + 1, h, w);
	}
	if (y - 1 >= 0) {
		point+=dfs(x, y - 1, h, w);
	}
	if (x + 1 <= w - 1) {
		point+=dfs(x + 1, y, h, w);
	}
	if (x - 1 >= 0) {
		point+=dfs(x - 1, y, h, w);
	}
	return point;
}

int main(void) {
	
	while (1) {
		int h, w;
		int posx = -1, posy = -1;
		int count = 0;

		cin >> w >> h;
		if (h == 0) {
			break;
		}
		

		FOR(i, 0, h - 1) {
			cin >> c[i];
		}
		FOR(i, 0, h - 1) {
			FOR(t, 0, w - 1) {
				if (c[i][t] == '@') {
					posx = t;
					posy = i;
					break;
				}
			}
			if (posx != -1) {
				break;
			}
		}
		
		cout << dfs(posx, posy, h, w) << endl;
	}

}

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

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

int ans;
vector<string> f;

void solve(int x, int y)
{
  if (x < 0 || x >= f[0].size())
    return;
  if (y < 0 || y >= f.size())
    return;
  if (f[y][x] == '#')
    return;
  
  ++ans;
  f[y][x] = '#';

  for (int i = 0; i < 4; ++i)
    solve(x+dx[i], y+dy[i]);
}

int main()
{
  int W, H, x, y;
  for ( ; ; ) {
    cin >> W >> H;
    if (W == 0 && H == 0)
      break;

    f.clear();
    for (int i = 0; i < H; ++i) {
      string tmp;
      cin >> tmp;
      int a = tmp.find_first_of("@", 0);
      if (a != string::npos) {
	x = a;
	y = i;
      }
      f.push_back(tmp);
    }
    ans = 0;
    solve(x, y);
    cout << ans << endl;
  }
  return 0;
}