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 <cstring>

using namespace std;

int used[20][20];
char M[20][20];
int W, H;

bool in(int sx, int sy) {
  return 0<=sx && sx<W && 0<=sy && sy<H;
}

int dfs(int x, int y) {
  int res = 1;
  used[y][x] = 1;
  
  const int dx[]={-1,1,0,0};
  const int dy[]={0,0,-1,1};
  for(int i=0; i<4; i++) {
    int nx = x+dx[i], ny = y+dy[i];
    if(in(nx, ny) && M[ny][nx] == '.' && !used[ny][nx])
      res += dfs(nx, ny);
  }
  return res;
}

int main() {
  
  int stx, sty;
  while(cin >> W >> H, (W|H)) {
    memset(used, 0, sizeof(used));
    for(int i=0; i<H; i++)
      for(int j=0; j<W; j++) {
	cin >> M[i][j];
	if(M[i][j] == '@') {
	  stx = j, sty = i;
	  M[i][j] = '.';
	}
      }
    cout << dfs(stx, sty) << endl;
  }
  
  return 0;
}

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

using namespace std;

int dx[] = { -1, 0, 1, 0 };
int dy[] = { 0, -1, 0, 1 };

string tile[20];

int w, h;

int dfs(int x, int y){
	int ret = 1;
	if (tile[x][y] == '#') return 0;
	tile[x][y] = '#';

	for (int i = 0; i < 4; i++){
		if (x + dx[i] < 0 || x + dx[i] >= h) continue;
		if (y + dy[i] < 0 || y + dy[i] >= w) continue;
		ret += dfs(x + dx[i], y + dy[i]);
	}
	return ret;
}

int main(){
	while (cin >> w >> h,w){
		int x, y;
		for (int i = 0; i < h; i++){
			cin >> tile[i];
			for (int j = 0; j < w; j++){
				if (tile[i][j] == '@'){
					x = i;
					y = j;
				}
			}
		}
		cout << dfs(x, y) << endl;
	}
}

C++: /*
 * red_and_black.cpp
 *
 *  Created on: 2017/02/10
 *      Author: Nishio
 */

#include <bits/stdc++.h>
#define REP(i,n,N) for(int i=n;i<N;i++)
#define p(S) cout<<S<<endl
#define mp(a,b) make_pair(a,b)
using namespace std;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};

int main(){
	int H,W;

	while(cin>>W>>H&&W){
		queue<pair<int,int>> q;
		bool dp[21][21]={false};
		int nowx,nowy,nextx,nexty,count=1;
		char c[21][21];
		REP(i,0,H){
			REP(j,0,W){
				cin>>c[i][j];
				if(c[i][j]=='@'){
					dp[i][j]=true;

					q.push(mp(i,j));
				}
			}
		}
		while(!q.empty()){
			nowy=q.front().first;
			nowx=q.front().second;
			q.pop();
			REP(i,0,4){
				nextx=nowx+dx[i];
				nexty=nowy+dy[i];
				if(0<=nextx && nextx<W && 0<=nexty &&nexty<H){
					if(!dp[nexty][nextx]&&c[nexty][nextx]=='.'){
						dp[nexty][nextx]=true;
						q.push(mp(nexty,nextx));
						count++;
					}
				}
			}
		}
		p(count);
	}
	return 0;
}

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

int point(int x,int y,int w,int h){
  int s;
  s= y*w+x;
  return s;
}

int point_x(int x,int w){
  int s;
  s=x%w;
  return s;
}

int point_y(int x,int w){
  int s;
  s=x/w;
  return s;
}

int main(){
  int w,h;
  cin>>w>>h;
  while(w!=0){
    int a[21][21]={};
    int sx,sy;
    for(int j=0;j<h;j++){
      for(int i=0;i<w;i++){
	char s;
	cin>>s;
	if(s=='.'){
	  a[i][j]=1;
	}else if(s=='#'){
	  a[i][j]=-1;
	}else{
	  a[i][j]=2;
	  sx=i;
	  sy=j;
	}
      }
    }

    int d[21][21];
    for(int i=0;i<21;i++){
      for(int j=0;j<21;j++){
	d[i][j]=-1;
      }
    }
    queue<int>q;
    d[sx][sy]=0;
    int str;
    str=point(sx,sy,w,h);
    q.push(str);
    while(! q.empty()){
      int cur =q.front();
      q.pop();
      int x,y;
      x=point_x(cur,w);
      y=point_y(cur,w);
      if(x>=1){
	if(a[x-1][y]==1&&d[x-1][y]==-1){
	  d[x-1][y]=0;
	  q.push(point(x-1,y,w,h));
	}
      }
      if(x<=w-2){
	if(a[x+1][y]==1&&d[x+1][y]==-1){
	  d[x+1][y]=0;
	  q.push(point(x+1,y,w,h));
	}
      }
      if(y>=1){
	if(a[x][y-1]==1&&d[x][y-1]==-1){
	  d[x][y-1]=0;
	  q.push(point(x,y-1,w,h));
	}
      }
      if(y<=h-2){
	if(a[x][y+1]==1&&d[x][y+1]==-1){
	  d[x][y+1]=0;
	  q.push(point(x,y+1,w,h));
	}
      }
    }

    int t=0;
    for(int i=0;i<w;i++){
      for(int j=0;j<h;j++){
	if(d[i][j]==0){
	  t+=1;
	}
      }
    }
    cout <<t<< endl;
    cin>>w>>h;
  }
}

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

int W, H;
char tile[25][25];
int cnt;
int x, y;

void dfs(int y, int x){
	tile[y][x] = '#';
	cnt++;
	int dy[4] = { 0, 1, 0, -1 };
	int dx[4] = { 1, 0, -1, 0 };
	for (int i = 0; i < 4; i++){
		int Y = y + dy[i];
		int X = x + dx[i];
		if (0 <= Y&&Y < H && 0 <= X&&X < W&&tile[Y][X] != '#'){
			dfs(Y, X);
		}
	}
}

int main(){

	while (cin >> W >> H, W != 0 && H != 0){
		for (int i = 0; i < H; i++){
			for (int j = 0; j < W; j++){
				cin >> tile[i][j];
				if (tile[i][j] == '@'){
					y = i;
					x = j;
				}
			}
		}
		dfs(y, x);
		cout << cnt << endl;
		cnt = 0;
	}

	return 0;
}