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>
using namespace std;
const int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};

void solve(int W,int H){
    vector<string> S(H);
    for (int i=0;i<H;++i) cin >> S[i];
    vector<vector<bool>> seen(H,vector<bool>(W,false));
    int ans=0;
    auto dfs=[&](auto self,int x,int y)->void{
        seen[x][y]=true; ++ans;
        for (int i=0;i<4;++i){
            int nx=x+dx[i],ny=y+dy[i];
            if (nx<0||H<=nx||ny<0||W<=ny) continue;
            if (S[nx][ny]=='#') continue;
            if (seen[nx][ny]) continue;
            self(self,nx,ny);
        }
    };
    for (int i=0;i<H;++i){
        for (int j=0;j<W;++j){
            if (S[i][j]=='@'){
                dfs(dfs,i,j);
            }
        }
    }
    cout << ans << '\n';
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int W,H;
    while(cin >> W >> H,W){
        solve(W,H);
    }
}

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

string map[20];
int count,W,H;
int walk(int ,int);

int main(){
	int i,j;
	while(cin >> W >> H,(W || H)){
		count=0;
		for(i=0;i<H;i++) cin >> map[i];
		for(i=0;i<H;i++){
			for(j=0;j<W;j++){
				if(map[i][j]=='@') goto LOOP;
			}
		}
	LOOP:
		walk(i,j);
	cout << count << endl;
	}

}

int walk(int a,int b){
	map[a][b] = '#';
	count++;
	if(a+1<H && map[a+1][b]  == '.') walk(a+1,b);
	if(b+1<W && map[a][b+1]  == '.') walk(a,b+1);
	if(a-1>=0 && map[a-1][b]  == '.') walk(a-1,b);
	if(b-1>=0 && map[a][b-1]  == '.') walk(a,b-1);
	return 0;
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
typedef unsigned int ui;
int main(){
	int w, h;
	string s;
	int i, j;
	while (cin >> w >> h) {
		if (w == 0)break;
		char tile[22][22]; int used[22][22];
		for (i = 0; i < 22; i++) {
			tile[i][0] = '#';
			tile[i][h + 1] = '#';
		}
		for (j = 0; j < 22; j++) {
			tile[0][j] = '#';
			tile[h + 1][j] = '#';
		}
		for (i = 0; i < 22; i++) {
			for (j = 0; j < 22; j++) {
				used[i][j] = 0;
			}
		}
		vector<int> q1,q2;
		for (j = 1; j <= h; j++) {
			cin >> s;
			for (i = 1; i <= w; i++) {
				tile[i][j] = s[i - 1];
				if (s[i - 1] == '@') {
					q1.push_back(i); q2.push_back(j);
				}
			}
		}
		while (!q1.empty()) {
			int x = q1[0]; int y = q2[0]; used[x][y]=1;
			q1.erase(q1.begin() + 0); q2.erase(q2.begin() + 0);
			if (used[x - 1][y] == 0 && tile[x - 1][y]=='.') {
				q1.push_back(x - 1); q2.push_back(y);
			}
			if (used[x + 1][y] == 0 && tile[x + 1][y] == '.') {
				q1.push_back(x + 1); q2.push_back(y);
			}
			if (used[x][y-1] == 0 && tile[x][y-1] == '.') {
				q1.push_back(x); q2.push_back(y-1);
			}
			if (used[x][y+1] == 0 && tile[x][y+1] == '.') {
				q1.push_back(x); q2.push_back(y+1);
			}
		}
		int count = 0;
		for (i = 0; i < 22; i++) {
			for (j = 0; j < 22; j++) {
				if (used[i][j] == 1)count++;
			}
		}
		cout << count << endl;
	}
	return 0;
}

C++: #include<iostream>
using namespace std;
bool b[21][21];
int w,h,count=0;

void dfs(int x, int y){
 b[y][x]=1;count++;
 int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
 int nx,ny;
 for(int i=0;i<4;i++){
  nx=dx[i]+x;
  ny=dy[i]+y;
  if(0<=nx&&nx<w&&0<=ny&&ny<h)
   if(b[ny][nx]==0)dfs(nx,ny);
  }
 }

int main(){
char a;
int sx,sy;
while(1){
cin>>w>>h;
count=0;
b[21][21]={};
if(w+h==0)break;
for(int i=0;i<h;i++)
for(int j=0;j<w;j++){
cin>>a;
if(a=='#')b[i][j]=1;
if(a=='@'){sx=j;sy=i;}
}
dfs(sx,sy);
cout<<count<<endl;
}
 return 0;
}

C++: #include <iostream>

using namespace std;

int W, H;
int dx[4] = {0, 1, 0, -1},
    dy[4] = {-1, 0, 1, 0};
char field[22][22];     // 1-origin

int dfs(int y, int x)
{
  if (field[y][x] == '#') {
    return 0;
  }
  
  field[y][x] = '#';
  int res = 1;
  for (int k = 0; k < 4; k++) {
    res += dfs(y + dy[k], x + dx[k]);
  }

  return res;
}

int main()
{

  while (cin >> W >> H && (W && H)) {

    // pre exec
    for (int i = 0; i <= 22; i++)
      for (int j = 0; j < 22; j++) 
        field[i][j] = '#';

    // read
    int sx = 1, sy = 1;
    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++) {
        cin >> field[i][j];
        if (field[i][j] == '@') {
          sx = j; sy = i;
          field[i][j] = '*';
        }
      }
    }

    // depth first search
    cout << dfs(sy, sx) << endl;
  }
}

C++: #include <bits/stdc++.h>

using namespace std;

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

int dfs(int x, int y, vector<vector<bool> >& Cango){
  
  int counter = 0;
  Cango[x][y] = false;
  counter++;

  for(int i = 0; i < 4; i++){
    int nextx = x + dx[i];
    int nexty = y + dy[i];
    
    if(Cango[nextx][nexty]) counter += dfs(nextx, nexty, Cango);

  }

  return counter;
}

int main(void)
{
  
  int w, h;
  int si, sj;
  while(1){
    cin >> w >> h;
    if(w == 0 && h == 0) break;

    vector<vector<bool> > Cango(h + 2, vector<bool>(w + 2, false));
    for(int i = 1 ; i <= h; i++){
      for(int j = 1; j <= w; j++){
	char input; cin >> input;
	if(input != '#') Cango[i][j] = true;
	if(input == '@'){
	  si = i;
	  sj = j;
	}
      }
    }
    int ans = dfs(si, sj, Cango);
   
    cout << ans << endl;
  }

  return 0;
    
}