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<algorithm>

using namespace std;
int W,H;
void solve(int,int);
char dfs[20][20];
int d_x[]={1,0,-1,0},d_y[]={0,1,0,-1};

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

		int dx,dy;
		for(int i=0;i<H;i++){
			for(int j=0;j<W;j++){
				cin>>dfs[j][i];
				if(dfs[j][i]=='@'){
					dx=j;
					dy=i;
				}
			}
		}
		solve(dx,dy);
		for(int i=0;i<H;i++)
			for(int j=0;j<W;j++)
				if(dfs[j][i]=='T')
					ans++;
		cout<<ans<<endl;
		memset(dfs,0,sizeof(dfs));
	}
}

void solve(int x,int y){
	dfs[x][y]='T';
	
	for(int j=0;j<4;j++){
		int mx=x+d_x[j],my=y+d_y[j];
		if(mx<0||my<0||mx>W||my>H)	continue;
		if(dfs[mx][my]=='.')
			solve(mx,my);
	}
	return ;
}

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

const int MAXH = 30;
const int MAXW = 30;
int W, H;
char G[MAXH][MAXW];

int dfs(int x, int y) {
  const static int dx[] = {1,0,-1,0};
  const static int dy[] = {0,1,0,-1};
  G[y][x] = '#';
  int res = 1;
  for(int k = 0; k < 4; ++k) {
    int nx = x + dx[k];
    int ny = y + dy[k];
    if(nx < 0 || nx >= W) continue;
    if(ny < 0 || ny >= H) continue;
    if(G[ny][nx] == '#') continue;
    res += dfs(nx, ny);
  }
  return res;
}

int main() {
  for(; cin >> W >> H && (W|H);) {
    int sx, sy;
    for(int i = 0; i < H; ++i) {
      for(int j = 0; j < W; ++j) {
        cin >> G[i][j];
        if(G[i][j] == '@') {
          sx = j;
          sy = i;
        }
      }
    }
    cout << dfs(sx, sy) << endl;
  }
  return 0;
}

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

int count;
int w,h;
int x,y;
char map[20][20];

void tannkenn(int x,int y);

int main()
{
	cin>>w>>h;
	count=0;
	for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                cin >> map[i][j];
				if(map[i][j]=='@'){
					x=i;
					y=j;
				}
			}
	}

	tannkenn(x,y);
	cout<<count<<endl;

	return 0;
}

void tannkenn(int x,int y)
{
	count++;
	map[x][y]='#';

	if(x+1<h && map[x+1][y]=='.')
		tannkenn(x+1,y);
	if(y+1<w && map[x][y+1]=='.')
		tannkenn(x,y+1);
	if(x-1>=0 && map[x-1][y]=='.')
		tannkenn(x-1,y);
	if(y-1>=0 && map[x][y-1]=='.')
		tannkenn(x,y-1);
}

C++: #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
typedef long long ll;
int r[20][20],ans,w,h;
char C[20][20];
int search(int x,int y){
    if(x<0||x>=w||y<0||y>=h||C[y][x]=='#'||r[y][x]) return 0;
    r[y][x]=1;
    ans++;
    search(x+1,y);search(x-1,y);search(x,y+1);search(x,y-1);
}
int main(){
    while(true){
        cin>>w>>h;
        if(w==0) break;
        int sx,sy;
        rep(i,h)rep(j,w){
            cin>>C[i][j];
            if(C[i][j]=='@') sx=j,sy=i;
        }
        rep(i,h)rep(j,w) r[i][j]=0;
        ans=0;
        search(sx,sy);
        cout<<ans<<endl;
    }
}

