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>
using namespace std;

int w, h, black;
char tile[24][24];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

void dfs(int x, int y){
    black++;
    tile[x][y] = '#';
    for(int i = 0; i < 4; i++){
        if( x+dx[i] >= 0 && x+dx[i] < h &&
           y+dy[i] >= 0 && y+dy[i] < w &&
           tile[ x+dx[i] ][ y+dy[i] ] == '.' ){
            dfs( x+dx[i], y+dy[i] );
        }
    }
}

int main(){
    int i, j;
    while( cin >> w >> h){
        if(w==0&&h==0)
            break;
        for(i = 0; i < h; i++){
            for(j=0;j<w;j++){
                cin >> tile[i][j];
            }
        }
        black = 0;
        for(j = 0; j < w && !black; j++){
            for(i = 0; i < h && !black; i++){
                if( tile[i][j] == '@' ){
                    dfs(i, j);
                }
            }
        }
        cout << black << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <string>
using namespace std;
int w,h;
int dx[4] = {0,1,0,-1},dy[4] = {1,0,-1,0};
int main(void) {
	while (1) {
		int ans = 0;
		cin >> w >> h;
		vector<vector<int> > d(h,vector<int>(w,-1));
		vector<string> s(h);
		queue<pair<int,int> > q;
		if (w == 0 && h == 0) break;
		for(int i = 0;i < h;i++){
		    cin >> s[i];
		    for(int j = 0;j < w;j++){
		        if(s[i][j] == '@'){
		            d[i][j] = 0;
		            q.push(pair<int,int>(i,j));
		        }
		    }
		}
		
		while(!q.empty()){
		    pair<int,int> cur = q.front();
		    q.pop();
		    for(int k = 0;k < 4;k++){
		        int x = cur.first+dx[k];
		        int y = cur.second+dy[k];
		        if(x < 0 || x >= h || y < 0 || y >= w) continue;
		        if(s[x][y] == '#') continue;
		        if(d[x][y] == -1){
		            d[x][y] = d[cur.first][cur.second]+1;
		            ans++;
		            q.push(pair<int,int>(x,y));
		        }
		    }
		}
		cout << ans+1 << endl;
	}
	return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <fstream>
#include <set>
#include <utility>
#include <algorithm>
#include <queue>
#include <stack>
#include <deque>
#include <numeric>
#include <sstream>
#include <list>
#include <map>
#include <functional>
#include <iterator>

#define rep(i,p,n) for(int i=(int)p;i<(int)n;i++)
#define repp(i,p,n) for(int i=(int)p;i>=(int)n;i--)
#define Sort(x) sort(x.begin(),x.end())
#define PSort(x) sort(begin(x), end(x), pairCompare)
#define ll long long
#define INF 2000000000
#define pb push_back
#define mp make_pair
#define PI 3.14159265358979
int dx[4] = { -1,0,1,0 }, dy[4] = { 0,1,0,-1 };
using namespace std;

int w, h;

vector<vector<char>> v;
int ans = 0;

void dfs(int y,int x) {
	if (v[y][x] == '.') {
		v[y][x] = '#';
		ans++;
	}

	rep(i, 0, 4) {
		int nx = x + dx[i], ny = y + dy[i];
		if (0 <= nx && 0 <= ny&&nx < w&&ny < h&&v[ny][nx] == '.')dfs(ny, nx);
	}
}

int main(){
	while (1) {
		int sx, sy;
		cin >> w >> h;
		if (w == 0 && h == 0)break;
		v.resize(h, vector<char>(w));
		rep(i, 0, h) {
			rep(j, 0, w) {
				cin >> v[i][j];
				if (v[i][j] == '@') {
					sx = j;
					sy = i;
					v[i][j] = '.';
				}
			}
		}

		dfs(sy, sx);
		cout << ans << endl;

		v.clear();
		ans = 0;
	}


	

	return 0;
}

C++: # -*- coding: utf-8 -*-

count = 0
def dfs(i, j, island):
    global count
    print i, j
    dxy = [(1, 0), (0, 1), (-1, 0), (0, -1)]
    
    if i < 0 or i >= len(island) or j < 0 or j >= len(island[0]):
        return

    if island[i][j] == '#' or island[i][j] == 'x':
        return

    island[i][j] = 'x'
    count += 1
    
    for d in dxy:
        dfs(i+d[0], j+d[1], island)

def solve(w, h, island):
    s_i = 0
    s_j = 0
    for i, row in enumerate(island):
        for j, s in enumerate(row):
            if s == '@':
                s_i = i
                s_j = j
                break

    dfs(s_i, s_j, island)
    print count

def func():
    '''

    '''
    while True:
        global count
        wh = map(int, raw_input().split())
        w = wh[0]
        h = wh[1]
        island = []
        count = 0
        for i in range(h):
            row = []
            line = raw_input()
            for j in line:
                row.append(j)
            island.append(row)
        
        for row in island:
            for j in row:
                print j
            print ''

        solve(w, h, island)
    return None


func()

C++: #include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); i++)

using namespace std;

int d4x[] = {0, 1, 0, -1};
int d4y[] = {-1, 0, 1, 0};

int W, H;
int reachable(vector<string>& map, int x, int y){
  //cout << x << "," << y << endl;
  map[y][x] = '#';
  int res = 0;
  rep(i, 4){
    int sx = d4x[i] + x, sy = d4y[i] + y;
    if(0 <= sx && sx < W && 0 <= sy && sy < H){
      if(map[sy][sx] != '#'){
        res += reachable(map, sx, sy);
      }
    }
  }
  return res + 1;
}

int main(){
  while(cin >> W >> H, W != 0){
    vector<string> map(H);
    rep(i, H) cin >> map[i];
    int gy, gx;
    rep(y, H) {
      rep(x, W) {
        if (map[y][x] == '@'){
          gy = y; gx = x;
          break;
        }
      }
    }
    //cout << "OK" << endl;
    cout << reachable(map, gx, gy) << endl;
  }
}