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++: //
//  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] == 1) dfs(x, y-1);
    if (y+1 < h && room[y-1][x] == 1) dfs(x, y+1);
    if (x-1 > -1 && room[y-1][x] == 1) dfs(x-1, y);
    if (x+1 < w && room[y-1][x] == 1) dfs(x+1, y);

    return;
}

int main() {
    while (cin >> w >> h) {
        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;
}

C++: 
#include <iostream>
#include <algorithm>
#include <functional>
#include<vector>
#include<math.h>
#include<bitset>
#include<string>
#include <deque>
#include<queue>
#include<map>
#include<type_traits>
using namespace std;
char D[28][28];
int Y = 0;
void serch(int X, int Q) {
	if (D[X][Q] == '.'||D[X][Q]=='@') {
		Y++;
		D[X][Q] = '#';
		serch(X - 1, Q);
		serch(X + 1, Q);
		serch(X , Q-1);
		serch(X , Q+1);
		

	}
	else return;
}


int main() {
	int H, W;
	cin >> W >> H;
	for (int i = 0; i <28; i++) {
		for (int j = 0; j <28; j++) {
			D[i][j] = '#';
		}
	}
	int Sx,Sy;
	while (H) {
		 Y = 0;
		for (int i = 1; i <= H; i++) {
			for (int j = 1; j <= W; j++) {
				cin >> D[j][i];
				if (D[j][i] == '@') {
					Sx = j;
					Sy = i;
				}
			}
		}
		serch(Sx, Sy);
		
		cout <<"Y"<< Y << endl;
		for (int i = 0; i <28; i++) {
			for (int j = 0; j <28; j++) {
				D[i][j] = '#';
			}
		}
		cin >> W >> H;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <math.h>

#define REP(i, n) for (int (i) = 0; (i) < (n); (i)++)

using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
template<class T> using hset = unordered_set<T>;
template<class Key, class T> using hmap = unordered_map<Key, T>;

int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
bool isOutOfBounds(int y, int x, int h, int w) {
    return y < 0 || y >= h || x < 0 || x >= w;
}

int main() {
    int W, H;
    vs room;

    int sy, sx;
    while (cin >> W >> H) {
        if (W == 0) break;

        room.resize(H);
        REP(y, H) {
            cin >> room[y];
            REP(x, W) {
                if (room[y][x] == '@') { sy = y; sx = x; room[y][x] = '#'; }
            }
        }

        queue<int> que;
        que.push(sy);
        que.push(sx);
        int y, x, cnt=1;
        while (!que.empty()) {
            y = que.front(); que.pop();
            x = que.front(); que.pop();

            for (int i=0; i<4; i++) {
                if (isOutOfBounds(y+dy[i], x+dx[i], H, W)) continue;
                if (room[y+dy[i]][x+dx[i]] == '#') continue;

                cnt++;
                room[y+dy[i]][x+dx[i]] = '#';
                que.push(y+dy[i]);
                que.push(x+dx[i]);
            }
        }

        cout << cnt << endl;
    }

    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <functional>
#include <string>
#include <queue>
#include <vector>
#include <cstring>

using std::cin;
using std::cout;
using std::cerr;
using std::endl;

const int vx[4] = { 0, 1, 0, -1 }, vy[4] = { 1, 0, -1, 0 };

int W, H;
std::string s[21];
bool is_searched[21][21];

int solve() {
  int sx, sy;

  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      if (s[i][j] == '@') {
        sx = i, sy = j;
        goto L;
      }
    }
  }
L:

  std::memset(is_searched, false, sizeof is_searched);

  int ans = 1;

  std::queue<std::pair<int, int> > que;
  que.push({ sx, sy });
  is_searched[sx][sy] = true;

  while (!que.empty()) {
    std::pair<int, int> p = que.front();
    que.pop();

    for (int i = 0; i < 4; ++i) {
      int nx = p.first + vx[i], ny = p.second + vy[i];

      if (0 <= nx && nx < H && 0 <= ny && ny < W && !is_searched[nx][ny] && s[nx][ny] == '.') {
        ans++;
        is_searched[nx][ny] = true;
        que.push({ nx, ny });
      }
    }
  }

  return ans;
}

int main() {
  while (cin >> W >> H, H | W) {
    for (int i = 0; i < H; ++i) {
      cin >> s[i];
    }

    int ans = solve();

    cout << ans << endl;
  }

  return 0;
}

C++: #include <iostream>
#include <queue>

using namespace std;
typedef pair<int, int> P;

char t[20][20];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int ans,h,w;
queue<P> q;

int bfs(int x, int y){

  q.push( P(x, y) );
  
  while( !q.empty() ){
    P now = q.front();
    q.pop();
    for( int i = 0 ; i < 4 ; i++ ){
      int nx = now.first + dx[i];
      int ny = now.second + dy[i];
      if( 0 <= nx && nx < w && 0 <= ny && ny <= h 
	  && t[ny][nx] == '.' ){
	t[ny][nx] = '#';
	q.push( P(nx, ny) );
	ans++;
      }
    }
  }
  return ans;
}

int main(void){

  int x,y;
  while( cin >> w >> h, h + w ){

    memset( t, 0, sizeof(t) );
    ans = 1;

    for( int i = 0 ; i < h ; i++ ){
      for( int j = 0 ; j < w ; j++ ){
	cin >> t[i][j];
	if( t[i][j] == '@' ){
	  x = j;
	  y = i;
	}
      }
    }
    cout << bfs(x,y) << endl;
  }
  return 0;
}