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>
using namespace std;

int main()
{
	int w, h;
	while (true) {
		do {
			cin >> w >> h;
		} while (w < 0 || h < 0);
		
		if (w == 0 && h == 0) {
			break;
		}
		unsigned next = 1U;
		char** room = new char*[h];
		unsigned** group = new unsigned*[h];
		int x, y;
		for (int i = 0; i < h; i++) {
			room[i] = new char[w];
			group[i] = new unsigned[w];
			for (int j = 0; j < w; j++) {
				cin >> room[i][j];
				if (room[i][j] == '@') {
					x = j;
					y = i;
				}
				group[i][j] = 0U;
			}
		}

		for (int k = 0; k < 2; k++) {
			for (int i = 0; i < h; i++) {
				for (int j = 0; j < w; j++) {
					if (room[i][j] != '#') {
						for (int a = i - 1; a <= i + 1; a++) {
							for (int b = j - 1; b <= j + 1; b++) {
								if (a < 0 || b < 0 || a >= h || b >= w) {
									continue;
								}
								if ((a == i && b != j) || (a != i && b == j)) {
									group[i][j] |= group[a][b];
								}
								if (group[i][j] == 0U) {
									group[i][j] |= next;
									next <<= 1;
								}
							}
						}
					}
				}
			}
			for (int i = h - 1; i >= 0; i--) {
				for (int j = w - 1; j >= 0; j--) {
					if (room[i][j] != '#') {
						for (int a = i - 1; a <= i + 1; a++) {
							for (int b = j - 1; b <= j + 1; b++) {
								if (a < 0 || b < 0 || a >= h || b >= w) {
									continue;
								}
								if ((a == i && b != j) || (a != i && b == j)) {
									group[i][j] |= group[a][b];
									if (group[i][j] == 0U) {
										group[i][j] |= next;
										next <<= 1;
									}
								}
							}
						}
					}
				}
			}
			for (int j = 0; j < w; j++) {
				for (int i = 0; i < h; i++) {
					if (room[i][j] != '#') {
						for (int a = i - 1; a <= i + 1; a++) {
							for (int b = j - 1; b <= j + 1; b++) {
								if (a < 0 || b < 0 || a >= h || b >= w) {
									continue;
								}
								if ((a == i && b != j) || (a != i && b == j)) {
									group[i][j] |= group[a][b];
									if (group[i][j] == 0U) {
										group[i][j] |= next;
										next <<= 1;
									}
								}
							}
						}
					}
				}
			}
			for (int j = w - 1; j >= 0; j--) {
				for (int i = h - 1; i >= 0; i--) {
					if (room[i][j] != '#') {
						for (int a = i - 1; a <= i + 1; a++) {
							for (int b = j - 1; b <= j + 1; b++) {
								if (a < 0 || b < 0 || a >= h || b >= w) {
									continue;
								}
								if ((a == i && b != j) || (a != i && b == j)) {
									group[i][j] |= group[a][b];
									if (group[i][j] == 0U) {
										group[i][j] |= next;
										next <<= 1;
									}
								}
							}
						}
					}
				}
			}
		}
		
		int count = 0;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if ((group[i][j] & group[y][x]) != 0U) {
					count++;
				}
			}
		}

		cout << count << '\n';

		for (int i = 0; i < h; i++) {
			delete[] room[i];
			delete[] group[i];
		}
		delete[] room;
		delete[] group;
	}

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T> inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return 1;
    }
    return 0;
}
typedef long long int ll;

#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define endl "\n"
const double EPS = 1e-7;
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
const double PI = acos(-1);
const int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

//-------------------------------------

using P = pair<int, int>;

int h, w;

void dfs(int x, int y, vector<string> &g, vector<vector<bool>> &seen) {
    if(seen[y][x] || g[y][x] == '#') {
        return;
    }
    seen[y][x] = true;
    for(int i = 0; i < 4; i++) {
        int ny = y + dy[i];
        int nx = x + dx[i];
        if(0 <= nx && nx < w && 0 <= ny && ny < h && g[ny][nx] == '.' &&
           !seen[ny][nx]) {
            dfs(nx, ny, g, seen);
        }
    }
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(1) {
        cin >> w >> h;
        if(h == 0 && w == 0) {
            break;
        }
        vector<string> g(h);
        for(int i = 0; i < h; i++) {
            cin >> g[i];
        }
        vector<vector<bool>> seen(h, vector<bool>(w, false));
        int sx, sy;
        for(int i = 0; i < h; i++) {
            for(int j = 0; j < w; j++) {
                if(g[i][j] == '@') {
                    sy = i, sx = j;
                }
            }
        }
        dfs(sx, sy, g, seen);
        int ans = 0;
        for(int i = 0; i < h; i++) {
            ans += count(ALL(seen[i]), true);
        }
        cout << ans << endl;
    }
}