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>
#ifndef LIB_DEBUG_HPP
#define LIB_DEBUG_HPP
#ifdef DEBUG
#include <cassert>
#define debug(a) a
#else
#define debug(a)
#endif
#endif
#ifndef repeat
#define repeat(i,n) for (int i = 0; i < (n); ++i)
#endif
#ifndef repeat_from
#define repeat_from(i,m,n) for (int i = (m); i < (n); ++i)
#endif
#ifndef repeat_one
#define repeat_one(i,n) for (int i = 1; i <= (n); ++i)
#endif
#ifndef repeat_rev
#define repeat_rev(i,n) for (int i = (n)-1; 0 <= i; --i)
#endif
#ifndef foreach
#define foreach(it, cont) for (typeof(cont.begin()) it = cont.begin(); it != cont.end(); ++it)
#endif
#ifndef LIB_TYPEDEF_LL_HPP
#define LIB_TYPEDEF_LL_HPP
typedef long long ll;
typedef unsigned long long ull;
#endif
#ifndef LIB_DEBUG_HPP
#define LIB_DEBUG_HPP
#ifdef DEBUG
#include <cassert>
#define debug(a) a
#else
#define debug(a)
#endif
#endif
using namespace std;
const int D4[4][2] = { {0,-1}, {0,1}, {-1,0}, {1,0} };
int H, W;
vector<vector<bool> > tile;
vector<vector<bool> > used;
int dfs(int h, int w) {
    if (used[h][w]) return 0;
    used[h][w] = true;
    int acc = 1;
    repeat (d,4) {
        int h0 = h + D4[d][0];
        int w0 = w + D4[d][1];
        if (0 <= h0 and h0 < H and 0 <= w0 and w0 < W and not used[h0][w0]) {
            if (tile[h0][w0] == false) {
                acc += dfs(h0, w0);
            }
        }
    }
    return acc;
}
int main() {
    ios_base::sync_with_stdio(false);
    while (true) {
        cin >> W >> H; cin.ignore();
        if (W == 0 and H == 0) break;
        tile = vector<vector<bool> >(H, vector<bool>(W));
        pair<int,int> p;
        repeat (h,H) {
            repeat (w,W) {
                char c; cin >> c;
                if (c == '.') {
                    tile[h][w] = false;
                } else if (c == '#') {
                    tile[h][w] = true;
                } else if (c == '@') {
                    tile[h][w] = false;
                    p = make_pair(h,w);
                } else {
                    debug (assert (false));
                }
            }
            cin.ignore();
        }
        used = vector<vector<bool> >(H, vector<bool>(W, false));
        cout << dfs(p.first, p.second) << endl;
    }
    return 0;
}

C++: #include "bits/stdc++.h"
#include<unordered_map>
#pragma warning(disable:4996)
using namespace std;

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

struct aa {
	int y;
	int x;
	bool black;
};

int main() {
	while (1) {
		int w, h; cin >> w >> h;
		if (!w)break;
		vector<vector<int>>field(h + 2, vector<int>(w + 2, 0));
		int sx, sy;
		for (int i = 0; i < h; ++i) {
			string st; cin >> st;
			for (int j = 0; j < w; ++j) {
				if (st[j] == '.') {
					field[i + 1][j + 1] = 1;
				}
				else {
					if (st[j] == '#') {
						field[i + 1][j + 1] = 2;
					}
					else {
						sx = j + 1;
						sy = i + 1;
						field[i + 1][j + 1] = 1;
					}
				}
			}
		}
		vector<aa>que;
		que.push_back(aa{ sy, sx, true });
		int ans = 1;
		vector<vector<bool>>used(h + 2, vector<bool>(w + 2, false));
		used[sy][sx] = true;

		while (!que.empty()) {
			aa atop(que.back());
			que.pop_back();
			for (int i = 0; i < 4; ++i) {
				const int ny = atop.y + dy[i];
				const int nx = atop.x + dx[i];
				if (atop.black) {
					if (field[ny][nx] == 1) {
						if (!used[ny][nx]) {
							used[ny][nx] = true;
							ans++;
							que.push_back(aa{ ny,nx,!atop.black });
						}
					}
				}
				else {
					if (field[ny][nx] == 1) {
						if (!used[ny][nx]) {
							used[ny][nx] = true;
							ans++;
							que.push_back(aa{ ny,nx,!atop.black });
						}
					}
				}

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