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 <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int INF=1e9;

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

	int w,h;
	while(cin>>w>>h and w>0){
		queue<pair<int,int>> q;
	    int sy,sx;
		char c[h][w]; int d[h][w];
		rep(i,h){
	        rep(j,w){
	            cin>>c[i][j]; d[i][j]=INF;
				if(c[i][j]=='@'){
					sy=i; sx=j;
				}
	        }
	    }
		d[sy][sx]=0;
	    q.push(make_pair(sy,sx));

		int ans=1;
		while(q.size()){
	        pair<int,int> p=q.front(); //キューの先頭を取り出す
	        q.pop(); //取り出したら削除
	        int prev=d[p.first][p.second]; //一手前の手数prev
	        rep(i,4){ //4方向探索
	            int cy=p.first+dy[i];
	            int cx=p.second+dx[i];
	            if(c[cy][cx]=='.' and d[cy][cx]==INF){ //通行可能かつ未到達
					if(0<=cy and cy<h){
						if(0<=cx and cx<w){
							d[cy][cx]=prev+1;
			                q.push(make_pair(cy,cx));
							ans++;
						}
					}
	            }
	        }
	    }
		cout<<ans<<endl;
	}
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

const int dx[] = {0, 0, 1, -1};
const int dy[] = {-1, 1, 0, 0};
int W, H;
char board[30][30];

int dfs(int y, int x) {
   board[y][x] = '#';
   int ret = 1;
   for (int i = 0; i < 4; i++) {
      int ny = y + dy[i], nx = x + dx[i];
      if (ny >= 0 && ny < H && nx >= 0 && nx < W && board[ny][nx] == '.') {
         ret += dfs(ny, nx);
      }
   }
   return ret;
}

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

      for (int i = 0 ; i < H; i++) {
         for (int j = 0 ; j < H; j++) {
            if (board[i][j] == '@') {
               cout << dfs(i, j) << endl;
            }
         }
      }
   }

   return 0;
}

C++: #include<iostream>
using namespace std;
typedef pair<int, int> pii;
pii dxdy[]={{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
char fld[20][20];
bool used[20][20];
int W, H;
 
void rec(int i, int j){
   used[i][j]=1;
   for(pii dd: dxdy){
      int ni=i+dd.first;
      int nj=j+dd.second;
      if(!(0<=ni&&ni<H&&0<=nj&&nj<W)) continue;
      if(used[ni][nj]) continue;
      if(fld[ni][nj]=='#') continue;
      rec(ni, nj);
   }
}
 
int main(){
 
   while(1){
      cin>> W>> H;
      if(!(H||W)) break;
      int sx, sy;
      for(int i=0; i<H; i++){
         for(int j=0; j<W; j++){
            cin>> fld[i][j];
            if(fld[i][j]=='@'){
               sy=i;
               sx=j;
            }
         }
      }
 
      for(int i=0; i<20; i++)for(int j=0; j<20; j++) used[i][j]=0;
      used[sy][sx]=1;
      rec(sy, sx);
      int ans=0;
      for(int i=0; i<H; i++)for(int j=0; j<W; j++){
         ans+=used[i][j];
      }
      cout<< ans<< endl;
 
   }
 
   return 0;
}

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

int w, h;
char tile[21][21];

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

int func(int x, int y){
	if(x < 0 || x >= w || y < 0 || y >= h || tile[y][x] == '#'){
		return 0;
	}
	int res = 1;
	tile[y][x] = '#';
	for(int i = 0; i < 4; ++i){
		for(int j = 0; j < 4; ++j){
			res += func(x + mx[i], y + my[i]);
		}
	}
	return res;
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	
	while(cin >> w >> h && w != 0 && h != 0){
	
		int sx = 0, sy = 0;
		for(int i = 0; i < h; ++i){
			for(int j = 0; j < w; ++j){
				cin >> tile[i][j];
				if(tile[i][j] == '@'){
					sy = i;
					sx = j;
				}
			}
		}
		
		cout << func(sx, sy) << endl;
	}
	
	return 0;
}

C++: #include<iostream>
#include<stdio.h>
#include<vector>
#include<stdlib.h>
#include<set>
#include<string>
#include<queue>
#include<map>
#include<stack>
#include <algorithm>

using namespace std;

int main(){
	int w, h;
	int sx, sy;
	int dx[4] = { 1, 0, -1, 0 }, dy[4] = {0 , 1 , 0 , -1};
	char str[20];

	cin >> w >> h;
	do{
		int masu[20][20] = { 0 };
		int check_masu[20][20] = { 0 };

		for (int i = 0; i < h; i++){
			cin >> str;
			for (int j = 0; j < w; j++){
				masu[j][i] = str[j];
				if (masu[j][i] == '@'){
					sx = j; sy = i;
				}
			}
		}

		int x, y;
		int count = 0; //count looked check
		stack< pair<int, int> > s;
		s.push(make_pair(sx, sy));	//set start place 
		while (s.size()){
			x = s.top().first;
			y = s.top().second;
			s.pop();

			if (check_masu[x][y] == 'c' || masu[x][y] == '#')
				continue;

			check_masu[x][y] = 'c';	//looked
			count++;

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

					s.push(make_pair(x + dx[i], y + dy[i]));
				}
			}
		}

		cout << count << endl;

		cin >> w >> h;
	} while (w != 0 && h != 0);

	return 0;
}