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 <cstdio>
using namespace std;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int a,b,sum=0;
int y[21][21]={};
char x[21][21];
void dfs(int aa,int bb){
for(int i=0;i<4;i++){
  if(aa+dx[i]>=0 && aa+dx[i]<a && bb+dy[i]>=0 && bb+dy[i]<b && y[bb+dy[i]][aa+dx[i]]==0){
y[bb+dy[i]][aa+dx[i]]=1;
dfs(aa+dx[i],bb+dy[i]);
}
}
}
int main(){
int c,d;
while(1){
scanf("%d %d",&a,&b);
if(a==0 && b==0) break;
for(int i=0;i<b;i++){
scanf("%s",x[i]);
}
for(int i=0;i<b;i++){
for(int j=0;j<a;j++){
if(x[i][j]=='.'){
y[i][j]=0;
}else if(x[i][j]=='#'){
y[i][j]=2;
}else{
	y[i][j]=1;
c=j;
d=i;
}
}}
dfs(c,d);
for(int i=0;i<b;i++){
  for(int j=0;j<a;j++){
    if(y[i][j]==1){
      sum++;
    }
  }
}
printf("%d\n",sum);
sum=0;
}
return 0;
}

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

int h, w, ans;
char field[25][25];

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

void dfs(int x, int y) {
  if(field[y][x] == '.' && 0 <= x && x < w && 0 <= y && y < h) {
    field[y][x] = '#';
    ans++;
    for(int i = 0; i < 4; i++) {
      int nx = x+dx[i], ny = y+dy[i];
      dfs(nx, ny);
    }
  }
}

int main() {
  int sx, sy;
  while(cin >> w >> h, w+h) {
    for(int i = 0; i < h; i++) {
      for(int j = 0; j < w; j++) {
	cin >> field[i][j];
	if(field[i][j] == '@') {
	  sx = j;
	  sy = i;
	  field[i][j] = '.';
	}
      }
    }

    ans = 0;
    dfs(sx, sy);
    cout << ans << endl;
  }
}

C++: #include<iostream>
#include<iomanip>
#include<algorithm>
#include<string>
#include<map>
#include<cmath>
#include<queue>	
#include<functional>

using namespace std;

namespace MyLib {
#define ALL(a) a.begin(),a.end()
#define REP(i,n) for(int i=0;i<n;i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)
#define debug(x) if(1)cout<<#x<<":"<<x<<endl;
#define DEBUG(x) if(1)cout<<#x<<":"<<x<<endl;
#define ll long long
}

using namespace MyLib;

int dfs(int i, int j);
int t[20][20];
int b[20][20];
int w, h;


int main() {

	int x, y;
	string buf;
	while (cin >> w >> h, w) {
		REP(i, 20)
			fill(b[i],b[i]+20, 0);
		REP(i, h) {
			cin >> buf;
			REP(j, w) {
				if (buf[j] == '#')
					t[i][j] = 0;
				else
					t[i][j] = 1;
				if (buf[j] == '@')
					y = i, x = j;
			}
		}
		cout << dfs(y, x)-1 << endl;



	}
	
}

int dfs(int i, int j) {
	int a, x, c, d;
	a = x = c = d = 0;
	if (0 <= i - 1 && b[i - 1][j] == 0 && t[i - 1][j] == t[i][j]) {
		b[i - 1][j] = 1;
		a = dfs(i - 1, j);
	}
	if (h > i + 1 && b[i + 1][j] == 0 && t[i + 1][j] == t[i][j]) {
		b[i + 1][j] = 1;
		x = dfs(i +1, j);
	}
	if (0<= j- 1 && b[i][j-1] == 0 && t[i][j-1] == t[i][j]) {
		b[i][j-1] = 1;
		c = dfs(i , j-1);
	}
	if (w > j + 1 && b[i ][j+1] == 0 && t[i ][j+1] == t[i][j]) {
		b[i ][j+1] = 1;
		d = dfs(i , j+1);
	}
	return a + x + c + d + 1;



}

C++: #include <iostream>
#include <queue>
#include <utility>
#include <array>

using namespace std;

typedef pair<int, int> pii;

int height, width;
int sx, sy;


void bfs(vector<vector<char>>& field)
{
    int ans = 1;
    array<int, 4> dx = {0, 0, 1, -1};
    array<int, 4> dy = {1, -1, 0, 0};

    queue<pii> p;
    p.push({sx, sy});
    
    while (!p.empty()) {
        pii q = p.front(); p.pop();
        
        //??????
        int cx = q.first;
        int cy = q.second;

        for (int i = 0; i < 4; ++i) {
            int nx = cx + dx.at(i);
            int ny = cy + dy.at(i);

            if (nx >= 0 && nx < width && ny >= 0 && ny < height 
                    && field.at(ny).at(nx) == '.') {

                ans++;
                field.at(ny).at(nx) = '#';
                p.push({nx, ny});
            }
        }
    }

    std::cout << ans << std::endl;
}


int main()
{
    while (cin >> width >> height) {
        if (height == 0 && width == 0) break;

        vector<vector<char>> field(height, vector<char>(width));

        for (int y = 0; y < height; ++y) {
            for (int x = 0; x < width; ++x) {
                cin >> field.at(y).at(x);

                if (field.at(y).at(x) == '@') {
                    sx = x; sy = y;
                }
            }
        }

        bfs(field);
    }

    return 0;
}