C++: #include<iostream>
using namespace std;
typedef pair<int, int> pii;
pii dxdy[]={{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
char fld[21][21];
int used[20][20];
int W, H;

int rec(int i, int j){

   if(fld[i][j]=='#') return 0;
   if(used[i][j]) return 0;
   used[i][j]=1;
   int ret=0;
   for(pii dd: dxdy){
      int ni=i+dd.first;
      int nj=j+dd.second;
      if(!(0<=ni&&ni<H&&0<=nj&&nj<W)) continue;
      ret+=rec(ni, nj);
   }

   return ret+1;
}

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;
               fld[i][j]='.';
            }
         }
      }

      for(int i=0; i<20; i++)for(int j=0; j<20; j++) used[i][j]=0;
      cout<< rec(sy, sx)<< endl;
   }

   return 0;
}

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

vector<string> f;
void init(){
  f.clear();
}

int input(){
  int w,h;
  cin>>w>>h;
  if(w==0&&h==0)return false;
  string s;
  for(int i=0;i<h;i++){
    cin>>s;
    f.push_back(s);
  }
  return true;
}

int vx[] = {0,0,1,-1};
int vy[] = {1,-1,0,0};

int rec(int x,int y){
  if(f[x][y]!='.')return 0;
  f[x][y]='#';
  int res = 1;
  for(int i=0;i<4;i++){
    res += rec(x+vx[i],y+vy[i]);
  }
  return res;
}

int solve(){
  int x,y;
  for(int i=0;i<f.size();i++){
    for(int j=0;j<f[i].size();j++){
      if(f[i][j]=='@'){
        x = i;
        y = j;
        f[i][j] = '.';
      }
    }
  }
  return rec(x,y);
}

int main(){
  while(init(),input()){
    cout<<solve()<<endl;
  }
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
bool c[22][22] = {} ;
int cnt,w, h, p[22][22] = {};

int main() {
	int j, f,cx,cy;
	char in[21];
	while (1) {
		for (int i = 0; i < 484; i++) {
			c[i / 22][i % 22] = p[i / 22][i % 22] = 0;
		}
		cnt = 0;
		f = 1;
		scanf("%d %d", &w, &h);
		if (w ==0&& h == 0)
			break;
		scanf("\n", &j);
		for (int i = 0; i < h; i++) {
			scanf("%s", &in);
			for (j = 0; j < w; j++) {
				switch (in[j]) {
				case '#':
					c[j+1][i+1] = 0;
					p[j+1][i+1] = -1;
					break;
				case '@':
					cx = j;
					cy = i;
					p[j+1][i+1] = 1;
				case '.':
					c[j+1][i+1] = 1;
				}
			}
		}

		/*for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++)
				printf("%3d", c[j][i]);
			printf("\n");
		}*/

		while (f == 1) {
			f = 0;
/*			for (int i = 0; i < h; i++) {
				for (int j = 0; j < w; j++)
					printf("%3d", p[j+1][i+1]);
				printf("\n");
			}
*/			printf("\n");
			if (w != 1 && h != 1) {
				for (int i = 0; i < w*h; i++) {
					if (p[i / h+1][i%h+1] == 0 && (p[i / h + 2][i % h+1] == 1 || p[i / h][i % h+1] == 1 || p[i / h+1][i % h + 2] == 1 || p[i / h+1][i % h] == 1)) {
						p[i / h+1][i%h+1] = 1;
						f = 1;
					}
				}
			}
			if (w == 1 && h != 1) {
				for (int i = 0; i < h; i++) {
					if (p[1][i+1] == 0 && (p[1][i + 2] == 1 || p[0][i] == 1)) {
						p[1][i+1] = 1;
						f = 1;
					}
				}
			}

			if (w != 1 && h == 1) {
				for (int i = 0; i < w; i++) {
					if (p[i+1][1] == 0 && (p[i + 2][1] == 1 || p[i][1] == 1)) {
						p[i+1][1] = 1;
						f = 1;
					}
				}
			}
		}

		for (int i = 0; i < w*h; i++) {
			if (p[i / h+1][i%h+1] == 1)
				cnt++;
		}
		printf("%d\n", cnt);
	}
	return 0;
}