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 <string>

using namespace std;

void room_search(int w, int h, const vector<string>& room, vector< vector<int> >& visited, int W, int H, int& ret) {
    int dw[] = {1, 0, -1, 0}, dh[] = {0, 1, 0, -1};
    visited[h][w] = 1;
    ret++;
    for (int i = 0; i < 4; i++) {
        int nw = w + dw[i], nh = h + dh[i];
        if (0 <= nw && nw < W && 0 <= nh && nh < H && visited[nh][nw] != 1 && room[nh][nw] == '.')  {
            room_search(nw, nh, room, visited, W, H, ret);
        }
    }
}

int main() {
    while(true) {
        int W, H;
        cin >> W >> H;
        if(W == 0 && H == 0) break;
        vector<string> room;
        vector< vector<int> > visited;
        int iw, ih;
        for(int h = 0; h < H; h++) {
            string s;
            cin >> s;
            for(int w = 0; w < W; w++) {
                if(s[w] == '@') {
                    iw = w;
                    ih = h;
                }
            }
            room.push_back(s);
            visited.push_back(vector<int>(W, 0));
        }
        int ret = 0;
        room_search(iw, ih, room, visited, W, H, ret);
        cout << ret << endl;
    }
}
// [Problem] Red and Black
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1130&lang=jp

C++: #include <iostream>

int H, W;
int map[20][20];
int c;
using namespace std;

void printArray();
void check(int, int);

int main() {
  while (true) {
    cin  >> W >> H;
    if ((W | H) == 0) {
      break;
    }
    int sy, sx;
    for (int i = 0; i < H; i++) {
      string line;
      cin >> line;
      for (int j = 0; j < W; j++) {
        if (line[j] == '@') {
          sy = i;
          sx = j;
          map[i][j] = 0;
        } else if (line[j] == '.') {
          map[i][j] = 0;
        } else if (line[j] == '#') {
          map[i][j] = 1;
        }
      }
    }
    // printArray();
    c = 0;
    check(sy, sx);
    // printArray();
    cout << c << endl;
  }
  return 0;
}

void check(int y, int x) {
  if (y < 0 || x < 0 || y >= H || x >= W || map[y][x] == 1) {
    return;
  }
  map[y][x] = 1;
  c++;
  check(y + 1, x);
  check(y - 1, x);
  check(y, x + 1);
  check(y, x - 1);
}

void printArray() {
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cout << map[i][j];
    }
    cout << endl;
  }
}

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

int main(){
	int dx[4]={0,1,0,-1};
    int dy[4]={1,0,-1,0};

	int w,h;
	while(cin>>w>>h and w>0){
		queue<pair<int,int>> q;
	    int sy,sx;
		char c[h][w]; int d[h][w];
		rep(i,h){
	        rep(j,w){
	            cin>>c[i][j]; d[i][j]=INF;
				if(c[i][j]=='@'){
					sy=i; sx=j;
				}
	        }
	    }
		d[sy][sx]=0;
	    q.push(make_pair(sy,sx));

		int ans=1;
		while(q.size()){
	        pair<int,int> p=q.front(); //キューの先頭を取り出す
	        q.pop(); //取り出したら削除
	        int prev=d[p.first][p.second]; //一手前の手数prev
	        rep(i,4){ //4方向探索
	            int cy=p.first+dy[i];
	            int cx=p.second+dx[i];
	            if(c[cy][cx]=='.' and d[cy][cx]==INF){ //通行可能かつ未到達
					if(0<=cy and cy<h){
						if(0<=cx and cx<w){
							d[cy][cx]=prev+1;
			                q.push(make_pair(cy,cx));
							ans++;
						}
					}
	            }
	        }
	    }
		cout<<ans<<endl;
	}
    return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;
typedef struct P{
  int first;
  int second;
} PAIR;

int main(void){
  int w, h, sx, sy, x, y, i,ans;
  char mp[22][22];
  bool visited[22][22];
  queue<PAIR> que;
  int dx[4] = {0, 1, 0, -1};
  int dy[4] = {1, 0, -1, 0};
  PAIR next, tmp;

  while(1){
    cin >> w >> h;
    if(w == 0) break;
    for(y=0;y<h+2;y++){
      if(y == 0 || y == h + 1){
        for(x=0;x<w+2;x++) mp[y][x] = '#';
      }else{
        for(x=0;x<w+2;x++){
          if(x == 0 || x == w + 1){
            mp[y][x] = '#';
          }else{
            cin >> mp[y][x];
          }
        }
      }
    }
    for(y=0;y<h+2;y++){
      for(x=0;x<w+2;x++){
        if(mp[y][x] == '@'){
          sx = x;
          sy = y;
        }
        visited[y][x] = false;
      }
    }
    que.push(PAIR{sx, sy});

    visited[sy][sx] = true;
    ans = 1;
    
    while(!que.empty()){
      next = que.front();
      que.pop();
      for(i=0;i<4;i++){
        if((!visited[next.second+dy[i]][next.first+dx[i]]) && (mp[next.second+dy[i]][next.first+dx[i]] == '.')){
          ans++;
          visited[next.second+dy[i]][next.first+dx[i]] = true;
          que.push(PAIR{next.first+dx[i], next.second+dy[i]});
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

C++: //PKU 1979
#include<iostream>
#include<cstdio>
char mat[21][21];
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
int w,h;
using namespace std;

void check(int x,int y){
	int i;
	int mx,my;
	for(i=0;i<4;i++){
		mx=x+dx[i];
		my=y+dy[i];
		if(mx<w && my<h && mx>=0 && my>=0){
			if(mat[my][mx]=='.'){
				mat[my][mx]='z';
				check(mx,my);
			}
		}
	}
	return;
}

int main(void){
	int i,j;
	int mx,my;

	while(cin>>w>>h,(w||h)){
		for(i=0;i<21;i++)for(j=0;j<21;j++)mat[i][j]=0;
		for(i=0;i<h;i++){
			for(j=0;j<w;j++){
				cin>>mat[i][j];
			}
		}
		for(i=0;i<h;i++){
			for(j=0;j<w;j++){
				if(mat[i][j]=='@'){
					mx=j,my=i;
					mat[my][mx]='z';
				}
			}
		}
		check(mx,my);//4ツ陛サツ古シツづ個づづェツつゥツ行ツつュ

		int cnt=0;
		for(i=0;i<h;i++){
			for(j=0;j<w;j++){
				if(mat[i][j]=='z'){
					cnt++;
				}
			}
		}
		cout<<cnt<<endl;
	}

	return 0;
}