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>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

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

int main(){
	for(int w,h;scanf("%d%d",&w,&h),w;){
		char B[20][21];
		rep(i,h) scanf("%s",B[i]);

		int ans=1;
		rep(_,h*w){
			rep(i,h) rep(j,w) if(B[i][j]=='@') {
				rep(k,4){
					int y=i+dy[k],x=j+dx[k];
					if(0<=y && y<h && 0<=x && x<w && B[y][x]=='.') B[y][x]='@', ans++;
				}
			}
		}
		printf("%d\n",ans);
	}

	return 0;
}

C++: #define _USE_MATH_DEFINES
#include "bits/stdc++.h"
#define EPS 1e-10
using namespace std;
typedef long long llong;
vector<vector<char> >v;
int w, h;
int ans = 0;
void dfs(int x, int y) {
	if (x < 0 || h <= x)return;
	if (y < 0 || w <= y)return;

	if (v[x][y] == '#')return;

	if (v[x][y] == '.'||v[x][y]=='@') {
		ans++;
		v[x][y] = '#';
		dfs(x + 1, y);
		dfs(x - 1, y);
		dfs(x, y - 1);
		dfs(x, y + 1);
	}
	return;
}
int main() {
	while (cin >> w >> h, h) {
		ans = 0;
		int x, y;
		v = vector<vector<char> >(h, vector<char>(w));
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				cin >> v[i][j];
				if (v[i][j] == '@') {
					x = i;
					y = j;
				}
			}
		}
		dfs(x, y);
		cout << ans << endl;
	}
	return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include "bits/stdc++.h"
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define FOR(i, m, n) for(int i=(m); i<(n); ++i)
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mp make_pair
#define pb push_back
#define Cout(x) cout << (x) << endl
#define Cout2(x, y) cout << (x) << " " << (y) << endl
#define dump(x) cerr << #x << " = " << (x) << endl;
typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<string> VS;
typedef vector<bool> VB;
typedef vector<vector<int>> VVI;
typedef pair<int, int> PII;
const int inf = 1e9;
const double pi = acos(-1.0);

int w, h;
vector<VB> a(20, VB(20));
vector<VB> v(20, VB(20));
int dx[] = { 1,0,-1,0 }, dy[] = { 0,1,0,-1 };

void Dfs(int x, int y) {
	if (x < 0 || y < 0 || x >= w || y >= h || !a[y][x] || v[y][x])return;
	v[y][x] = true;
	rep(i, 4)Dfs(x + dx[i], y + dy[i]);
}

int main() {
	while (cin >> w >> h && (w || h)) {
		VS s(h); rep(i, h)cin >> s[i];
		int sx, sy;
		rep(i, h) {
			rep(j, w) {
				if (s[i][j] == '.')a[i][j] = true;
				if (s[i][j] == '@') { a[i][j] = true; sx = j; sy = i; }
				if (s[i][j] == '#')a[i][j] = false;
			}
		}
		rep(i, 20)rep(j, 20)v[i][j] = false;

		Dfs(sx, sy);

		int ans = 0;
		rep(i, h) {
			rep(j, w)ans += v[i][j];
		}
		Cout(ans);
	}
}

C++: #include<iostream>
#include<queue>
#include<utility>
using namespace std;
int main(){
    int W,H,x0,y0;
    char t[20][20];
    bool visit[20][20];
    const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};
    typedef pair<int,int>P;
    queue<P>Q;
    while(cin>>W>>H){
        if(W==0||H==0)break;
        for(int i=0;i<H;++i){
            for(int j=0;j<W;++j){
                cin>>t[i][j];
                if(t[i][j]=='@'){
                    x0=j;
                    y0=i;
                }
                visit[i][j]=true;
            }
        }
        int sum=0;
        Q.push(P(x0,y0));
        visit[y0][x0]=false;
        while(!Q.empty()){
            int srcx=Q.front().first;
            int srcy=Q.front().second;
            Q.pop();
            sum++;
            for(int i=0;i<4;++i){
                int curx=srcx+dx[i];
                int cury=srcy+dy[i];
                if(t[cury][curx]=='#') continue;
                if(curx<0||curx>=W||cury<0||cury>=H) continue;
                if(!visit[cury][curx]) continue;
                visit[cury][curx]=false;
                Q.push(P(curx,cury));
            }
            
        }
        cout<<sum<<endl;
    }
}

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

int main()
{
	int W, H;
	int dx[] = {0,1,0,-1};
	int dy[] = {1,0,-1,0};
	while (cin >> W >> H && W != 0 && H != 0)
	{
		bool path[20 + 1][20 + 1];
		for (int i = 1; i <= H; i++)
		{
			for (int j = 1; j <= W; j++)
			{
				path[i][j] = false;
			}
		}
		char field[20 + 1][20 + 1];
		int startx, starty;
		for (int i = 1; i <= H; i++)
		{
			char buf[21];
			cin >> buf;
			string str = buf;
			for (int j = 0; j < W; j++)
			{
				field[i][j + 1] = str.at(j);
				if (str.at(j) == '@')
				{
					startx = j + 1;
					starty = i;
				}
			}
		}
		int tiles = 0;
		queue<pair<int,int>> q;
		q.push(make_pair(startx, starty));
		path[starty][startx] = true;
		while(q.size() > 0)
		{
			pair<int,int> p = q.front();
			q.pop();
			int x = p.first;
			int y = p.second;
			tiles++;
			for (int i = 0; i < 4; i++)
			{
				int nx = x + dx[i];
				int ny = y + dy[i];
				if (nx > W || ny > H || nx < 1 || ny < 1)continue;
				if (path[ny][nx])continue;
				if (field[ny][nx] == '#')continue;
				path[ny][nx] = true;
				q.push(make_pair(nx, ny));
			}
		}
		cout << tiles << endl;

	}
	return 0;
}