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 <stdio.h>
int x,y,p,q,i,w,h,r=1;
int j,k[101][101];
char s[101][101];
int a[]={1,-1,0,0}, b[]={0,0,1,-1};
void func(int x, int y){
    for(i=0;i<4;i++){
        if (x+a[i]<=h&&x+a[i]>0&&y+b[i]<=w&&y+b[i]>0){
            if(k[x+a[i]][y+b[i]]==1);
            else if(s[x+a[i]][y+b[i]]=='.'){
                r++;
                k[x+a[i]][y+b[i]]=1;
                func(x+a[i],y+b[i]);
            }
            
        }
    }
}

int main(){
        while(1){
        scanf("%d %d",&w,&h);
        if (w==0&&h==0) break;
        for (i=1;i<=h;i++){
            for (j=1;j<=w;j++){
                scanf("%c",&s[i][j]);
                if (s[i][j]=='@') k[i][j]=1;
                else k[i][j]=0;
                if (s[i][j]=='@') {
                    p=i; q=j;
                }
            }
        }
        func(p,q);
        printf("%d\n",r);
        
    }
    return 0;
}

C++: #include <bits/stdc++.h>

#define ll long long

using namespace std;


int W,H;

char r[1000][1000];

int sh,sw;
int bnum=0;

int c[1000][1000] = {};




int solve(int i,int j){
    int ans=0;

    if(i <0 || H <= i || j < 0 || W <= j)
        return 0;
    if(r[i][j] == '#'){
        return 0;
    }

    if(i-1>=0){
        ans += solve(i-1,j);
    }
    
    if(i+1 <H){
        ans += solve(i+1,j);
    }
    if(j-1>=0){
        ans += solve(i,j-1);
    }
    if(j+1 < W){
        ans += solve(i,j+1);
    }
    return ans;
}


int main(){

    while(1){
        scanf("%d %d",&W,&H);
        if(W == 0 && H == 0) break;

        for(int i=0;i<H;i++){
            for(int j=0;j<W;j++){
                cin >> r[i][j];
                if(r[i][j] == '@'){
                    sh = i;
                    sw = j;
                    r[i][j] = '.';
                }
            }
        }
        cout << solve(sh,sw) << endl;
    }

}

C++: #include "bits/stdc++.h"
using namespace std;
#define mod 1000000007
#define all(c) begin(c),end(c)
template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
using ll = long long;
using vi = vector<int>; using vs = vector<string>;
int dd[] = { 0, 1, 0, -1, 0 }; //→↓←↑

bool B[25][25]; int N, M;
void dfs(int ty, int tx, vs S)
{
	for (int r = 0; r < 4; r++)
	{
		int y = ty + dd[r];
		int x = tx + dd[r + 1];
		if (y < 0 || y >= N || x < 0 || x >= M)
		{
			continue;
		}
		if (S[y][x] == '.' && B[y][x] == false)
		{
			B[y][x] = true;
			dfs(y, x, S);
		}
	}
}

void solve()
{
	while (true)
	{
		cin >> M >> N;
		if (N == 0 && M == 0)
		{
			break;
		}

		vs S(N);
		for (int i = 0; i < N; i++)
		{
			cin >> S[i];
		}
		int sy = -1, sx = -1;
		for (int i = 0; i < N; i++)
		{
			for (int j = 0; j < M; j++)
			{
				if (S[i][j] == '@')
				{
					sy = i;
					sx = j;
					break;
				}
			}
		}
		for (int i = 0; i < N; i++)
		{
			for (int j = 0; j < M; j++)
			{
				B[i][j] = false;
			}
		}

		dfs(sy, sx, S);
		int ans = 0;
		for (int i = 0; i < N; i++)
		{
			for (int j = 0; j < M; j++)
			{
				if (B[i][j])
				{
					ans++;
				}
			}
		}
		cout << (ans+1) << endl;
	}

}

int main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	solve();
	return 0;
}