Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

var (
	sc = bufio.NewScanner(os.Stdin)
)

type Color int

const (
	ColorBlack Color = iota
	ColorRed
)

type Node struct {
	ID    int
	X, Y  int
	Color Color
	Nexts []*Node
}

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])

		// Initialize map
		m := map[int]map[int]*Node{}
		for x := 1; x <= w; x++ {
			m[x] = map[int]*Node{}
			for y := 1; y <= h; y++ {
				m[x][y] = &Node{ID: x + y*w, X: x, Y: y}
			}
		}
		for x := 1; x <= w; x++ {
			for y := 1; y <= h; y++ {
				nexts := []*Node{}
				if n, ok := m[x-1][y]; ok {
					nexts = append(nexts, n)
				}
				if n, ok := m[x][y-1]; ok {
					nexts = append(nexts, n)
				}
				if n, ok := m[x+1][y]; ok {
					nexts = append(nexts, n)
				}
				if n, ok := m[x][y+1]; ok {
					nexts = append(nexts, n)
				}
				m[x][y].Nexts = nexts
			}
		}

		// Set colors
		queue := []*Node{}
		touchedIDs := map[int]struct{}{}
		for y := 1; y <= h; y++ {
			sc.Scan()
			for i, c := range sc.Text() {
				switch c {
				case '.', '@':
					n := m[i+1][y]
					n.Color = ColorBlack
					if c == '@' {
						queue = append(queue, n)
						touchedIDs[n.ID] = struct{}{}
					}
				case '#':
					m[i+1][y].Color = ColorRed
				}
			}
		}

		// Search
		visitedIDs := map[int]struct{}{}
		for {
			if len(queue) == 0 {
				break
			}
			n := queue[0]
			queue = queue[1:]
			visitedIDs[n.ID] = struct{}{}
			for _, next := range n.Nexts {
				if next.Color == ColorRed {
					continue
				}
				if _, ok := touchedIDs[next.ID]; !ok {
					queue = append(queue, next)
					touchedIDs[n.ID] = struct{}{}
				}
			}
		}
		fmt.Println(len(visitedIDs))
	}
}

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

using namespace std;

struct locate{
  int x;
  int y;
};
 
char graph[20][20];
int cou = 0;
int H,W;

void bfs(locate z){
  queue<locate> Q;
  locate u;
  locate v;
  Q.push(z);
 

  while(!Q.empty()){

    u = Q.front();Q.pop();

    if(graph[u.y + 1][u.x] == '.' && graph[u.y + 1][u.x] != 'N'){
      graph[u.y + 1][u.x] = 'N';
      cou++;
      v.y = u.y + 1;
      v.x = u.x;
      Q.push(v);
    }

    if(graph[u.y][u.x + 1] == '.' && graph[u.y][u.x + 1] != 'N'){
      graph[u.y][u.x + 1] = 'N';
      cou++;
      v.x  = u.x + 1;
      v.y = u.y;
      Q.push(v);
    }

    if(graph[u.y - 1][u.x] == '.' && graph[u.y - 1][u.x] != 'N'){
      graph[u.y - 1][u.x] = 'N';
      cou++;
      v.x = u.x;
      v.y = u.y - 1;
      Q.push(v);
    }

    if(graph[u.y][u.x - 1] == '.' && graph[u.y][u.x - 1] != 'N'){
      graph[u.y][u.x - 1] = 'N';
      cou++;
      v.x = u.x - 1;
      v.y = u.y;
      Q.push(v);
    }
    
  }
  cout << endl;
    for(int i = 0; i< H ;i++){
      for(int j = 0; j < W; j++){
	cout << graph[i][j];
      }
      cout << endl;
      }
}

