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 <bits/stdc++.h>
using namespace std;
int main(void){
    int w,h;
    while (cin>>w>>h) {
        if (w==0&&h==0) {
            break;
        } else {
            std::vector<vector<char>> vec(h,vector<char>(w));
            bool seen[h][w];
            char c;
            int now_H,now_W;
            for (int i=0;i<h;i++) {
                for (int j=0;j<w;j++) {
                    cin>>c;
                    vec[i][j]=c;
                    if (c=='@') {
                        now_H=i;
                        now_W=j;
                        seen[i][j]=true;
                    } else {
                        seen[i][j]=false;
                    }
                }
            }
            int count=1;
            std::deque<pair<int,int>> place;
            place.emplace_back(make_pair(now_H,now_W));
            while (place.size()!=0) {
                int tmp_H=place.front().first;
                int tmp_W=place.front().second;
                if (tmp_H+1<h&&seen[tmp_H+1][tmp_W]==false&&vec[tmp_H+1][tmp_W]!='#'&&vec[tmp_H+1][tmp_W]!='@') {
                    tmp_H++;
                    seen[tmp_H][tmp_W]=true;
                    place.emplace_back(make_pair(tmp_H,tmp_W));
                    count++;
                } else if (tmp_H-1>=0&&seen[tmp_H-1][tmp_W]==false&&vec[tmp_H-1][tmp_W]!='#'&&vec[tmp_H-1][tmp_W]!='@') {
                    tmp_H--;
                    seen[tmp_H][tmp_W]=true;
                    place.emplace_back(make_pair(tmp_H,tmp_W));
                    count++;
                } else if (tmp_W+1<w&&seen[tmp_H][tmp_W+1]==false&&vec[tmp_H][tmp_W+1]!='#'&&vec[tmp_H][tmp_W+1]!='@') {
                    tmp_W++;
                    seen[tmp_H][tmp_W]=true;
                    place.emplace_back(make_pair(tmp_H,tmp_W));
                    count++;
                } else if (tmp_W-1>=0&&seen[tmp_H][tmp_W-1]==false&&vec[tmp_H][tmp_W-1]!='#'&&vec[tmp_H][tmp_W-1]!='@') {
                    tmp_W--;
                    seen[tmp_H][tmp_W]=true;
                    place.emplace_back(make_pair(tmp_H,tmp_W));
                    count++;
                } else {
                    place.pop_front();
                }
            }
            cout<<count<<endl;
        }
    }
}

C++: #include<iostream>
#include<string.h>
#include<string>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#include<set>
#define ll long long int
#define ld long double
#define INF 1000000000
#define EPS 0.0000000001
#define rep(i,n) for(i=0;i<n;i++)
using namespace std;
typedef pair<int, int> pii;

int main()
{
  int i,j,k;
  int h,w;
  int dx[4]={0,1,0,-1};
  int dy[4]={1,0,-1,0};
  bool used[25][25];
  char c[25][25];

  while(1){

    cin>>w>>h;
    if(w==0 && h==0)break;

    int sum=1;
    rep(i,25)rep(j,25)
      used[i][j]=false;

    rep(i,h)
      scanf("%s",c[i]);

    queue<pii> q;
    rep(i,h)rep(j,w)
      if(c[i][j]=='@'){
	q.push(pii(i,j));//y,x
	used[i][j]=true;
	break;
      }

    while(q.size()){
      i=q.front().first;
      j=q.front().second;
      q.pop();
      rep(k,4){
	int y=i+dy[k];
	int x=j+dx[k];
	if(y>=0 && y<h && x>=0 && x<w && used[y][x]==false && c[y][x]=='.'){
	    q.push(pii(y,x));
	    used[y][x]=true;
	    sum++;
	  }
      }
    }

    cout<<sum<<endl;

  }
}

C++: #include <iostream>
#include <queue>
#include <utility>
#include <array>

using namespace std;

typedef pair<int, int> pii;

int height, width;
int sx, sy;
int ans = 0;
array<int, 4> dx = {0, 0, 1, -1};
array<int, 4> dy = {1, -1, 0, 0};


void bfs(vector<vector<char>>& field)
{
    queue<pii> p;
    p.push({sx, sy});
    
    while (!p.empty()) {
        pii q = p.front(); p.pop();
        
        //??????
        int cx = q.first;
        int cy = q.second;

        for (int i = 0; i < 4; ++i) {
            int nx = cx + dx.at(i);
            int ny = cy + dy.at(i);

            if (nx >= 0 && nx < width && ny >= 0 && ny < height 
                    && field.at(ny).at(nx) == '.') {

                ans++;
                p.push({nx, ny});
            }
        }
    }

    std::cout << ans << std::endl;
}


int main()
{
    while (cin >> height >> width) {
        if (height == 0 && width == 0) break;

        ans = 0;
        
        vector<vector<char>> field(height, vector<char>(width));

        for (int y = 0; y < height; ++y) {
            for (int x = 0; x < width; ++x) {
                cin >> field.at(y).at(x);

                if (field.at(y).at(x) == '@') {
                    sx = x; sy = y;
                }
            }
        }

        bfs(field);
    }

    return 0;
}