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>
#include<vector>
#include<algorithm>
#include<string>
#include<queue>
#include<utility>
int dx[4] = {0,1,0,-1};
int dy[4] = {1,0,-1,0};

using namespace std;

int main(){
  int w,h;
  vector<string> s;
  queue<pair<int,int>> que;
  vector<vector<int>> d;
  int defi,defj;
  while(cin>>w&&cin>>h){
    if(!w&&!h)break;
    s.resize(h);
    d.resize(h);
    for(int i=0;i<h;i++)
      d[i].resize(w,0);
    for(int i=0;i<h;i++){
      cin >> s[i];
      for(int j=0;j<w;j++)
	if(s[i][j] == '@'){defi=i;defj=j;}
    }
    
    que.push(make_pair(defi,defj));
    int ct=1;//???????????????
    while(que.size()){
      pair<int,int> defp = que.front();
      que.pop();
      pair<int,int> p;
      for(int i=0;i<4;i++){
	p.first = defp.first + dy[i];p.second = defp.second + dx[i];
	if(p.first>=0 && p.second>=0 && p.first<h && p.second<w){
	  if(d[p.first][p.second])continue;
	  else if(s[p.first][p.second] == '.'){
	    ct++;
	    d[p.first][p.second]++;
	    que.push(p);
	  }else if(s[p.first][p.second] == '#'){
	    d[p.first][p.second]++;
	  }
	}
      }
    }
    cout << ct << endl;
    s.clear();
    d.clear();
  }
  return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
int dy[]={0,0,-1,1},dx[]={-1,1,0,0};
class data{
public:
    int y,x;
    data(int _y,int _x){
        y=_y;x=_x;
    }
    data(){}
};
int main(){
    int w,h;
    while(cin>>w>>h,h||w){
        char field[20][21];
        bool f[20][20]={{0}};
        for(int i=0;i<h;i++)cin>>field[i];
        int sy,sx;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(field[i][j]=='@'){
                    sy=i;
                    sx=j;
                }
            }
        }
        queue<data>Q;
        Q.push(data(sy,sx));
        int cnt=0;
        while(Q.size()){
            data a=Q.front();Q.pop();
            int y=a.y,x=a.x;
            if(y<0||y>=h||x<0||x>=w)continue;
            if(f[y][x])continue;
            if(field[y][x]=='#')continue;
            f[y][x]=true;
            cnt++;
            for(int i=0;i<4;i++){
                Q.push(data(y+dy[i],x+dx[i]));
            }
        }
        cout<<cnt<<endl;
    }
}

C++: #include<iostream>
using namespace std;
int w,h;
char tail[21][21];
int a[]={-1,0,1,0};
int b[]={0,1,0,-1};
int cnt=0;
void func(int x,int y);
main(){
  int sx,sy;
  while(1){
    cin>> w>> h;
      if(w==0&&h==0) break;
    cnt=0;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	cin>> tail[i][j];
	if(tail[i][j]=='@'){
	  sx=j;
	  sy=i;
	}
      }
    }
    func(sx,sy);
    cout<< cnt<< endl;
  }
}

void func(int x,int y){
  tail[y][x]='#';
  cnt++;
  for(int i=0;i<4;i++){
    int nx=x+a[i];
    int ny=y+b[i];
    if(nx>=0 && nx<w && ny>=0 && ny<h && tail[ny][nx]!='#'){
      func(nx,ny);
    }
  }
}

C++: #include <iostream>
#include <stdio.h>
using namespace std;

int main() {
	int w,h;// =<20
	char map[20][20];
	int res[20][20]={};
	int result=0;
	int x,y;
	int start;// xxyy
	int stage;
	int check;
	int finish=0;
	
	scanf("%d%d",&w,&h);
	while(finish==0){
	for(y=0;y<h;y++){
		for(x=0;x<w;x++){
			cin >> map[x][y];
			if(map[x][y]=='@'){start=x*100+y;}
		}
	}
	
	x=start/100;
	y=start%100;
	res[x][y]=1;
	stage=1;
	check=1;
	while(check==1){
		check=0;
		for(y=0;y<h;y++){
			for(x=0;x<w;x++){
				if(res[x][y]==stage){
					if(x>0){
						if((map[x-1][y]=='.')&&(res[x-1][y]==0)){res[x-1][y]=stage+1;check=1;}
					}
					if(y>0){
						if((map[x][y-1]=='.')&&(res[x][y-1]==0)){res[x][y-1]=stage+1;check=1;}
					}
					if(x<w-1){
						if((map[x+1][y]=='.')&&(res[x+1][y]==0)){res[x+1][y]=stage+1;check=1;}
					}
					if(y<h-1){
						if((map[x][y+1]=='.')&&(res[x][y+1]==0)){res[x][y+1]=stage+1;check=1;}
					}
				}
			}
		}
		stage+=1;
	}

	result=0;
	for(y=0;y<h;y++){
		for(x=0;x<w;x++){
			if(res[x][y]!=0){result+=1;}
		}
	}
	cout << result << endl;

	for(y=0;y<h;y++){
		for(x=0;x<w;x++){
			map[x][y]=0;
			res[x][y]=0;
		}
	}

	scanf("%d%d",&w,&h);
	if((w==0)&&(h==0)){
		finish=1;
	}
	}
	return 0;
}