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 <vector>
#include <functional>
#include <set>
using namespace std;

int main() {
  int h, w;
  while(cin >> w >> h, w || h) {
    vector<vector<bool> > qfield(h, vector<bool>(w)); // black: true, red: true
    int starty, startx;
    for(int y = 0; y < h; ++y) for(int x = 0; x < w; ++x) {
      char c;
      cin >> c;
      qfield[y][x] = (c == '#' ? false : true);
      if(c == '@') {
        starty = y;
        startx = x;
      }
    }
    set<pair<int, int> > countedPoses;
    function<int(int, int)> tileCount = [&](int y, int x) {
      int rtn = 0;
      if(y >= 0 && y < h && x >= 0 && x < w && qfield[y][x] && countedPoses.count(make_pair(y, x)) == 0) {
        countedPoses.insert(make_pair(y, x));
        int dy[] = {0, 1, 0, -1};
        int dx[] = {1, 0, -1, 0};
        for(int i = 0; i < 4; ++i) rtn += tileCount(y + dy[i], x + dx[i]);
        ++rtn;
      }
      return rtn;
    };
    cout << tileCount(starty, startx) << endl;
  }
  return 0;
}

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

#include <iostream>
using namespace std;

int w, h, num = 0, room[20][20];
int dx[] = {-1, 1, 0, 0};
int dy[] = { 0, 0,-1, 1};

void dfs(int x, int y) {
    num++;
    room[y][x] = 0;
    
    // ??????????????¢?´¢
    for (int i = 0; i < 4; i++) {
        int mx = x + dx[i];
        int my = y + dy[i];
        
        if (mx < 0 || w <= mx) continue;
        if (my < 0 || h <= my) continue;
        if (room[my][mx] == 0) continue;
        dfs(mx, my);
    }

    return;
}

int main() {
    while (cin >> w >> h, w || h) {
        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 <stack>
using namespace std;

typedef pair<int,int> P;

int w, h, sx, sy;
char t[22][22];

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

void solve(){
  stack<P> open;
  int ans = 0;

  open.push(P(sx, sy));

  while(!open.empty()){
    P p = open.top();
    open.pop();
    ans++;

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

      if(nx < 0 || w <= nx || ny < 0 || h <= ny || t[ny][nx] != '.') continue;
      t[ny][nx] = '#';
      open.push(P(nx, ny));
    }
  }

  cout << ans - 1 << endl;
}

int main(){
  while(cin >> w >> h, w || h){
    for(int i = 0; i < h; i++){
      cin >> t[i];
      for(int j = 0; j < w; j++){
        if(t[i][j] == '@'){
          sx = j;
          sy = i;
          t[i][j] = '.';
        }
      }
    }
    solve();
  }
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <string>
#include <utility>
#include <unordered_set>

using namespace std;

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

int main(){
    int w, h;
    while(cin >> w >> h && w > 0 && h > 0){
        string tmp;
        pair<int, int> init;
        vector<vector <int> > grid(25, vector<int>(25, -1));
        vector<vector <int> > visited(25, vector<int>(25, 0));
        for(int i = 0; i < h; i++){
            cin >> tmp;
            for (int j = 0; j < w; j++){
                if(tmp[j] == '.'){
                    grid[i][j] = 0;
                }else if(tmp[j] == '#'){
                    grid[i][j] = -1;
                }else if(tmp[j] == '@'){
                    grid[i][j] = 0;
                    init = make_pair(i, j);
                }
            }
        }
        /*for(int i = 0; i < h; i++){
            for (int j = 0; j < w; j++){
                if(grid[i][j] == 0){
                    if(i == init.first && j == init.second){
                        cout << "@";
                    }else{
                        cout << ".";
                    }
                }else if(grid[i][j] == -1){
                    cout << "#";
                }
            }
            cout << endl;
        }
        */
        queue<pair<int, int> > Q;
        Q.push(init);
        visited[init.first][init.second] = 1; 
        while(!Q.empty()){
            pair<int, int> cur = Q.front(); 
            Q.pop();
            int cur_x = cur.first;
            int cur_y = cur.second;
            for(int i = 0; i < 4; i++){
                pair<int, int> dst = make_pair(cur_x + dx[i], cur_y + dy[i]);
                int x = dst.first;
                int y = dst.second;
                if(0 <= x && 0 <= y && grid[x][y] == 0 && visited[x][y] == 0){
                    Q.push(dst);
                    visited[x][y] = 1;
                }
            }
        }
        int ans = 0;
        for(int i = 0; i < h; i++){
            for (int j = 0; j < w; j++){
                ans += visited[i][j];
            }
        }
        cout << ans << endl << endl << endl;
    }
}