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>

using namespace std;
int W, H;
string s[20];
int num;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {-1, 0, 1, 0};

void dfs(int x, int y){
  num++;
  s[y][x] = '#';
  for(int i=0; i<4; i++){
    if(0<=x+dx[i] && x+dx[i]<W && 0<=y+dy[i] && y+dy[i]<H && s[y+dy[i]][x+dx[i]]=='.'){
      dfs(x+dx[i], y+dy[i]);
    }
  }
}

int main(){
  while(cin >> W >> H, W || H){
    for(int i=0; i<H; i++){
      cin >> s[i];
    }
    int x, y;
    for(int i=0; i<H; i++){
      for(int j=0; j<W; j++){
	if(s[i][j] == '@'){
	  x = j;
	  y = i;
	}
      }
    }
    num = 0;
    dfs(x, y);
    cout << num << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
 
using namespace std;
typedef long long ll;
 
#define rep(i, n) for(int i=0; i<(n); ++i)

char field[21][21];
char visit[21][21];
int dx[]= {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int W, H;
void dfs(int y, int x) {
    visit[y][x] = 1;
    rep(i, 4) {
        int ny = y+dy[i];
        int nx = x+dx[i];
        if(field[ny][nx] == '.' && visit[ny][nx] == 0 &&
               nx >= 0 && nx < W && ny >= 0 && ny < H) {
            dfs(ny, nx);
        }
    }
}
int main(void){
    while(cin >> W >> H && W) {
        int y, x;
        rep(i, H) rep(j, W){
            cin >> field[i][j];
            if(field[i][j] == '@') y = i, x = j;
        }
        memset(visit, 0, sizeof(visit));
        dfs(y, x);
        int ans = 0;
        rep(i, H) rep(j, W){
            ans += visit[i][j];
        }
        cout << ans << endl;
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
int w, h;
char ma[30][30];
char check[30][30];
int mx[4] = {-1, 0, 1, 0};
int my[4] = {0, -1, 0, 1};
int ans;

void Rec(int x, int y);

int main(void){
	while(1){
		ans = 0;

		cin >> w >> h;
		if(w == 0 && h == 0) break;
		for(int i=0; i<h; i++){
			scanf("%s", ma[i]);
		}

		int sx, sy;
		for(int i=0; i<h; i++){
			for(int j=0; j<w; j++){
				if(ma[i][j] == '@'){
					sx = j;
					sy = i;
				}
				check[i][j] = 0;
			}
		}
		
		check[sy][sx] = 1;
		ans++;
		Rec(sx, sy);

		cout << ans << endl;
	}

	return 0;
}

void Rec(int x, int y){
	for(int i=0; i<4; i++){
		int nx = x + mx[i];
		int ny = y + my[i];

		if(nx >= 0 && nx < w && ny >= 0 && ny < h && ma[ny][nx] == '.' && check[ny][nx] == 0){
			check[ny][nx] = 1;
			ans++;
			Rec(nx, ny);
		}
	}
}

C++: #include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>

using namespace std;

int W, H;
string m[30];
bool visit[30][30];
const int di[4] = {0,0,-1,1};
const int dj[4] = {1,-1,0,0};

int dfs(const int ci, const int cj) {
  int ret = 1;
  for (int i = 0; i < 4; ++i) {
    int ni = ci + di[i], nj = cj + dj[i];
    if (ni < 0 || nj < 0 || H <= ni || W <= nj) continue;
    if (m[ni][nj] == '#' || visit[ni][nj]) continue;
    visit[ni][nj] = true;
    ret += dfs(ni, nj);
  }
  return ret;
}

int solve() {
  memset(visit, 0, sizeof visit);
  int ret = 0;
  for (int i = 0; i < H; ++i)
    for (int j = 0; j < W; ++j)
      if (m[i][j] == '@')
        visit[i][j] = true , ret = dfs(i, j);
  return ret;
}

int main() {
  ios_base::sync_with_stdio(false);
  while (cin >> W >> H, W && H) {
    for (int i = 0; i < H; ++i)
      cin >> m[i];
    cout << solve() << endl;
  }
  return 0;
}

C++: #include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <fstream>
using namespace std;
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; }
template<class T> inline string toStr(T x) { ostringstream sout; sout << x; return sout.str(); }
typedef vector<int> vi;
typedef vector<vi>  vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n) FOR(i,0,(n)-1)
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = INT_MAX / 10;

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

int main() {
	int W, H;
	while (cin >> W >> H, W) {
		vs field(H);
		pii s;
		REP(i, H) {
			cin >> field[i];
			REP(j, W) {
				if (field[i][j] == '@') {
					s = pii(j, i);
				}
			}
		}

		int ans = 0;
		queue<pii> Q;
		Q.push(s);
		while (!Q.empty()) {
			pii p = Q.front();
			Q.pop();
			ans++;
			
			REP(d, 4) {
				int nx = p.first + dx[d], ny = p.second + dy[d];
				if (nx < 0 || W <= nx || ny < 0 || H <= ny) {
					continue;
				}
				if (field[ny][nx] == '.') {
					field[ny][nx] = '#';
					Q.push(pii(nx, ny));
				}
			}
		}

		cout << ans << endl;
	}
	return 0;
}