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 dfs(int h, int w, vector<vector<int>> &v, vector<vector<int>> &field) {
    v[h][w] = 1;
    int dh[] = {0, 1, 0, -1};
    int dw[] = {-1, 0, 1, 0};
    
    for (int i = 0; i < 4; i++) {
        int nh = h + dh[i];
        int nw = w + dw[i];
        if (field[nh][nw] == 1 && v[nh][nw] == 0) {
            dfs(nh, nw, v, field);
        }
    }
    return 0;
}

int solve(int H, int W) {
    vector<vector<int>> field(H+2, vector<int>(W+2, 0));
    
    int startH;
    int startW;
    for (int h = 1; h <= H; h++) {
        for (int w = 1; w <= W; w++) {
            char c; cin >> c;
            if (c == '.') {
                field[h][w] = 1;
            } else if (c == '@') {
                field[h][w] = 1;
                startH = h;
                startW = w;
            }
        }
    }
    
    int ret = 0;
    vector<vector<int>> v(H+2, vector<int>(W+2, 0));
    dfs(startH, startW, v, field);
    for (int h = 1; h <= H; h++) {
        for (int w = 1; w <= W; w++) {
            if (v[h][w] == 1) {
                ret++;
            }
        }
    }
    return ret;
}

int main(void){
    // Your code here!
    int W, H;
    vector<int> ans;
    while (1) {
        cin >> W >> H;
        if (!W && !H) break;
        ans.push_back(solve(H, W));
    }
    for (auto &i : ans) {
        cout << i << endl;
    }
}

C++: #include <iostream>
#include <queue>
#include <set>
using namespace std;

const int MAX_SIZE = 20;
typedef pair<int, int> P;

int calc(char f[][MAX_SIZE], queue<P> q, int W, int H) {
  set<P> s;
  while (q.size()) {
    P p = q.front();
    q.pop();
    s.insert(p);
    int di[] = {-1, 1, 0, 0};
    int dj[] = {0, 0, -1, 1};
    for (int k = 0; k < 4; ++k) {
      int i = p.first  + di[k];
      int j = p.second + dj[k];
      if (i >= 0 && j >= 0 && i < H && j < W && f[i][j] == '.') {
        pair<set<P>::iterator, bool> r = s.insert(P(i, j));
        if (r.second) q.push(*r.first);
      }
    }
  }
  return s.size();
}
int main() {
  int W, H;
  char f[MAX_SIZE + 1][MAX_SIZE + 1];
  while (true) {
    queue<P> q;
    cin >> W >> H;
    if (W == 0 && H == 0) break;
    for (int i = 0; i < H; ++i) {
      cin >> f[i];
      for (int j = 0; j < W; ++j) {
        if (f[i][j] == '@') {
          q.push(P(i, j));
        }
      }
    }
    cout << calc(f, q, W, H) << endl;
  }
}

C++: #include <iostream>
#include <string>

#define rep(i, n) for(int i = 0 ; i < n ; i++)
using namespace std;
void dfs(int x, int y);

int cnt = 0;
string data[20];
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
int w, h;

int main()
{
    while(true){
      cin >> w >> h;
      if(w == 0 && h == 0) break;
      
      int sx, sy;
      for(int i = 0 ; i < h ; i++){
	cin >> data[i];
	for(int j = 0 ; j < w ; j++){
	  if(data[i][j] == '@'){
	    sx = i;
	    sy = j;
	  }
	}
      }
      dfs(sx, sy);
      cout << cnt << '\n';
      cnt = 0;
  }
}

void dfs(int x, int y)
{
  data[x][y] = '#';
  cnt++;

  rep(i, 4){
    int nx = x + dx[i], ny = y + dy[i];
    if(0 <= nx && nx < w && 0 <= ny && ny < h && data[nx][ny] == '.'){
      dfs(nx, ny);
    }
      }
}

C++: #include<iostream>
#include<queue>
#include<utility>
using namespace std;
int main(){
while(true){
        int w,h;
        cin >>w >>h;
        if(w==0&&h==0)break;
        char map[w][h];
        bool is_reached[w][h];
        queue<pair<int,int> >recentpos;
        int ans =0;
        for(int j =0;j<h;++j){
                for(int i = 0;i<w;++i){
                        char temp;
                        cin >>temp;
                        map[i][j]=temp;
                        is_reached[i][j]=false;
                        if(temp=='@'){
                                recentpos.push(pair<int,int>(i,j));
                                is_reached[i][j]=true;
                                ans++;
                      }
                }
        }
        while(!recentpos.empty()){
                int old_x=recentpos.front().first;
                int old_y=recentpos.front().second;
                recentpos.pop();
                for(int dx =-1;dx<2;dx++){
                        for(int dy=-1;dy<2;dy++){
                                if(dx*dy!=0)continue;
                                int x=old_x+dx;
                                int y=old_y+dy;
                                if(x<0||x>=w||y<0||y>=h)continue;
                                if(is_reached[x][y]||map[x][y]=='#')continue;
                                recentpos.push(pair<int,int>(x,y));
                                is_reached[x][y]=true;
                                ans++;
                        }
                }
        }
        cout<<ans<<'\n';
        }
        return 0;
}