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<queue>
using namespace std;
int W,H;
int m,n;
int counts;
struct test
{
    char c;
    int flag;
    int x;
    int y;
};
queue<test>Q;
int main()
{
    cin>>W>>H;
    while(W!=0 && H!=0)
    {
        counts=0;
        test A[H][W];
        for(int i=0;i<H;i++)
            for(int j=0;j<W;j++)
            {
                cin>>A[i][j].c;
                A[i][j].flag=0;
                A[i][j].x=i;
                A[i][j].y=j;
                if(A[i][j].c=='@')
                {
                    m=i;
                    n=j;
                }
            }
        Q.push(A[m][n]);
        counts++;
        A[m][n].flag=1;
        while(!Q.empty())
        {
            test p=Q.front();
            if(p.x-1>=0 && p.y>=0 && p.x-1<H && p.y<W && A[p.x-1][p.y].c=='.' && A[p.x-1][p.y].flag==0)
            {
                Q.push(A[p.x-1][p.y]);
                A[p.x-1][p.y].flag=1;
                counts++;
            }
            if(p.x>=0 && p.y-1>=0 && p.x<H && p.y-1<W && A[p.x][p.y-1].c=='.' && A[p.x][p.y-1].flag==0)
            {
                Q.push(A[p.x][p.y-1]);
                A[p.x][p.y-1].flag=1;
                counts++;
            }
            if(p.x+1>=0 && p.y>=0 && p.x+1<H && p.y<W && A[p.x+1][p.y].c=='.' && A[p.x+1][p.y].flag==0)
            {
                Q.push(A[p.x+1][p.y]);
                A[p.x+1][p.y].flag=1;
                counts++;
            }
            if(p.x>=0 && p.y+1>=0 && p.x<H && p.y+1<W && A[p.x][p.y+1].c=='.' && A[p.x][p.y+1].flag==0)
            {
                Q.push(A[p.x][p.y+1]);
                A[p.x][p.y+1].flag=1;
                counts++;
            }
            Q.pop();
        }
        cout<<counts<<endl;
        cin>>W>>H;
    }
    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
char cm[200][200];
int im[200][200];
 int W,H;
bool visited[200][200];
int sy,sx;
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};
typedef pair<int,int> P;

int bfs();

int main(){
 

  while(1)
    {
      cin >> W >> H;
      if(W == 0 && H == 0)break;
      memset(im,0,sizeof(im));
      memset(visited,false,sizeof(visited));
      for(int i=0;i<H;i++)
	{
	  for(int j=0;j<W;j++)
	    {
	      cin >> cm[i][j];
	      if(cm[i][j] == '#'){
		im[i][j] = 8;
	      }
	      else if(cm[i][j] == '@')
		{
		  sy = i;
		  sx = j;
		}
	    }
	}

      cout << bfs() << endl;
      /*     
 for(int i=0;i<H;i++){
	for(int j=0;j<W;j++)cout << im[i][j];
	cout << endl;
      }
      */

    }
}

int bfs()
{
  int maxx = 0;
  queue<P> que;
  que.push(P(sx,sy)); 
  im[sy][sx] = 1;
  int cou=1;
  visited[sy][sx] = true;

  while(que.size())
    {
      P p = que.front();
      que.pop();

      for(int i=0;i<4;i++)
	{
	  int nx = p.first + dx[i],ny = p.second + dy[i];

	  if(0 <= nx && nx < W && 0 <= ny && ny < H && cm[ny][nx] != '#' && !visited[ny][nx])
	    {
	      visited[ny][nx] = true;
	      cou++;
	      im[ny][nx]=cou;
	      maxx = max(maxx,im[ny][nx]);
	      que.push(P(nx,ny));
	    }
	}
      
    }
return maxx;
}

C++: #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define MOD 1000000007
#define rep(i,n) for(i=0;i<n;i++)
#define loop(i,a,n) for(i=a;i<n;i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

typedef vector<int> vi;
typedef pair<int,int> pii;

int main(void) {
  int i,j;
  int h,w;
  while(1){
    cin>>w>>h;
    if(w+h==0)break;
    cin.ignore();
    vector<string> s(h);
    rep(i,h)cin>>s[i];
    queue<pii> q;
    vector<vi> used(h,vi(w,0));
    rep(i,h)rep(j,w)if(s[i][j]=='@'){
      q.push(pii(i,j));
      used[i][j]++;
    }
    int d[5]={0,1,0,-1,0};
    while(q.size()){
      rep(i,4){
	int x=q.front().first+d[i];
	int y=q.front().second+d[i+1];
	if(x>=0 && x<h && y>=0 && y<w && 
	   s[x][y]=='.' && used[x][y]==0){
	     used[x][y]++;
	     q.push(pii(x,y));
	}
      }
      q.pop();
    }
    int ans=0;
    rep(i,h)rep(j,w)if(used[i][j])ans++;
    cout<<ans<<endl;
  }
}