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;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
//const ll mod = 1000000007;
int dh[4] = {1, 0, -1, 0};
int dw[4] = {0, 1, 0, -1};
int main() {
    //cout.precision(10);
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(true) {
        int H, W;
        cin >> W >> H;
        if(H == 0 && W == 0) break;
        string field[21];
        i_i start;
        for(int i = 1; i <= H; i++) {
            cin >> field[i];
            field[i] = "&" + field[i];
            for(int w = 1; w <= W; w++) {
                if(field[i][w] == '@') start = {i, w};
            }
        }
        queue<i_i> que;
        que.push(start);
        int ans = 0;
        while(!que.empty()) {
            i_i now = que.front();
            que.pop();
            int h = now.first;
            int w = now.second;
            //cerr << h << " " << w << endl;
            if(field[h][w] == '#') continue;
            ans++;
            field[h][w] = '#';
            for(int k = 0; k < 4; k++) {
                int newh = h + dh[k];
                int neww = w + dw[k];
                if(neww <= 0 || neww > W || newh > H || newh <= 0) continue;
                if(field[newh][neww] == '#') continue;
                que.push({newh, neww});
            }
        }
        cout << ans << endl;
    }
    return 0;
}

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

int main(void){
    int h, w;
    while (cin >> w >> h) {
        if (h * w == 0) return 0;
        
        int on_h, on_c;
        bool check[30][30] = {};
        char s[30][30];
        for (int i = 0; i < h; i++) {
            cin >> s[i];
            for (int j = 0; j < w; j++) {
                if (s[i][j] == '@') {
                    on_h = i;  on_c = j;
                }
                else if (s[i][j] == '.') check[i][j] = true;
            }
        }
        
        int ans = 1, dh[4] = {1, 0, -1, 0}, dc[4] = {0, 1, 0, -1};
        queue<int> que_h, que_c;
        que_h.push(on_h);
        que_c.push(on_c);
        
        while (!que_h.empty()) {
            int qh = que_h.front(), qc = que_c.front();
            for (int i = 0; i < 4; i++) {
                if (qh + dh[i] >= 0 && qh + dh[i] < h && qc + dc[i] >= 0 && qc + dc[i] < w 
                && check[qh + dh[i]][qc + dc[i]]) {
                    ans++;
                    check[qh + dh[i]][qc + dc[i]] = false;
                    que_h.push(qh + dh[i]);
                    que_c.push(qc + dc[i]);
                }
            }
            que_h.pop();
            que_c.pop();
        }
        
        cout << ans << endl;
    }
}

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

int w,h,ans;
char c[25][25];
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};

void dfs(int y,int x)
{
	if(c[y][x]=='#') return;
	c[y][x]='#';
	++ans;
	for (int i = 0; i < 4; ++i)
	{
		int ny=y+dy[i],nx=x+dx[i];
		if(0<=ny&&ny<h&&0<=nx&&nx<w) dfs(ny,nx);
	}
	return;
}

int main(int argc, char const *argv[])
{
	while(cin>>w>>h,w)
	{
		ans=0;
		int x,y;
		for (int i = 0; i < h; ++i)
		{
			for (int j = 0; j < w; ++j)
			{
				cin>>c[i][j];
				if(c[i][j]=='@') y=i,x=j,c[i][j]='.';
			}
		}
		dfs(y,x);
		cout<<ans<<endl;
	}
	return 0;
}

C++: #include<iostream>
#include<queue>
using namespace std;
     
const int INF=100000000;
     
char maze[20][20];
int w, h;
int sx, sy;
int cnt;
     
int d[20][20];
     
int dx[4]={1, 0, -1, 0}, dy[4]={0, 1, 0, -1};
     
     
int bfs(){
  queue< pair<int, int> > que;
     
  for(int i=0;i<h;i++) for(int j=0;j<w;j++) d[i][j]=INF;
     
  que.push(pair<int, int>(sy, sx));
  d[sy][sx]=0;
     
  while(que.size()){
    pair<int, int> p=que.front();
    que.pop();
     
    for(int i=0;i<4;i++){
      int ny=p.first+dy[i], nx=p.second+dx[i];
     
      if(0<=ny&&ny<h&&0<=nx&&nx<w&&maze[ny][nx]!='#'&&d[ny][nx]==INF){
	que.push(pair<int, int>(ny, nx));
	d[ny][nx]=d[p.first][p.second]+1;
	cnt++;
      }
    }
  }
  return cnt+1;
}
     
int main(){
     
  while(cin>>w>>h, w){
  
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	cin>>maze[i][j];
	if(maze[i][j]=='@'){
	  sy=i;
	  sx=j;
	}
      }
    }
    cnt=0;
    int res=bfs();
     
    cout<<res<<endl;

  }
     
  return 0;
}

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

int h, w = 0;
int ct = 0;
vector<string> map;

void count(int y,int x){
	map[y][x] = 'X';
	if (y != 0){
		if (map[y-1][x] == '.'){
				count(y-1,x);
		}
	}
	if (y != h-1){
		if (map[y+1][x] == '.'){
				count(y+1,x);
		}
	}
	if (x != 0){
		if (map[y][x-1] == '.'){
				count(y,x-1);
		}
	}
	if (x != w-1){
		if (map[y][x+1] == '.'){
				count(y,x+1);
		}
	}
	ct++;
}

int main(){
	int stx, sty;
	while (1){
		cin >> w >> h;
		if (h == 0 && w == 0){
			break;
		}
		for (int i = 0; i < h; i++){
			string tmp;
			cin >> tmp;
			map.push_back(tmp);
			for (int j = 0; j < w; j++){
				if (map[i][j] == '@'){
					sty = i;
					stx = j;
				}
			}
		}
		count(sty,stx);
		cout << ct << endl;
		ct = 0;
		map.clear();
	}
	return 0;
}