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>

typedef long long LL;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define DECIM8  fixed<<setprecision(8) 
#define SZ(a) int((a).size())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);

//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))

using namespace std;
int field[32][32];
int dfs(int x,int y){
  int dx[4]={-1,1,0,0},dy[4]={0,0,-1,1};
  if(field[x][y]==0) return 0;
  field[x][y]=0;
  int answer=1;
  REP(i,4) answer += dfs(x+dx[i],y+dy[i]);
  return answer;
}
int main(void)
{
  for(;;){
    int w,h;
    cin >> w >> h;
    if(!w&&!h) return 0;
    REP(i,32) REP(j,32) field[i][j]=0;
    vector<string> fi;
    fi.resize(h);
    REP(i,h) cin >> fi[i];
    REP(i,h) REP(j,w) if(fi[i][j]!='#') field[i+5][j+5]=1;
    REP(i,h) REP(j,w) if(fi[i][j]=='@')
      cout << dfs(i+5,j+5) << endl;
  }
}

C++: #include <iostream>
using namespace std;
char m[21][21];
int w,h,ans;
int dx[]={0,1,0,-1},dy[]={1,0,-1,0};

void solve(int x,int y) {
  m[y][x]='#';
  ans++;
  
  for(int i=0;i<4;i++) {
    int nx=x+dx[i],ny=y+dy[i];
    if(nx>=0 && nx<w && ny>=0 && ny<h && m[ny][nx]!='#') {
	solve(nx,ny);
    }
  }
}

int main() {
  int sx,sy;
  while(cin>>w>>h,w||h) {
    ans=0;
    for(int y=0;y<h;y++) {
      for(int x=0;x<w;x++) {
	cin>>m[y][x];
	if(m[y][x]=='@') {
	  sx=x;sy=y;
	}
      }
    }
    solve(sx,sy);
    cout<<ans<<endl;
  }

  return 0;
}

C++: //C++14 (Clang 3.8.0)

#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string>
#define rp(i,a,b) for(int (i)=(int)(a);i<(int)(b);++i)

typedef long long ll;

int mx[]={-1,1,0,0};
int my[]={0,0,-1,1};
using namespace std;
int n=0;
void dfs(int startx,int starty,vector<vector<bool> > &map){
    for(int i=0;i<4;i++){
        int x=startx+mx[i],y=starty+my[i];
        if(x>=0 and y>=0 and y<map.size() and x<map[0].size() and map[y][x]==true){
        map[y][x]=false;
        n++;
        dfs(x,y,map);}
        
    }
    
}

int main(){
    while(1){
        int x,y,startx,starty;
        vector<vector<bool> > map;
        cin>>x>>y;

        if(x==0 and y==0) return 0;
        for(int i=0;i<y;i++){
            vector<bool> tmp;
            string s;
            cin>>s;
            for(int j=0;j<x;j++){

                if(s[j]=='@') {startx=j;starty=i;tmp.push_back(true);}
                else if(s[j]=='.') tmp.push_back(true);
                else tmp.push_back(false);    
            }
            map.push_back((tmp));
        }
        n=0;
        dfs(startx,starty,map);
        cout<<n<<endl;
        
    }

    return 0;
};

C++: #include <bits/stdc++.h>

using namespace std;

int W, H;
int start_x = 0, start_y = 0;
int sum;

vector<vector<char>> makeroom(int w, int h) {
	vector<vector<char>> room(h);
	for(int i = 0; i < h; i++) {
		for(int j = 0; j < w; j++) {
			char tmp;
			cin >> tmp;
			if(tmp == '@') {
				start_x = j;
				start_y = i;
			}
			room[i].push_back(tmp);
		}
	}
	return room;
}

void dfs_run(vector<vector<char>>& room, vector<vector<bool>>& checker, int x, int y) {
	
	if(x != 0) {
		if(room[y][x - 1] == '.' and checker[y][x - 1]) {
			sum++;
			checker[y][x - 1] = false;
			dfs_run(room, checker, x - 1, y);
		}
	}
	if(x != W - 1) {
		if(room[y][x + 1] == '.' and checker[y][x + 1]) {
			sum++;
			checker[y][x + 1] = false;
			dfs_run(room, checker, x + 1, y);
		}
	}
	if(y != 0) {
		if(room[y - 1][x] == '.' and checker[y - 1][x]) {
			sum++;
			checker[y - 1][x] = false;
			dfs_run(room, checker, x, y - 1);
		}
	}
	if(y != H - 1) {
		if(room[y + 1][x] == '.' and checker[y + 1][x]) {
			sum++;
			checker[y + 1][x] = false;
			dfs_run(room, checker, x, y + 1);
		}
	}
}

void dfs(vector<vector<char>>& room) {
	vector<vector<bool>> checker;
	checker = vector<vector<bool>>(room.size(), vector<bool>(room[0].size(), true));

	dfs_run(room, checker, start_x, start_y);
}


int main(void) {
	while(1) {
		cin >> W >> H;
		if(W == 0 and H ==0) {
			break;
		}

		vector<vector<char>> room = makeroom(W, H);
		sum = 0;
		dfs(room);

		cout << sum + 1 << endl;
	}
	return 0;
}