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;
int main(){
	int w, h; 
	while (cin >> w >> h) {
		if (!w && !h)return 0;
		char map[21][21];
		bool b[21][21];
		queue<pair<int, int> >bfs;
		for (int i = 0; i < h; i++)for (int j = 0; j < w; j++) {
			cin >> map[i][j];
			b[i][j] = false;
			if (map[i][j] == '@') {
				bfs.push(make_pair(i, j));
				b[i][j] = true;
			}
		}
		int ans = 1;
		while (bfs.size() > 0) {
			pair<int, int>p = bfs.front();
			int x = p.first;
			int y = p.second;
			map[x][y] = '#';
			if (x > 0){
				if (map[x - 1][y] == '.' && !b[x - 1][y]) {
					bfs.push(make_pair(x - 1, y));
					ans++;
					b[x - 1][y] = true;
				}
			}
			if (x < h) {
				if (map[x + 1][y] == '.' && !b[x + 1][y]) {
					bfs.push(make_pair(x + 1, y));
					ans++;
					b[x + 1][y] = true;
				}
			}
			if (y > 0) {
				if (map[x][y - 1] == '.'&& !b[x][y - 1]) {
					bfs.push(make_pair(x, y - 1));
					ans++;
					b[x][y - 1] = true;
				}
			}
			if (y < w) {
				if (map[x][y + 1] == '.' && !b[x][y + 1]) {
					bfs.push(make_pair(x, y + 1));
					ans++;
					b[x][y + 1] = true;
				}
			}
			bfs.pop();
		}
		cout << ans << endl;
	}
}

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

int dfs(int x,int y);
int h,w;
char ta[22][22];
//int cheak[22][22]={0};
int dx[4]={-1,0,1,0};
int dy[4]={0,-1,0,1};
int cou=0;

int main(){
  int a,b;
  while(1){
    cin >> w >> h;
    if(w==0 && h==0) break;
    cou=0;
    
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	cin >> ta[i][j];
	cheak[i][j]=0;
	if(ta[i][j] == '@'){
	  a=i,b=j;
	}
      }
    }
    //cout << a << " " << b << endl;
    cout << dfs(a,b) << endl;
    /*for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
      cout << cheak[i][j];
      }
      cout << endl;
      }*/
  }
}

void dfs(int x,int y){
  //cheak[x][y]=1;
  ta[nx][ny]='#';
  cou++;
  for(int i=0;i<4;i++){
    int nx=x+dx[i];
    int ny=y+dy[i];
    if(nx>=0 && nx<h && ny>=0 && ny<w && ta[nx][ny]=='.'){
      dfs(nx,ny);
    }
  }
}

C++: #include<iostream>
#include<iomanip>
#include<string>
#include<cstdlib>
#include<cmath>
#include<algorithm>

using namespace std;

int H, W;
int table[20][20];
int cnt;

void calc(int current_y, int current_x) {
	table[current_y][current_x] = 3;

	if(current_y > 0 && table[current_y-1][current_x] == 1)
		calc(current_y-1, current_x);
	if(current_x > 0 && table[current_y][current_x-1] == 1)
		calc(current_y, current_x-1);
	if(current_y < H-1 && table[current_y+1][current_x] == 1)
		calc(current_y+1, current_x);
	if(current_x < W-1 && table[current_y][current_x+1] == 1)
		calc(current_y, current_x+1);

	cnt++;
}


int main() {
	while(1) {
		cnt = 0;
		cin >> W >> H;
		if(W == 0 && H == 0) break;

		char temp;
		int current[2];
		for(int i=0; i<H; i++) {
			for(int j=0; j<W; j++) {
				cin >> temp;
				if(temp=='.') table[i][j] = 1;
				else if(temp=='#') table[i][j] = 2;
				else if(temp=='@') {
					table[i][j] = 0;
					current[0] = i;
					current[1] = j;
				}
			}
		}

		calc(current[0], current[1]);

		cout << cnt << endl;
	}

	return 0;
}

C++: #include<bits/stdc++.h>
#include<queue>
using namespace std;
int W,H;
char c[101][101];
bool visited [100][100];

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

void compute(){
  memset(visited,false,sizeof visited);


  queue<int> que;
int i,j;
 for(i=0;i<H;++i){
   for(j=0;j<W;++j){
     if(c[i][j]=='@'){
       que.push(j+i*W);
       visited[i][j]=true;
       break;}
       
	 }


   }
 int answer=1;
 
 while(!que.empty()){
   int cur = que.front();
   que.pop();
   int x = cur % W,y=cur/W;

   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*W);
       visited[ny][nx]=true;
       ++answer;
       
     }
 }
 cout<<answer<<endl;
 
}





int main(){
  while(cin>>W>>H,W|H) {
    for(int i=0;i<H;++i) {
      cin>>c[i];	    
    }
    compute();
  }
  return 0;
}

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

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

int w, h;
char B[20][21];

int dfs(int i, int j) {
   B[i][j] = '#';
   int res = 1;
   for (int k = 0; k < 4; k++) {
      int y = i + dy[k], x = j + dx[k];
      if (y >= 0 && y < h && x >= 0 && x < w && B[y][x] == '.') res += dfs(y, x);
   }
   return res;
}

int main() {
   for (; scanf("%d%d", &w, &h), w;) {
      for (int i = 0; i < h; i++) {
         scanf("%s", B[i]);
      }
      for (int i = 0; i < h; i++) {
         for (int j = 0; j < w; j++) {
            if (B[i][j] == '@') printf("%d\n", dfs(i, j));
         }
      }
   }
}

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

int w, h, ret;
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
string data[25];

void func(int nx, int ny)
{
	ret++;
	data[ny][nx] = '#';
	if (nx+1 < w && data[ny][nx+1] == '.') func(nx+1, ny);
	if (0 <= nx-1 && data[ny][nx-1] == '.') func(nx-1, ny);
	if (ny+1 < h && data[ny+1][nx] == '.') func(nx, ny+1);
	if (0 <= ny-1 && data[ny-1][nx] == '.') func(nx, ny-1);
	
	return;
}

int main(void)
{
	while (cin >> w >> h, w && h){
		for (int i = 0; i < 25; i++) data[i].clear();
		for (int i = 0; i < h; i++) {
			cin >> data[i];
		}
		int sx, sy;
		for (int i = 0; i < w; i++){
			for (int j = 0; j < h; j++){
				if (data[j][i] == '@'){
					sx = i, sy = j;
					break;
				}
			}
		}
		ret = 0;
		data[sy][sx] = '#';
		func(sx, sy);
		cout << ret << endl;
	}
	return 0;
}

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

using namespace std;

int tile[22][22] = {};
int w, h;
int cnt;

void fill(int x, int y) {
	tile[x][y] = 0;
	cnt++;
	if (tile[x - 1][y] == 1) fill(x - 1, y);
	if (tile[x][y - 1] == 1) fill(x, y - 1);
	if (tile[x][y + 1] == 1) fill(x, y + 1);
	if (tile[x + 1][y] == 1) fill(x + 1, y);
}

int main() {
	int x = 0, y = 0;
	while (cin >> w >> h && (w || h)) {
		cnt = 0;
		for (int i = 1; i <= h; ++i){
			for (int j = 1; j <= w; ++j) {
				char tmp;
				cin >> tmp;
				if (tmp == '.') tile[i][j] = 1;
				else if (tmp == '#') tile[i][j] = 0;
				else x = i, y = j, tile[i][j] = 1;
			}
		}
		fill(x, y);
		cout << cnt << endl;
	}

	return 0;
}