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;

char tyle[30][30]={'0'};

int find_tyle(int i,int j){
  if(tyle[i+1][j]=='.'){
    tyle[i+1][j]='a';
    find_tyle(i+1,j);
  }
  if(tyle[i][j+1]=='.'){
    tyle[i][j+1]='a';
    find_tyle(i,j+1);
  }
  if(tyle[i-1][j]=='.'){
    tyle[i-1][j]='a';
    find_tyle(i-1,j);
  }
  if(tyle[i][j-1]=='.'){
    tyle[i][j-1]='a';
    find_tyle(i,j-1);
  }
}

int main(){
  
  int w,h;
  
  while(cin>>w>>h,w){
    int ans=0;
    for(int i=1;i<=h;++i){
      for(int j=1;j<=w;++j){
      cin>>tyle[i][j];
      }
    }
    for(int i=1;i<=h;++i){
      for(int j=1;j<=w;++j){
	if(tyle[i][j]=='@'){
	  find_tyle(i,j);
	  tyle[i][j]='a';
	}
      }
    }
    for(int i=1;i<=h;++i){
      for(int j=1;j<=w;++j){
	if(tyle[i][j]=='a'){
	  ans++;
      }
    }
  }
    cout<<ans<<endl;

   for(int i=0;i<30;++i){
      for(int j=0;j<=30;++j){
      tyle[i][j]='0';
      }
    }
    
  }
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
char maze[25][25];
bool visited[25][25];
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};
int W,H,ans;

void init()
{
	rep(i,25)
	{
		rep(j,25)
		{
			visited[i][j] = false;
		}
	}
}

void dfs(int x, int y)
{
	visited[x][y] = true;
	ans++;
	for (int i = 0; i < 4; ++i)
	{
		int x2 = x + dx[i];
		int y2 = y + dy[i];

		if (x2 >= 0 && x2 < H && y2 >= 0 && y2 < W)
		{
			if (!visited[x2][y2] && maze[x2][y2] != '#')
			{
				dfs(x2,y2);
			}
		}
	}
}

int main()
{
	while(1)
	{
		ans = 0;
		int si,sj;
		cin >> W >> H;
		if (W == 0 && H == 0) return 0;
		rep(i,H)
		{
			rep(j,W)
			{
				cin >> maze[i][j];
				if (maze[i][j] == '@')
				{
					si = i;
					sj = j;
				}
			}
		}

		init();

		dfs(si,sj);

		cout << ans << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int mod = 1e9 + 7;
const int inf = (1 << 30) - 1;
const ll infll = (1LL << 61) - 1;
#define fast() ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define digit(N) cout << fixed << setprecision((N))

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

int main()
{
    while (cin >> H >> W)
    {
        if (H == 0 && W == 0)
            return 0;
        vector<string> grid(W);

        pair<int, int> s;
        for (int i = 0; i < W; i++)
        {
            cin >> grid[i];
            for (int j = 0; j < H; j++)
            {
                if (grid[i][j] == '@')
                    s = {i, j};
            }
        }

        queue<pair<int, int>> que;
        que.push(s);

        vector<vector<int>> visited(W, vector<int>(H));

        visited[s.first][s.second] = 1;

        while (!que.empty())
        {
            auto p = que.front();
            que.pop();

            visited[p.first][p.second] = 1;
            for (int i = 0; i < 4; i++)
            {
                int nx = p.first + dx[i], ny = p.second + dy[i];
                if (nx < 0 || nx >= W)
                    continue;
                if (ny < 0 || ny >= H)
                    continue;
                if (visited[nx][ny])
                    continue;
                if (grid[nx][ny] == '.')
                    que.push({nx, ny});
            }
        }

        int ans = 0;

        for (int i = 0; i < W; i++)
        {
            for (int j = 0; j < H; j++)
            {
                ans += visited[i][j];
            }
        }

        cout << ans << endl;
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, m, n) for (int i = m; i < n; ++i)
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main() {
  while(1) {
    int W, H; cin >> W >> H;
    if(!W) break;
    vector<string> G(H);
    rep(i, 0, H) cin >> G[i];
    vector<vector<int>> visited(H,vector<int>(W));
    using P = pair<int, int>;
    queue<P> Q;
    rep(i, 0, H) rep(j, 0, W) {
      if(G[i][j] == '@') {
        Q.push(P(i, j));
        visited[i][j] = 1;
      }
    }
    int ans = 0;
    while(!Q.empty()) {
      ans++;
      P p = Q.front(); Q.pop();
      int y = p.first, x = p.second;
      rep(i, 0, 4) {
        int ny = y + dy[i], nx = x + dx[i];
        if(ny < 0 || nx < 0 || ny >= H || nx >= W) continue;
        if(visited[ny][nx]) continue;
        if(G[ny][nx] == '.') {
          visited[ny][nx] = 1;
          Q.push(P(ny, nx));
        }
      }
    }
    cout << ans << "\n";
  }
  return 0;
}

C++: #include<iostream>
#include<queue>
#include<string>
#include<map>
#define F first
#define S second
using namespace std;

char c[33][33];
int w,h;

int solve(int x, int y){
  typedef pair < int, int > P;
  queue < P > que;
  int count = 0;

  que.push( P(x, y) );
  
  while(!que.empty()){
    P q = que.front(); que.pop();
    
    int dx[] = {0, 0, 1, -1};
    int dy[] = {1, -1, 0, 0};
    
    for(int i=0;i<4;i++){
      int xx = dx[i] + q.F;
      int yy = dy[i] + q.S;

      if(xx < 0 || yy < 0 || xx == w || yy == h) continue;
      if(c[yy][xx] == '#') continue;

      c[yy][xx] = '#';
      que.push( P(xx, yy) );
      count++;
    }

  }

  return count;
}

int main(){

  int sx, sy;
  while(true){
    cin >> w >> h;
    if(!w && !h) break;

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

    while(!que.empty()) que.pop();
    cout << solve(sx, sy) << endl;
  }

}