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;

typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pair<int, int> > vii;
#define rrep(i, m, n) for(int (i)=(m); (i)<(n);  (i)++)
#define erep(i, m, n) for(int (i)=(m); (i)<=(n); (i)++)
#define  rep(i, n)    for(int (i)=0; (i)<(n);  (i)++)
#define rrev(i, m, n) for(int (i)=(n)-1; (i)>=(m); (i)--)
#define erev(i, m, n) for(int (i)=(n); (i)>=(m); (i)--)
#define  rev(i, n)    for(int (i)=(n)-1; (i)>=0; (i)--)
#define vrep(i, c)    for(__typeof((c).begin())i=(c).begin(); i!=(c).end(); i++)
#define  ALL(v)       (v).begin(), (v).end()
#define mp            make_pair
#define pb            push_back
template<class T, class S> inline bool minup(T& m, S x){ return m>(T)x ? (m=(T)x, true) : false; }
template<class T, class S> inline bool maxup(T& m, S x){ return m<(T)x ? (m=(T)x, true) : false; }

const int    INF = 1000000000;
const ll     MOD = 1000000007LL;
const double EPS = 1E-12;

const int MAX_W = 50;
const int MAX_H = 20;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
int H, W;
bool ok[MAX_H][MAX_W];
char maze[MAX_H][MAX_W+1];
int sx, sy;

inline bool check(int hx, int hy){ return 0 <= hx && hx < H && 0 <= hy && hy < W; }

int main()
{
  while((cin >> W >> H) && W){
    rep(i, H) cin >> maze[i];

    rep(i, H) rep(j, W) ok[i][j] = false;
    rep(i, H) rep(j, W) if(maze[i][j] == '@') sx = i,  sy = j;

    stack<pii> st;
    st.push(mp(sx, sy));
    maze[sx][sy] = '.';
    ok[sx][sy] = true;
    while(!st.empty()){
      pii here = st.top();  st.pop();
      int hx = here.first;
      int hy = here.second;
      rep(k, 4){
        int nx = hx + dx[k];
        int ny = hy + dy[k];
        if(!check(nx, ny) || maze[nx][ny] == '#') continue;
        if(ok[nx][ny]) continue;
        ok[nx][ny] = true;
        st.push(mp(nx, ny));
      }
    }

    int res = 0;
    rep(i, H) rep(j, W) if(ok[i][j]) res += 1;
    cout << res << endl;
  }

  return 0;
}

C++: #include<stdio.h>

int dfs(int x,int y);
int w,h,i,j;
char maps[21][21];
int dx[4]={0,-1,0,1};
int dy[4]={-1,0,1,0};
int sum=0;

int main(){
  int sx,sy;
  while(1){
    sum=0;
    scanf("%d %d",&h,&w);
    if(h==0 && w==0) break;
    for(i=0;i<h;i++){
      for(j=0;j<w;j++){
	scanf("%c",&maps[i][j]);
	if(maps[i][j]=='@'){
	  sy=i;
	  sx=j;
	}
      }
    }
    dfs(sx,sy);
    printf("%d\n",sum);
  }
  return(0);
}

int dfs(int x,int y){
  int nx,ny;
  for(i=0;i<4;i++){
    nx=x+dx[i];
    ny=y+dy[i];
    maps[y][x]='#';
    if(0<=nx && nx<w && ny<h && maps[ny][nx]!='#'){
      dfs(nx,ny);
      sum++;
    }
  }
  return 0;
}

C++: //
//  main.cpp
//  AOJ-1130-2
//
//  Created by Kenta Kodera on 2017/06/18.
//  Copyright ?? 2017??´ Kenta Kodera. All rights reserved.
//

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <numeric>
#include <string>
#include <string.h>
#include <map>
#include <set>
#include <functional>
#include <complex>
#include <stack>
#include <tuple>
#include <array>
#include <ctype.h> // isdigit
#include <cassert>
using namespace std;

int W, H;

int dfs(vector<string> &room, int h, int w){
  
  int dh[4] = {1, 0, -1, 0};
  int dw[4] = {0, 1, 0, -1};
  room[h][w] = '#';
  
  int ans = 1;
  for(int i=0; i<4; i++){
    int nh = h + dh[i];
    int nw = w + dw[i];
    if(0<=nh && nh<H && 0<=nw && nw<W && room[nh][nw] == '.'){
      ans += dfs(room, nh, nw);
    }
  }
  return ans;
}

int main(){
  
  while(cin >> W >> H, W|H){
    
    vector<string> room(H);
    for(int i=0; i<H; i++){
      cin >> room[i];
    }
    
    for(int h=0; h<H; h++){
      for(int w=0; w<W; w++){
        if(room[h][w] == '@'){
          cout << dfs(room, h, w) << endl;
        }
      }
    }
  }
  return 0;
}