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<iomanip>
#include<string>
#include<cstdlib>
#include<cmath>
#include<algorithm>

using namespace std;

int H, W;
int table[20][20];
int cnt;

void calc(int current_y, int current_x) {
	table[current_y][current_x] = 3;

	if(current_y > 0 && table[current_y-1][current_x] == 1)
		calc(current_y-1, current_x);
	if(current_x > 0 && table[current_y][current_x-1] == 1)
		calc(current_y, current_x-1);
	if(current_y < H-1 && table[current_y+1][current_x] == 1)
		calc(current_y+1, current_x);
	if(current_x < W-1 && table[current_y][current_x+1] == 1)
		calc(current_y, current_x+1);

	cnt++;
}


int main() {
	while(1) {
		cnt = 0;
		cin >> W >> H;
		if(W == 0 && H == 0) break;

		char temp;
		int current[2];
		for(int i=0; i<H; i++) {
			for(int j=0; j<W; j++) {
				cin >> temp;
				if(temp=='.') table[i][j] = 1;
				else if(temp=='#') table[i][j] = 2;
				else if(temp=='@') {
					table[i][j] = 0;
					current[0] = i;
					current[1] = j;
				}
			}
		}

		calc(current[0], current[1]);

		cout << cnt << endl;
	}

	return 0;
}

C++: #include<bits/stdc++.h>
#include<queue>
using namespace std;
int W,H;
char c[101][101];
bool visited [100][100];

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

void compute(){
  memset(visited,false,sizeof visited);


  queue<int> que;
int i,j;
 for(i=0;i<H;++i){
   for(j=0;j<W;++j){
     if(c[i][j]=='@'){
       que.push(j+i*W);
       visited[i][j]=true;
       break;}
       
	 }


   }
 int answer=1;
 
 while(!que.empty()){
   int cur = que.front();
   que.pop();
   int x = cur % W,y=cur/W;

   for(i=0;i<4;i++)
     {int nx = x + dx[i],ny= y+dy[i];
       if(!(0<=nx && nx< W && 0<=ny && ny < H))continue;
       if(c[ny][nx]=='#'||visited[ny][nx])continue;
       
       que.push(nx+ny*W);
       visited[ny][nx]=true;
       ++answer;
       
     }
 }
 cout<<answer<<endl;
 
}





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

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

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

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

int dfs(int i, int j) {
   B[i][j] = '#';
   int res = 1;
   for (int k = 0; k < 4; k++) {
      int y = i + dy[k], x = j + dx[k];
      if (y >= 0 && y < h && x >= 0 && x < w && B[y][x] == '.') res += dfs(y, x);
   }
   return res;
}

int main() {
   for (; scanf("%d%d", &w, &h), w;) {
      for (int i = 0; i < h; i++) {
         scanf("%s", B[i]);
      }
      for (int i = 0; i < h; i++) {
         for (int j = 0; j < w; j++) {
            if (B[i][j] == '@') printf("%d\n", dfs(i, j));
         }
      }
   }
}

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

int w, h, ret;
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
string data[25];

void func(int nx, int ny)
{
	ret++;
	data[ny][nx] = '#';
	if (nx+1 < w && data[ny][nx+1] == '.') func(nx+1, ny);
	if (0 <= nx-1 && data[ny][nx-1] == '.') func(nx-1, ny);
	if (ny+1 < h && data[ny+1][nx] == '.') func(nx, ny+1);
	if (0 <= ny-1 && data[ny-1][nx] == '.') func(nx, ny-1);
	
	return;
}

int main(void)
{
	while (cin >> w >> h, w && h){
		for (int i = 0; i < 25; i++) data[i].clear();
		for (int i = 0; i < h; i++) {
			cin >> data[i];
		}
		int sx, sy;
		for (int i = 0; i < w; i++){
			for (int j = 0; j < h; j++){
				if (data[j][i] == '@'){
					sx = i, sy = j;
					break;
				}
			}
		}
		ret = 0;
		data[sy][sx] = '#';
		func(sx, sy);
		cout << ret << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>

using namespace std;

int tile[22][22] = {};
int w, h;
int cnt;

void fill(int x, int y) {
	tile[x][y] = 0;
	cnt++;
	if (tile[x - 1][y] == 1) fill(x - 1, y);
	if (tile[x][y - 1] == 1) fill(x, y - 1);
	if (tile[x][y + 1] == 1) fill(x, y + 1);
	if (tile[x + 1][y] == 1) fill(x + 1, y);
}

int main() {
	int x = 0, y = 0;
	while (cin >> w >> h && (w || h)) {
		cnt = 0;
		for (int i = 1; i <= h; ++i){
			for (int j = 1; j <= w; ++j) {
				char tmp;
				cin >> tmp;
				if (tmp == '.') tile[i][j] = 1;
				else if (tmp == '#') tile[i][j] = 0;
				else x = i, y = j, tile[i][j] = 1;
			}
		}
		fill(x, y);
		cout << cnt << endl;
	}

	return 0;
}