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;
struct PAD{
    char M;
    int x;
    int y;
    int flag = 0; 
}; 
int main(){
    queue<struct PAD> que;
    struct PAD pad[20][20+1];
    int W,H,count;
    int start_x,start_y;
    char baf_char;
    int dx[4] = {1,0,-1,0};
    int dy[4] = {0,1,0,-1};
    while(1){
        count = 0;
        cin >> W >> H;
        if(W==0&&H==0)break;
        for(int i=0;i<H;i++){
            for(int k=0;k<W;k++){
                cin >> baf_char;
                if(baf_char=='@'){
                    start_x = k;
                    start_y = i;
                    pad[i][k].flag = 1;
                }else if(baf_char=='#'){
                    pad[i][k].flag = 1;
                }else{
                    pad[i][k].flag = 0;
                }
                pad[i][k].x=k;
                pad[i][k].y=i;
                pad[i][k].M = baf_char;
            }
        }
        //pad[start_y][start_x].flag = 1;
        que.push(pad[start_y][start_x]);
        while(que.size()!=0){
            struct PAD p = que.front();
            que.pop();
            for(int j = 0;j<4;j++){
                int nx = p.x + dx[j];
                int ny = p.y + dy[j];
                if(ny>=0&&nx>=0&&ny<=(H-1) && nx<=(W-1) && pad[ny][nx].flag == 0 && pad[ny][nx].M == '.'){
                    pad[ny][nx].flag = 1;
                    que.push(pad[ny][nx]);
                    count++;
                }
            }
        }
        cout << count+1 << endl;
    }


    return 0;
}

C++: #include <iostream>

using namespace std;

int W, H;
char map[25][25];
int sx, sy;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

int dfs(int x, int y)
{
  int ret = 1;
  map[x][y] = '#';
  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) continue;
    if(map[nx][ny] == '#') continue;
    ret += dfs(nx, ny);
  }
  return ret;
}

int main(void)
{
  ios::sync_with_stdio(false);
  cin.tie(0);

  while(1){
    cin >> W >> H;
    if(W == 0 && H == 0) break;
    for(int y = 0; y < H; y++){
      for(int x = 0; x < W; x++){
        cin >> map[x][y];
        if(map[x][y] == '@'){
          sx = x, sy = y;
          map[x][y] = '.';
        }
      }
    }
    cout << dfs(sx, sy) << endl;
  }
  return 0;
}

C++: //??¨??¢?´¢:??±???????????¢?´¢??????????????¢?´¢ 

//??????????????¢?????¬?????????
 
//?????°??¢??°???????????????(?????????)????????\???(?????????)
#include <stdio.h>
#include <queue>
#include <string.h>
using namespace std;	
int W,H;
char c[101][101];
bool visited[100][100];
 
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
 	
void compute();//????????????????????£?¨? 
	
int main(){
	
	while(scanf("%d %d",&W,&H),W |H){
	    int i;
		
		for(i=0;i<H;i++){
			scanf(" %s",&c[i][0]);
		}
	}
	return 0;
}

void compute(){
	
	memset(visited,false,sizeof visited);  //{????????????????????????1bit??????????????????1bit????????§?????????} 
	
	queue<int> que;
	int i,j;
	for(j=0;j<W;j++){ 
	 for(i=0;i<H;i++){
	  if(c[i][j]=='@'){
	  	que.push(j+i*H);
	  	visited[i][j]=true;
	  	 break;
	  }
	 }
	}
  int answer=1;
  while(!que.empty()){
	int cur=que.front();
	        que.pop();
	        int x=cur%W;
	        int y=cur/W;
    

  for(i=0;i<4;i++){
	int nx=x+dx[i],ny=y+dy[j];
	
  
  
  if(!(0<=nx&&nx<W&&0<=ny))continue;
  if(c[ny][nx]=='#'||visited[ny][nx])continue;

  que.push(nx+ny*W);
  visited[ny][nx]=true;

    }
   }
 
 }

C++: #include <iostream>
#include <cstring>
using namespace std;
#define MAX 21
int w,h;
char map[MAX][MAX];
int dx[]={ 0, 1, 0,-1};
int dy[]={ 1, 0,-1, 0};
int count;
void search(int y,int x){
	//cout<<"now:("<<x<<","<<y<<")"<<endl;
	map[y][x]='#';
	for(int i=0;i<4;i++){
		int nextX=x+dx[i];
		int nextY=y+dy[i];
		//cout<<"next:("<<nextX<<","<<nextY<<")"<<endl;
		if(nextY<0||nextX<0||nextY>=h||nextX>=w || map[nextY][nextX]=='#')continue;
		++count;
		search(nextY,nextX);
	}
	return;
}
int main(){
	while(cin>>w>>h && (w||h)){
		int st_w,st_h;
		memset(map,' ',sizeof(map));
		for(int i=0;i<h;i++){
			cin>>map[i];
			for(int j=0;j<w;j++){
				if(map[i][j]=='@'){
					st_w=j;st_h=i;
				}
			}
		}
		count=1;
		search(st_h,st_w);
		cout<<count<<endl;
	}
	return 0;
}