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 <queue>
#include <utility>
#include <array>

using namespace std;

typedef pair<int, int> pii;

int height, width;
int sx, sy;
int ans = 0;
array<int, 4> dx = {0, 0, 1, -1};
array<int, 4> dy = {1, -1, 0, 0};


void bfs(vector<vector<char>>& field)
{
    queue<pii> p;
    p.push({sx, sy});
    
    while (!p.empty()) {
        pii q = p.front(); p.pop();
        
        //??????
        int cx = q.first;
        int cy = q.second;

        for (int i = 0; i < 4; ++i) {
            int nx = cx + dx.at(i);
            int ny = cy + dy.at(i);

            if (nx >= 0 && nx < width && ny >= 0 && ny < height 
                    && field.at(ny).at(nx) == '.') {

                ans++;
                p.push({nx, ny});
            }
        }
    }

    std::cout << ans << std::endl;
}


int main()
{
    while (cin >> height >> width) {
        if (height == 0 && width == 0) break;

        ans = 0;
        
        vector<vector<char>> field(height, vector<char>(width));

        for (int y = 0; y < height; ++y) {
            for (int x = 0; x < width; ++x) {
                cin >> field.at(y).at(x);

                if (field.at(y).at(x) == '@') {
                    sx = x; sy = y;
                }
            }
        }

        bfs(field);
    }

    return 0;
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<utility>
#include<queue>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<complex>
#define INF 2147483647
#define llINF 9223372036854775807
#define pb push_back
#define mp make_pair 
#define F first
#define S second
#define ll long long

using namespace std;
int main(){
  int w,h;
  int vx[4]={1,0,-1,0};
  int vy[4]={0,-1,0,1};
  while(cin>>w>>h,h+w){
    string str[h];
    for(int i=0;i<h;i++)cin>>str[i];
    bool Map[h][w];
    for(int i=0;i<h;i++)
      for(int j=0;j<w;j++)
	Map[i][j]=false;
    queue<pair<int,int> >que;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	if(str[i][j]=='@'){
	  que.push(mp(i,j));
	  Map[i][j]=true;
	}
      }
    }
    while(!que.empty()){
      pair<int,int>now=que.front();
      que.pop();
      //cout<<now.F<<" "<<now.S<<endl;
      for(int i=0;i<4;i++){
	if(now.S+vy[i]>=0&&now.S+vy[i]<w&&now.F+vx[i]>=0&&now.F+vx[i]<h&&(!Map[now.F+vx[i]][now.S+vy[i]])&&str[now.F+vx[i]][now.S+vy[i]]=='.'){
	  Map[now.F+vx[i]][now.S+vy[i]]=true;
	  que.push(mp(now.F+vx[i],now.S+vy[i]));
	}
      }
    }
    ll ans=0;
    for(int i=0;i<h;i++)
      for(int j=0;j<w;j++)
	if(Map[i][j])
	  ans++;
    cout<<ans<<endl;
  }
  return 0;
}

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

int x,y;
char ti[30][30];
const int dx[]={1,0,-1,0}, dy[]={0,-1,0,1};
int sum=0;

bool valid(int i,int j)
{
	return(i>=0&&i<y&&j>=0&&j<x);
}

void dfs(int m,int n)
{
	sum++;
	ti[m][n]='#';
	for(int i=0;i<4;i++)
	{
		int x,y;
		x=m+dx[i];
		y=n+dy[i];
		if(valid(x,y) && ti[x][y]=='.')
		  dfs(x,y);
	}
}

int main()
{
	int m,n;
	while(cin >> x >> y)
	{
		sum=0;
		if(!x&&!y)
		  break;
		for(int i=0;i<y;i++)
		   for(int j=0;j<x;j++)
		   {
		   	    cin >> ti[i][j];
		   	    if(ti[i][j]=='@')
		   	    {
		   	    	m=i;
		   	    	n=j;
				}
		   }
		dfs(m,n);
	    cout << sum << endl;
	}
}

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

const int MAX_SIZE = 20;
typedef pair<int, int> P;

int calc(char f[][MAX_SIZE], queue<P> q, int W, int H) {
  set<P> s;
  while (q.size()) {
    P p = q.front();
    q.pop();
    s.insert(p);
    int di[] = {-1, 1, 0, 0};
    int dj[] = {0, 0, -1, 1};
    for (int k = 0; k < 4; ++k) {
      int i = p.first  + di[k];
      int j = p.second + dj[k];
      if (i >= 0 && j >= 0 && i < H && j < W && f[i][j] == '.') {
        pair<set<P>::iterator, bool> r = s.insert(P(i, j));
        if (r.second) q.push(*r.first);
      }
    }
  }
  return s.size();
}
int main() {
  int W, H;
  char f[MAX_SIZE][MAX_SIZE];
  while (true) {
    queue<P> q;
    cin >> W >> H;
    if (W == 0 && H == 0) break;
    for (int i = 0; i < H; ++i) {
      cin >> f[i];
      for (int j = 0; j < W; ++j) {
        if (f[i][j] == '@') {
          q.push(P(i, j));
        }
      }
    }
    cout << calc(f, q, W, H) << endl;
  }
}

C++: //
//  RedAndBlack.cpp
//  
//
//  Created by ??°????????? on 2015/10/30.
//
//

#include <iostream>
using namespace std;

int w, h, num = 0, room[20][20];

void dfs(int x, int y) {
    num++;
    
    room[y][x] = 0;
    
    // ??????????????¢?´¢
    if (y-1 > -1 && room[y-1][x] == 0) dfs(x, y-1);
    if (y+1 < h && room[y+1][x] == 0) dfs(x, y+1);
    if (x-1 > -1 && room[y][x-1] == 0) dfs(x-1, y);
    if (x+1 < w && room[y][x+1] == 0) dfs(x+1, y);

    return;
}

int main() {
    while (cin >> w >> h, w) {
        if (w == 0 && h == 0) break;
        
        int manX, manY;
        
        for (int i = 0; i < h; i++) {
            string s;
            cin >> s;
            for (int j = 0; j < w; j++) {
                if (s[j] == '.') {
                    room[i][j] = 1;
                } else if (s[j] == '#') {
                    room[i][j] = 0;
                } else if (s[j] == '@') {
                    room[i][j] = 1;
                    manX = j;
                    manY = i;
                }
            }
        }
        
        dfs(manX, manY);
        
        cout << num << endl;
        
        num = 0;
    }
    
    return 0;
}