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>

using namespace std;

int count;
int W = 0, H = 0;
char tyle[22][22];


void check(int x, int y);


int main(){

	int i, j;
	int startH, startW;


	while(1){

		count = 0;

		cin >> W >> H;

		if(W != 0 && H != 0){

			for(i = 0; i <= W; i++){
				tyle[i][0] = '#';
				tyle[i][H+1] = '#';
			}

			for(i = 0; i <= H; i++){
				tyle[0][i] = '#';
				tyle[W+1][i] = '#';
			}

			for(i = 1; i <= H; i++){
				for(j = 1; j <= W; j++){
					cin >> tyle[j][i];
					if(tyle[j][i] == '@'){
						startH = i;
						startW = j;
					}
				}
			}


			check(startW, startH);

			cout << count << endl;

		}

		else{break;}

	}

	return 0;
}




void check(int x, int y){

	count++;

	tyle[x][y] = '#';

	if(tyle[x][y-1] == '.'){
		check(x, y-1);
	}
	if(tyle[x-1][y] == '.'){
		check(x-1, y);
	}
	if(tyle[x][y+1] == '.'){
		check(x, y+1);
	}
	if(tyle[x+1][y] == '.'){
		check(x+1, y);
	}
}

C++: #include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
typedef long long ll;
int H, W;
bool visited[20][20];
int calc(vector<string> &vs, int y, int x) {
  if(y < 0 || y >= H || x < 0 || x >= W) return 0;
  if(visited[y][x] || vs[y][x] == '#') return 0;
  visited[y][x] = true;
  int ret = 1;
  ret += calc(vs, y+1,x);
  ret += calc(vs, y-1,x);
  ret += calc(vs, y,x+1);
  ret += calc(vs, y,x-1);
  return ret;
}

int main()
{
  while(cin >> W >> H) {
    if(W == 0) return 0;
    vector<string> vs(H);
    FOR(i,0,H) cin >> vs[i];
    int sx, sy;
    FOR(y,0,H) {
      FOR(x,0,W) {
        if(vs[y][x] == '@') {
          sx = x;
          sy = y;
        }
      }
    }
    FOR(i,0,H) FOR(j,0,W) visited[i][j] = false;
    cout << calc(vs, sy, sx) << endl;
  }
  return 0;
}

C++: #include <cstring>
#include <functional>
#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::cerr;
using std::endl;

const int vx[4] = {0, 1, 0, -1}, vy[4] = {1, 0, -1, 0};

int W, H;
std::string s[21];
bool is_searched[21][21];

int dfs(int x, int y) {
	int ret = 0;
	is_searched[x][y] = true;

	for(int i = 0; i < 4; ++i) {
		int nx = x + vx[i], ny = y + vy[i];

		if(0 <= nx && nx < H && 0 <= ny && ny < W &&
		   !is_searched[nx][ny] && s[nx][ny] == '.') {

			ret += dfs(nx, ny) + 1;
		}
	}

	return ret;
}

int solve() {
	int ret = 0;
	int sx, sy;

	std::memset(is_searched, false, sizeof is_searched);

	for(int i = 0; i < H; ++i) {
		for(int j = 0; j < W; ++j) {
			if(s[i][j] == '@') {
				sx = i, sy = j;
				break;
			}
		}
	}

	ret = dfs(sx, sy) + 1; // +1 は'@' の分

	return ret;
}

int main() {

	while(cin >> W >> H, W | H) {
		for(int i = 0; i < H; ++i) {
			cin >> s[i];
		}

		int ans = solve();

		cout << ans << endl;
	}

	return 0;
}

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

int w, h, ans;
int d[4][2] = { { 1, 0 }, { -1, 0 }, {0, 1}, {0, -1} };
char map[20][20];

void search_dfs(int x, int y){
	if( x < 0 || x >= w || y < 0 || y >= h ) return;
	if( map[y][x] != '.' ) return;
	ans ++;
	map[y][x] = '*';
	for(int i = 0; i < 4; i++)
		search_dfs( x + d[i][0], y + d[i][1] );
}

int main(void){

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

		for(int i = 0; i < h; i++)
			cin >> map[i];

		for(int i = 0; i < h; i++)
			for(int j = 0; j < w; j++)
				if( map[i][j] == '@' ) {
					map[i][j] = '.';
					search_dfs(j, i);
				}

		cout << ans << endl;
	}

	return 0;
}

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

int w, h;
char tiles[20][21];

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

bool isRegion(int x, int y){
    return x >= 0 && x < w && y >= 0 && y < h;
}

int main(){
    while(1){
        cin >> w >> h;
        if(!w && !h) break;
        
        int sx, sy;
        for(int i = 0; i < h; i++){
            cin >> tiles[i];
            
            auto j = find(tiles[i], tiles[i]+w, '@');
            if(j != tiles[i] + w){
                sx = j - tiles[i]; sy = i;
            }
        }
        
        int cnt = 0;
        queue<pair<int, int> > q;
        q.push(make_pair(sx, sy));
        while(!q.empty()){
            cnt++;
            int x = q.front().first, y = q.front().second;
            q.pop();
            
            for(int i = 0; i < 4; i++){
                int nx = x + dx[i], ny = y + dy[i];
                
                if(isRegion(nx, ny) && tiles[ny][nx] == '.'){
                    tiles[ny][nx] = '#';
                    q.push(make_pair(nx, ny));
                }
            }
        }
        cout << cnt << endl;
        
    }
}