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 <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <functional>
using namespace std;
#define MOD 1000000007
#define ADD(X,Y) ((X) = ((X) + (Y)%MOD) % MOD)
typedef long long i64; typedef vector<int> ivec; typedef vector<string> svec; typedef pair<int, int> pi;
int dd[4] = { 0, 1, 0, -1 };


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

		char C[30][30];
		int B[30][30];
		for (int i = 0; i < H; i++)
		{
			scanf("%s", C[i]);
		}
		int sx = -1; int sy = -1;
		for (int i = 0; i < H; i++)
		{
			for (int j = 0; j < W; j++)
			{
				B[i][j] = 0;
				if (C[i][j] == '@')
				{
					sy = i;
					sx = j;
				}
			}
		}

		queue<pi> Q;
		Q.push(pi(sy, sx));
		B[sy][sx] = 1;
		int ans = 1;
		while (!Q.empty())
		{
			int ty = Q.front().first; int tx = Q.front().second;
			Q.pop();

			for (int r = 0; r < 4; r++)
			{
				int y = ty + dd[r];
				int x = tx + dd[r + 1];
				if (y < 0 || y >= H || x < 0 || x >= W)continue;
				if (C[y][x] == '#')continue;
				if (B[y][x])continue;
				ans++;
				B[y][x] = 1;
				Q.push(pi(y, x));
			}
		}

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

C++: #include<iostream>
#include<stdlib.h>

using namespace std;

char map[100][100];
char smap[100][100];
int ans;

int serch(int nx,int ny,int maxx,int maxy)
{
	int nextx[4]={1,0,-1,0},nexty[4]={0,1,0,-1};
	map[ny][nx]='0';
	for(int i=0;i<4;i++)
		if(0<=nx+nextx[i] && nx+nextx[i]<maxx && 0<=ny+nexty[i] && ny+nexty[i]<maxy &&map[ny+nexty[i]][nx+nextx[i]]=='.')
				serch(nx+nextx[i],ny+nexty[i],maxx,maxy);
	return ans++;
}

int main()
{
	int x,y;
	int i,j;
	int sx,sy;
	
	for(;;)
	{
		cin >> x >> y ;
		
		if(x==0 && y==0)
			break;
		
		for(i=0;i<y;i++)
		{
			for(j=0;j<x;j++)
			{
				cin >> map[i][j];
				smap[i][j]=map[i][j];
				if(map[i][j]=='@')
				{
					sx=j;
					sy=i;
				}
			}
		}
		
		ans=0;
		serch(sx,sy,x,y);
		
		cout << ans << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
const int inf = 1001001001;


int w, h;
int a[20][20];
int sw, sh;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int D[20][20];
int Ans = 1;


int main()
{
    while(cin >> w, w != 0)
    {
        Ans = 1;
        cin >> h;
        
        for(int i = 0; i < 20; i++)
        {
            for(int j = 0; j < 20; j++)
            {
                D[i][j] = inf;
            }
        }
        
        for(int i = 0; i < h; i++)
        {
            for(int j = 0; j < w; j++)
            {
                char x;
                cin >> x;
                if(x == '.')
                {
                    a[j][i] = 1;
                }
                else if(x == '#')
                {
                    a[j][i] = 0;
                }
                else
                {
                    sw = j;
                    sh = i;
                    a[j][i] = 100;  //  ????????§?????????
                }
            }
        }
        queue< pair<int, int> > Q;
        pair<int, int> S;
        S = make_pair(sw, sh);
        Q.push(S);
        D[sw][sh] = 0;
        int k = 0;
        while(! Q.empty())
        {
            pair<int, int> cur = Q.front();
            Q.pop();
            int x = cur.first;
            int y = cur.second;
            for(int i = 0; i < 4; i++)
            {
                if(a[x+dx[i]][y+dy[i]] == 1 && 0 <= x+dx[i] && x+dx[i] < w && 0 <= y+dy[i] && y+dy[i] < h && D[x+dx[i]][y+dy[i]] == inf)    //  ????????????????¢????????????????????????????????????°???
                {
                    D[x+dx[i]][y+dy[i]] = D[x][y] + 1;
                    Ans = Ans + 1;
                    pair<int, int> T;
                    T = make_pair(x+dx[i], y+dy[i]);
                    Q.push(T);
                }
            }
        }
        cout << Ans << endl;
    }
}

C++: #if 0
08-162015?????????????????°???????????????????????????
pair???
#endif

#include <iostream>
#include <queue>
#include <string.h>
#include <utility>
using namespace std;

int w,h;
int tot=1; //?????£????????????(?????????????????????)
const int dx[]={1,0,-1,0}, dy[]={0,-1,0,1}; //??????????§??????¨
char P[100][100]; //??§?¨?????????????????????¨???

bool valid(int x,int y){ //??????????????????
	return (x>=0)&&(x<w)&&(y>=0)&&(y<h)&&(P[x][y]!='#');
}

void bfs(int x,int y){ //???????????¢?´¢
	queue<pair<int,int> > q;
	pair<int,int> p;
	p = make_pair(x,y); //pair??????????????¨??????????????????
	q.push(p);
	while(!q.empty()){
		pair<int,int> cur=q.front(); //?????¨??°?????????????????????
		q.pop();
		for(int i=0;i<4;++i){
			if(valid(cur.first+dx[i],cur.second+dy[i])){
				tot+=1;
				q.push(make_pair(cur.first+dx[i],cur.second+dy[i]));
				P[cur.first+dx[i]][cur.second+dy[i]]='#'; //???????????£?????´???????°???????
			}
		}
		P[cur.first][cur.second]='#';
	}
}

int main(){
	int x1,y1;
	while((cin>>w>>h)&&(w!=0&&h!=0)){
		for(int j=0;j<h;++j){
			for(int i=0;i<w;++i){
				cin>>P[i][j];
				if(P[i][j]=='@')
					x1=i; y1=j; 
			}
		}
		bfs(x1,y1); //??????????????°????????£??\????????¢?´¢????§?
	}
	cout << tot <<endl;
}