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>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <queue>
#include <map>

using namespace std;


#define F first
#define S second
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define INT(a) int((a)+1e-9)
#define SUPMaxX 22
#define SUPMaxY 22

typedef pair<int,int> Pii;

int DX[4]={1,0,-1,0};
int DY[4]={0,1,0,-1};

int maxX,maxY;
char tizu[SUPMaxX][SUPMaxY];
queue<Pii> ps;

int solve(){
	int re=1;
	while(!ps.empty()){
		Pii p=ps.front();
		ps.pop();
		int px=p.F;
		int py=p.S;
		FOR(i,0,4){int qx=px+DX[i]; int qy=py+DY[i];
			if(tizu[qx][qy]=='.'){
				tizu[qx][qy]='@';
				ps.push(make_pair(qx,qy));
				re++;
			}
		}
	}
	return re;
}


int main(){
	while(true){
		cin >> maxY >> maxX;			if(!maxX) return 0;
		FOR(x,0,maxX+2) FOR(y,0,maxY+2) tizu[x][y]='#';
		FOR(x,1,maxX+1) FOR(y,1,maxY+1){
			cin>>tizu[x][y];
			if(tizu[x][y]=='@') ps.push(make_pair(x,y));
		}
		cout<<solve()<<endl;
	}
}

C++: #include <cstdio>
#include <iostream>
using namespace std;
int counta;
void judge(char d[][20], int x, int y, int limitx, int limity) {
	if (d[y][x] == '@' || d[y][x] == '.') {
		d[y][x] = '#';
		counta++;
		if (x > 0) judge(d, x - 1, y, limitx, limity);
		if (x <= limitx - 2)judge(d, x + 1, y, limitx, limity);
		if (y>0)judge(d, x, y - 1, limitx, limity);
		if (y <= limity-2)judge(d, x, y + 1, limitx, limity);
	}
}

int main() {
	int x, y;
	int manx = 0;
	int many = 0;
	do {

		counta = 0;
		char a[20][20];
		for (int i = 0; i < 20; i++) {
			for (int j = 0; j < 20; j++)
				a[i][j] = '#';
		}
		cin >> x >> y;
		if (x == 0)break;
		for (int i = 0; i < y; i++) {
			for (int j = 0; j < x; j++) {
				cin >> a[i][j];
				if (a[i][j] == '@') { many = i; manx = j; }
			}
		}
		judge(a, manx, many, x, y);
		cout << counta << "\n";

	} while (1);
}

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

using namespace std;
struct Point{
  int x;int y;
};

int main(){
  int w,h,i,j,k,c=0;
  string str;
  Point p,b;
  cin >> w >> h;
  while(w!=0&&h!=0){
    c=1;
    int dp[21][21]={{}};
    for(i=0;i<h;i++){
      cin >> str;
      for(j=0;j<w;j++){
	if(str[j]=='.') dp[j][i]= 1;
	if(str[j]=='#') dp[j][i]= 2;
	if(str[j]=='@') {
	  dp[j][i]= 3;
	  p.x=j;p.y=i;
	}
      }
    }
    
    queue <Point> q;
    q.push(p);
    while(!q.empty()){
      p = q.front();
      q.pop();
      if((p.x+1)<w&&dp[p.x+1][p.y]==1){
	c++;
	b.x=p.x+1;b.y=p.y;
	dp[p.x+1][p.y]=-1;
	q.push(b);
      }
      if((p.x-1)>=0&&dp[p.x-1][p.y]==1){
	c++;
	b.x=p.x-1;b.y=p.y;
	dp[p.x-1][p.y]=-1;
	q.push(b);
      }
      if((p.y+1)<h&&dp[p.x][p.y+1]==1){
	c++;
	b.x=p.x;b.y=p.y+1;
	dp[p.x][p.y+1]=-1;
	q.push(b);
      }
      if((p.y-1)>=0&&dp[p.x][p.y-1]==1){
	c++;
	b.x=p.x;b.y=p.y-1;
	dp[p.x][p.y-1]=-1;
	q.push(b);
      }
    }
    cout << c << endl;
    cin >> w >> h;
  }
  return 0;
}

C++: #include<iostream>
#include<stack>
using namespace std;

#define inRange(x,a,b) (a <= x && x < b)

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

int main(){
    int h, w;
    while(cin >> w >> h, w+h){
        char mat[h][w];
        int x, y;
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                cin >> mat[i][j];
                if(mat[i][j] == '@')    y = i, x = j, mat[i][j] = '.';
            }
        }

        stack<pair<int,int>> s;
        s.push({y,x});

        int ans = 0;
        while(!s.empty()){
            pair<int,int> now = s.top();    s.pop();
            if(mat[now.first][now.second] == '#')   continue;
            if(mat[now.first][now.second] == '.')   ans++;
            mat[now.first][now.second] = '#';
            for(int k = 0; k < 4; k++){
                int ny = now.first + dy[k];
                int nx = now.second + dx[k];
                if(inRange(ny, 0, h) && inRange(nx,0,w) && mat[ny][nx] == '.'){
                    s.push({ny, nx});
                }
            }
        }

        cout << ans << endl;
    }
    return 0;
}