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>
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>
#include <vector>
#include <string>
#include <utility>
#include <queue>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
#include <climits>
//#include "h.h"
using namespace std;

#define ll long long
#define pint pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define mp make_pair
#define pm(first, second) pb(mp(first, second))
#define SPACE " "
#define fpf first.first
#define fps first.second
#define spf second.first
#define sps second.second

#define all(X) (X).begin(), (X).end()

int main(){

	while(true){
		int W, H;
		cin >> W >> H;
		if(W == 0 && H == 0)break;
		vector<string> S(H);
		queue<pair<int, int>> Q;
		for(int i = 0; i < H; i ++){
			cin >> S[i];
			for(int j = 0; j < W; j ++){
				if(S[i][j] == '@')Q.push(mp(i, j));
			}
		}
		pair<int, int> P;
		while(!Q.empty()){
			P = Q.front();
			Q.pop();
			S[P.first][P.second] = '@';
			if(P.first > 0 && S[P.first - 1][P.second] == '.')Q.push(mp(P.first - 1, P.second));
			if(P.first < H - 1 && S[P.first + 1][P.second] == '.')Q.push(mp(P.first + 1, P.second));
			if(P.second > 0 && S[P.first][P.second - 1] == '.')Q.push(mp(P.first, P.second - 1));
			if(P.second < W - 1 && S[P.first][P.second + 1] == '.')Q.push(mp(P.first, P.second + 1));
		}
		int ans = 0;
		for(int i = 0; i < H; i ++){
			for(int j = 0; j < W; j ++){
				if(S[i][j] == '@')ans ++;
			}
		}
		cout << ans << endl;
	}

	return 0;
}

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

const int W = 20, H = 20;
char board[H][W];
int w, h;

int dfs(int x, int y){
	int dx[] = {0, -1, 1, 0}, dy[] = {-1, 0, 0, 1};
	int c = 1;
	board[y][x] = '#';
	for(int i = 0;i < 4;++i){
		int tx = x + dx[i], ty = y + dy[i];
		if(0 <= tx && 0 <= ty && tx < w && ty < h && board[ty][tx] == '.'){
			c += dfs(tx, ty);
		}
	}
	return c;
}

int main(){
	int sx, sy;
	while(cin >> w >> h, w){
		for(int i = 0;i < h;++i){
			for(int j = 0;j < w;++j){
				cin >> board[i][j];
				if(board[i][j] == '@'){
					sx = j;
					sy = i;
				}
			}
		}
		cout << dfs(sx, sy) << endl;
//		for(int i = 0;i < h;++i){
//			for(int j = 0;j < w;++j){
//				cout << board[i][j];
//			}
//			cout << endl;
//		}
	}
	return 0;
}

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

int main(){
	while(1){
		int W, H, x[30][30] = {};
		char str[30][30];
		cin>>W>>H;
		if(W==0 && H==0) break;
		for(int i = 0; i < H; i++){
			for(int j = 0; j < W; j++){
				cin>>str[i][j];
				if(str[i][j] == '@') x[i][j] = 1;
			}
		}
	
		int q = 0;
		while(1){
			int z = 0;
			for(int i = 0; i < H; i++){
				for(int j = 0; j < W; j++){
					if(x[i][j] == 1){
						if(i > 0 && str[i - 1][j] == '.') x[i - 1][j] = 1;
						if(i < H - 1  && str[i + 1][j] == '.') x[i + 1][j] = 1;
						if(j > 0 && str[i][j - 1] == '.') x[i][j - 1] = 1;
						if(j < W - 1&& str[i][j + 1] == '.') x[i][j + 1] = 1;
					}
				}
			}
			for(int i = 0; i < H; i++){
				for(int j = 0; j < W; j++){
					if(x[i][j] == 1){
						z++;
					}
				}
			}
			if(z == q) break;
			q = z;
		}

		
		cout<<q<<endl;
	}
	return 0;
}

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

int ans,w,h;
string g[30];
bool visit[30][30];

int rec(int y,int x){
  int res = 1;
  int dx[] = {0,1,0,-1}, dy[] = {-1,0,1,0};
  visit[y][x] = true;

  for(int k=0;k<4;k++){
    int sy = y+dy[k] ,sx = x+dx[k];
    if(sy<0 || sx<0 || h<=sy || w<=sx)continue;
    if(visit[sy][sx] || g[sy][sx] == '#')continue;
    res += rec(sy,sx);
  }
  return res;
}

int main(){
  while(cin >> w >> h && (w||h)){

    for(int i=0;i<h;i++)for(int j=0;j<w;j++)visit[i][j] = false;
    for(int i=0;i<h;i++)cin >> g[i];

    ans = 0;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	if(g[i][j] == '@')cout << rec(i,j) << endl;
      }
    }
  }
}

C++: #include <iostream>
#include <queue>
using namespace std;
typedef pair<int,int> P;
int W,H,sx,sy;
int dx[4] = {-1,1,0,0},dy[4] = {0,0,-1,1};
char F[21][21];
int visited[21][21] = {};

bool in(int x,int y){
    return 1<=x && x<=H && 1<=y && y<=W;
}

int main(){
    while(cin >> W >> H && W>0){
        queue<P> Q;
        for(int i=1;i<=H;i++){
            for(int j=1;j<=W;j++){
                visited[i][j] = 0;
                cin >> F[i][j];
                if(F[i][j]=='@'){
                    visited[i][j] = 1;
                    Q.push({i,j});
                }
            }
        }
        int ans = 0;
        while(!Q.empty()){
            ans++;
            int x = Q.front().first,y = Q.front().second; Q.pop();
            for(int i=0;i<4;i++){
                int nx = x+dx[i],ny = y+dy[i];
                if(in(nx,ny) && F[nx][ny]!='#' && visited[nx][ny]==0){
                    visited[nx][ny] = 1;
                    Q.push({nx,ny});
                }
            }
        }
        cout << ans << endl;
    }
}