Go: package main

import (
	"bufio"
	"encoding/json"
	"fmt"
	"os"
	"strconv"
	"strings"
)

var (
	sc = bufio.NewScanner(os.Stdin)
)

type Color int

const (
	ColorBlack Color = iota
	ColorRed
)

var strByColor = map[Color]string{
	ColorBlack: "black",
	ColorRed:   "red",
}

func (c Color) String() string {
	return strByColor[c]
}

type Node struct {
	ID      int
	X, Y    int
	Color   Color
	NextIDs map[int]struct{}
}

func (n *Node) String() string {
	d, _ := json.Marshal(n)
	return string(d)
}

type Map struct {
	Width, Height int
	Nodes         map[int]*Node
}

func (m *Map) GetByID(id int) *Node {
	return m.Nodes[id]
}

func (m *Map) Get(x, y int) *Node {
	return m.Nodes[m.getID(x, y)]
}

func (m *Map) SetColor(x, y int, c Color) {
	m.Nodes[m.getID(x, y)].Color = c
}

func (m *Map) getID(x, y int) int {
	return x + y*m.Width
}

func (m *Map) init() {
	for x := 1; x <= m.Width; x++ {
		for y := 1; y <= m.Height; y++ {
			id := m.getID(x, y)
			m.Nodes[id] = &Node{ID: id, X: x, Y: y}
		}
	}
	for x := 1; x <= m.Width; x++ {
		for y := 1; y <= m.Height; y++ {
			nextIDs := map[int]struct{}{}
			if x > 1 {
				nextIDs[m.getID(x-1, y)] = struct{}{}
			}
			if x < m.Width {
				nextIDs[m.getID(x+1, y)] = struct{}{}
			}
			if y > 1 {
				nextIDs[m.getID(x, y-1)] = struct{}{}
			}
			if y < m.Height {
				nextIDs[m.getID(x, y+1)] = struct{}{}
			}
			m.Get(x, y).NextIDs = nextIDs
		}
	}
}

func NewMap(w, h int) *Map {
	m := &Map{Width: w, Height: h, Nodes: map[int]*Node{}}
	m.init()
	return m
}

type Searcher struct {
	Map        *Map
	queue      []*Node
	touchedIDs map[int]struct{}
	visitedIDs map[int]struct{}
}

func NewSearcher(m *Map) *Searcher {
	return &Searcher{
		Map:        m,
		queue:      []*Node{},
		touchedIDs: map[int]struct{}{},
		visitedIDs: map[int]struct{}{},
	}
}

func (s *Searcher) Search(start *Node) {
	s.enqueue(start)

	for {
		if len(s.queue) == 0 {
			break
		}
		for nextID := range s.dequeue().NextIDs {
			n := s.Map.GetByID(nextID)
			if n.Color == ColorRed || s.hasTouched(n) {
				continue
			}
			s.enqueue(n)
		}
	}
}

func (s *Searcher) VisitedNodeCount() int {
	return len(s.visitedIDs)
}

func (s *Searcher) hasTouched(n *Node) bool {
	_, ok := s.touchedIDs[n.ID]
	return ok
}

func (s *Searcher) enqueue(n *Node) {
	s.queue = append(s.queue, n)
	s.touchedIDs[n.ID] = struct{}{}
}

func (s *Searcher) dequeue() *Node {
	n := s.queue[0]
	s.queue = s.queue[1:]
	s.visitedIDs[n.ID] = struct{}{}
	return n
}

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])

		m := NewMap(w, h)

		// Set colors
		var start *Node
		for y := 1; y <= h; y++ {
			sc.Scan()
			for i, c := range sc.Text() {
				switch c {
				case '.', '@':
					m.SetColor(i+1, y, ColorBlack)
					if c == '@' {
						start = m.Get(i+1, y)
					}
				case '#':
					m.SetColor(i+1, y, ColorRed)
				}
			}
		}

		// Search
		s := NewSearcher(m)
		s.Search(start)
		fmt.Println(s.VisitedNodeCount())
	}
}

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;
}