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 <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <fstream>
#include <queue>

#pragma warning( disable: 4996 )

using namespace std;

typedef long long ll;

#define INF 100000000
#define EPS 1e-9

#define MAX_N 50
#define MAX_R 50

#define MAX_W 20
#define MAX_H 20

int W, H;
int ans[100000];
int C = 0;
char maze[MAX_W][MAX_H];

void dfs(int sx, int sy){
	
	maze[sy][sx] = '#';

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

	for (int i = 0; i < 4; i++){
		int nx = sx + dx[i];
		int ny = sy + dy[i];
		if (nx >= 0 && nx < W && ny >= 0 && ny < H && maze[ny][nx] != '#'){
			ans[C]++;
			dfs(nx, ny);
		}
	}

}

int main(){

	while (true){
		cin >> W >> H;
		if (W == 0 && H == 0){
			break;
		}
		int sx;
		int sy;
		for (int i = 0; i < H; i++){
			for (int j = 0; j < W; j++){
				cin >> maze[i][j];
				if (maze[i][j] == '@'){
					sx = j;
					sy = i;
				}
			}
		}

		ans[C] = 1;
		dfs(sx,sy);
		C++;

	}

	for (int i = 0; i < C; i++){
		cout << ans[i] << endl;
	}


	return 0;

}

C++: #include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define pb push_back
#define debug(x) cout<< #x <<": "<<x<<endl
#define debug2(x,y) cout<< #x <<": "<< x <<", "<< #y <<": "<< y <<endl

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<i_i, int> p_i;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
template<class T> using vv=vector<vector< T > >;
typedef deque<int> di;
typedef deque<deque<int> > ddi;

int main() {
  while (true) {
    int w, h;
    cin >> w >> h;
    if (h == 0) { break; }
    vvi tiles(h+2, vi(w+2, -1)); // -1 : red, 0 : black, 1 : reached
    di current(2); // w(x), h(y)
    for (int ih = 1; ih <= h; ++ih) {
      for (int iw = 1; iw <= w; ++iw) {
        char c_tmp;
        cin >> c_tmp;
        if (c_tmp != '#') {
          tiles[ih][iw] = 0;
        }
        if (c_tmp == '@') {
          current = {ih, iw};
          tiles[ih][iw] = 1;
        }
      }
    }

    ddi s;
    s.pb(current);
    int reachable = 1;
    while ( !s.empty() ) {
      current = s.front();
      vvi search = {{1,0},{0,1},{-1,0},{0,-1}};
      for (auto v : search) {
        int next_h = current[0] + v[0];
        int next_w = current[1] + v[1];
        if (tiles[next_h][next_w] == 0) {
          tiles[next_h][next_w] = 1;
          reachable += 1;
          s.pb({next_h, next_w});
        }
      }
      s.pop_front();
    }
    printf("%d\n", reachable);
  }

  return 0;
}

C++: // 縦型探索
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int w, h;
int mx[] = {0, 0, -1, 1};
int my[] = {-1, 1, 0, 0};

int dfs(int x, int y, vector<string> &v) {
    int cnt = 1;
    for ( int i = 0; i < 4; i++ ) {
        int nx = x + mx[i], ny = y + my[i];
        if ( 0 <= nx && nx < w && 0 <= ny && ny < h && v[ny][nx] == '.' ) {
            v[ny][nx] = '#';
            cnt += dfs(nx, ny, v);
        }
    }
    return cnt;
}

int main() {
    while ( true ) {
        int x, y;
        cin >> w >> h;
        if ( w == 0 && h == 0 ) break;

        vector<string> v(h);

        for ( int i = 0; i < h; i++ ) {
            cin >> v[i];

            for ( int j = 0; j < v[i].size(); j++ ) {
                if ( v[i][j] == '@' ) {
                    y = i;
                    x = j;
                }
            }
        }

        cout << dfs(x, y, v) << endl;
    }
    return 0;
}

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

char map[21][21];
int tx[4] = {-1,0,1,0};
int ty[4] = {0,1,0,-1};
int w,h,c;

int search(int y,int x){
  int i,nx,ny;
  c++;
  for(i=0;i<4;i++){
    nx=x+tx[i];
    ny=y+ty[i];
    if(0<=nx && nx<w && 0<=ny && ny<h && map[ny][nx]!='#'){
      map[ny][nx]='#';
      search(ny,nx);
    }
  }
  
  return c;
  
}

int main(){

  int i,j,nx,ny;
  int a;

  while(1){
    c=0;
    cin>>w>>h;
  
    if(w==0&&h==0){
      break;
    }	    
 
    
    for(i=0; i<h ; i++){
      for(j=0; j<w ; j++){
	cin>>map[i][j];
	if(map[i][j]=='@'){
	  nx = j;
	  ny = i;
	}
      } 
    }
    
    map[ny][nx]='#';
    a=search(ny,nx);
    cout<<c<<endl;
    
  }
  return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#define mod 1000000007 //1e9+7
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 201

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

void calc(int h,int w){
  char mm[SIZE][SIZE];
  int sy,sx;
  int ans = 0;

  for(int i=0;i<h;i++){
    scanf("%s",mm[i]);

    for(int j=0;j<w;j++){
      if(mm[i][j]=='@'){
	sy = i;
	sx = j;
	mm[i][j]='.';
      }
    }
  }

  queue<pair<int,int> > q;

  q.push({sy,sx});

  while(q.size()){
    int y = q.front().first;
    int x = q.front().second;
    q.pop();

    if(mm[y][x]!='.') continue;
    mm[y][x]='#';
    ans++;

    for(int i=0;i<4;i++){
      if(y+dy[i]<0 || h<=y+dy[i] || x+dx[i]<0 || w<=x+dx[i]) continue;

      if(mm[y+dy[i]][x+dx[i]]=='.'){
	q.push({y+dy[i],x+dx[i]});
      }
    }
  }
  
  printf("%d\n",ans);
}

int main(){
  int h,w;
  
  while(1){
    scanf("%d%d",&w,&h);
    if(h==0) break;
    calc(h,w);
  }
  
  return 0;
}