int main(){

  int a1,a2;
  locate a;
  while(1){

    cin >> W >> H;

    if(W == 0 && H == 0) break; 


    for(int i = 0; i< H ;i++){
      for(int j = 0; j < W; j++){
	cin >> graph[i][j];
	if(graph[i][j] == '@'){
	  a1 = i;
	  a2 = j;
	}
      }
    }

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

    a.y = a1;
    a.x = a2;
    bfs(a);

    cout << cou+1 << endl;

    cou = 0;
    for(int i = 0;i < H ;i++){
      for(int j = 0;j < W; j++){
	graph[i][j] = 'N';
      }
    }
    cou = 0;
  }
  return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <algorithm>
#include <functional>

using namespace std;

#define rep(i,n) for((i)=0;(i)<(int)(n);(i)++)
#define each(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)

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

int w,h,res;
char field[30][30];

void dfs(int y, int x){
  int i;

  res++;
  rep(i,4){
    int my = y + dy[i], mx = x + dx[i];
    if(0 <= my && 0 <= mx && my < h && mx < w && field[my][mx] != '#' && field[my][mx] != '@'){
      field[my][mx] = '#';
      dfs(my,mx);
    }
  }
}

int main(){
  int i,j;

  while(true){
    scanf("%d %d",&w,&h);
    if(w == 0 && h == 0) break;
    res = 0;
    rep(i,h) scanf("%s",field[i]);
    rep(i,h) rep(j,w) if(field[i][j] == '@') dfs(i,j);
    printf("%d\n",res);
  }

  return 0;
}

C++: #include <iostream>
#include <queue>
#include <vector>
#include <string>
#include <utility>
#include <algorithm>

using namespace std;

#define RED false
#define BLACK true

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

bool valid(int x, int y, const vector<vector<bool> >& area){
  return 0 <= x && x < w && 0 <= y && y < h && area[x][y];
}

int main(){
  while(true){
    cin >> w >> h;
    if(w == 0 && h == 0)
      return 0;

    vector<vector<bool> > area(w, vector<bool>(h, BLACK));
    pair<int, int> hito;
    for(int i = 0; i < h; i++){
      string line;
      cin >> line;
      for(int j = 0; j < w; j++){
        if(line[j] == '#'){
          area[j][i] = RED;
        } else if(line[j] == '@'){
          hito = { j, i };
        }
      }
    }

    queue<pair<int, int> > qu;
    qu.push(hito);

    vector<vector<bool> > visit(w, vector<bool>(h, false));
    visit[hito.first][hito.second] = true;

    int ans = 1;

    while(!qu.empty()){
      auto now = qu.front();
      // cout << "now: " << now.first << ", " << now.second << endl;
      qu.pop();
      for(int i = 0; i < 4; i++){
        auto dst = make_pair( now.first + dx[i], now.second + dy[i] );
        if(valid(dst.first, dst.second, area) && !visit[dst.first][dst.second]){
          visit[dst.first][dst.second] = true;
          ans++;
          qu.push(dst);
        }
      }
    }
    cout << ans << endl;
  }
}

C++: #include<iostream>
#include<cstdio>
#include<string>
using namespace std;
#define loop(i,a,b) for(int i = a ; i < b ; i++)
#define rep(i,a) for(int i = 0 ; i < a ; i++) 

char maze[21][21];
int dx[4] = { 0, 0,-1, 1};
int dy[4] = {-1, 1, 0, 0};
int H,W;
bool inmaze(int x,int y){
  return (0<=x && x<H) && (0<=y && y<W);
}

int dfs(int x,int y){
  if(!inmaze(x,y))return 0;
  maze[x][y] = '#';
  int res = 1;
  rep(i,4){
    if(maze[x+dx[i]][y+dy[i]] == '.'){
      res += dfs(x+dx[i],y+dy[i]);
    }
  }
  return res;
}
int main(void){
  while(cin>>W>>H,H+W){
    int sx,sy;
    rep(i,H){
      string s;
      cin>>s;
      rep(j,W){
	maze[i][j] = s[j];
	if(s[j] == '@'){
	  sx = i;sy = j;
	}
      }
    }
    maze[sx][sy] = '.';
    cout<<dfs(sx,sy)<<endl;
  }
}

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%H;
	        int y=cur/H;
    

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

  que.push(nx+ny*H);
  visited[ny][nx]=true;
++answer;
    }
   }
 //cout << answer << endl;
 printf("%d\n",answer);
 }