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 <vector>
#include <utility>
#include <algorithm>

int solve(int width, int height) {
    std::vector<bool> available((width + 2) * (height + 2));
    std::vector<std::pair<int, int>> fringe;
    auto access = [&](int x, int y){ return available[y * (width + 2) + x]; };

    int current_x = 0, current_y = 0;

    for (int y = 1; y <= height; y++) {
        for (int x = 1; x <= width; x++) {
            char c;
            std::cin >> c;
            access(x,y) = c == '.';
            switch (c){
                case '.':
                    access(x, y) = true;
                    break;
                case '@':
                    access(x, y) = true;
                    current_x = x;
                    current_y = y;
                    break;
            }
        }
    }

    fringe.push_back(std::make_pair(current_x, current_y));

    int num = 0;
    while (!fringe.empty()) {
        auto top = fringe.back();
        fringe.pop_back();
        auto x = top.first, y = top.second;
        if (access(x, y)) {
            num++;
            access(x, y) = false;
            fringe.push_back(std::make_pair(x + 1, y));
            fringe.push_back(std::make_pair(x - 1, y));
            fringe.push_back(std::make_pair(x, y + 1));
            fringe.push_back(std::make_pair(x, y - 1));
        }
    }

    return num;
}

int main() {
    int w, h;
    while (std::cin >> w >> h) {
        if (w == 0 && h == 0) {
            break;
        }
        std::cout << solve(w, h) << std::endl;
    }
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <time.h>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <iterator>
#include <sstream>
#include <string>
#include <bitset>
using namespace std;
#define FOR(I,F,N) for(int I = F; I < (int)(N); I++)
#define rep(i, n) FOR(i, 0, n)
#define FIN(V) cout<<V<<endl
#define pb push_back
#define INF (1 << 28)

template<typename T>
void remove(vector<T>& vector, unsigned int index){
vector.erase(vector.begin() + index);
}

typedef pair<int, int> P;
typedef long long ll;
typedef priority_queue<int> pq;
int StrToInt(string);
string IntToStr(int);
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

int w, h;
int state[21][21];
bool memo[21][21] = {0};
int ans;
void dfs(int x, int y){
	if(!memo[x][y]){
		memo[x][y] = true;
		ans++;
	}else return;
	int res = 0;
	rep(k,4){
		int nx = x + dx[k];
		int ny = y + dy[k];
		if(nx < 0 || ny < 0 || nx >= w || ny >= h)continue;
		if(state[nx][ny])continue;
		dfs(nx, ny);
	}
}
int main(void){
	while(cin >> w >> h, w|h){
		ans = 0;
		fill_n(*memo, 21*21, false);
		int sx, sy;
		rep(i,h){
			rep(j,w){
				char c;
				cin >> c;
				if(c == '@'){
					sx = j;
					sy = i;
					state[j][i] = 2;
				}else if(c == '.'){
					state[j][i] = 0;
				}else{
					state[j][i] = 1;
				}
			}
		}
		dfs(sx, sy);
		cout << ans << endl;
	}
	return 0;
}



int StrToInt(string s){
	stringstream ss;
	ss << s;
	int val;
	ss >> val;
	return val;
}
string IntToStr(int i){
	stringstream ss;
	ss << i;
	return ss.str();
}

C++: #include <bits/stdc++.h>
using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(int)(b);i++)
#define rep(i,n) FOR(i,0,n)
#define ZERO(a) memset(a,0,sizeof(a))

int w,h,ans;
char f[22][22];
bool visited[22][22];
int dx[] = {1,0,-1,0}, dy[] = {0,1,0,-1};

bool ng(int x, int y) {
    return (x<0 or x>=w or y<0 or y>=h);
}

void dfs(int x, int y) {
    if(x>=w or y>=h) return ;
    visited[y][x] = true;
    ans++;
    rep(i,4) {
        int nx = x+dx[i], ny = y+dy[i];
        if(!ng(nx,ny) and !visited[ny][nx] and f[ny][nx]!='#')
            dfs(nx,ny);
    }

    return ;
}

int main() {
    while(cin>>w>>h && w) {
        int sx=0, sy=0;
        ans = 0;
        ZERO(visited);
        rep(y,h) rep(x,w) f[y][x] = '$';
        rep(y,h) rep(x,w) {
            cin>>f[y][x];
            if(f[y][x] == '@') {sx=x; sy=y;}
        }

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

    return 0;
}

C++: 1>------ テ」ツδ禿」ツδォテ」ツδ嘉ゥツ鳴凝・ツァツ? テ」ツδ療」ツδュテ」ツつクテ」ツつァテ」ツつッテ」ツδ?4_3, テヲツァツ凝ヲツ按?Debug Win32 ------
1>  Source3.cpp
1>  4_3.vcxproj -> C:\Users\hokiken\Documents\programming\contest5\Debug\4_3.exe
========== テ」ツδ禿」ツδォテ」ツδ? 1 テヲツュツ」テ・ツクツクテァツオツづ、ツコツ?」ツ?? テ・ツ、ツアテヲツ閉療」ツ?? テヲツ崢エテヲツ鳴ーテ、ツクツ催ィツヲツ?」ツ?? テ」ツつケテ」ツつュテ」ツδε」ツδ?==========