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 <string>
using namespace std;

int w,h;
string field[21]; // ¶ðµ¤ÌÅstring^Ìzñðg¤ÆÖÅ·B

// 4ßT(ãº¶E)ÌÆ«ÍÌæ¤ÈzñðpÓ·éÆÖ
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};

// »Ýn(x,y)
void dfs(int x, int y){
	// ¡¢éÆ±ëð'B'Éu«·¦é
	field[y][x] = 'B';

	// Ú®·é4ûüð[v
	for(int i=0 ; i < 4 ; i++ ){
		// xûüÉdx[i], yûüÉdy[i], Ú®µ½êð(mx,my)Æ·é
		int mx = x + dx[i];
		int my = y + dy[i];
		// ÍÍOÉo½Æ«ÍÌÖ
		if( mx < 0 || my < 0 || mx >= w || my >= h ) continue;
		// ¢^C'.'¾Á½çÄAÄÑoµ
		if( field[my][mx] == '.' ){
			dfs( mx , my );
		}
	}
}

int main(){
	while( cin >> w >> h , w||h ){
		for(int y=0 ; y < h ; y++ ){
			cin >> field[y];
		}

		for(int y=0 ; y < h ; y++ ){
			for(int x=0 ; x < w ; x++ ){
				if( field[y][x] == '@' ){ // l'@'¾Á½çÄAÖðÄÑo·
					dfs( x , y );
				}
			}
		}
		int ans = 0;
		// [vÅ'B'Ìð¦é(BÂ\È¢^CÌ)
		for(int y=0 ; y < h ; y++ ){
			for(int x=0 ; x < w ; x++ ){
				if( field[y][x] == 'B' )
					ans++;
			}
		}
		cout << ans << endl;
	}
}

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

using namespace std;

char graph[20][20];
int cou = 0;

void bfs(int a1,int a2){
  queue<int> Q;
  int u1,u2;
  Q.push(a1);
  Q.push(a2);

  while(!Q.empty()){

    u1 = Q.front();Q.pop();
    u2 = Q.front();Q.pop();

    if(graph[u1+1][u2] == '.'){
      graph[u1+1][u2] = 'N';
      cou++;
      Q.push(u1+1);Q.push(u2);
    }

    if(graph[u1][u2+1] == '.'){
      graph[u1][u2+1] = 'N';
      cou++;
      Q.push(u1);Q.push(u2+1);
    }

    if(graph[u1-1][u2] == '.'){
      graph[u1-1][u2] = 'N';
      cou++;
      Q.push(u1-1);Q.push(u2);
    }

    if(graph[u1][u2-1] == '.'){
      graph[u1][u2-1] = 'N';
      cou++;
      Q.push(u1);Q.push(u2-1);
    }

    /* for(int i = 0; i< 9 ;i++){
       for(int j = 0; j < 6; j++){
       cout << graph[i][j];
       }
       cout << endl;
       }
       }*/
  }
}

int main(){

  int W,H,a1,a2;

  while(1){

    cin >> W >> H;

    if(W == 0 && H == 0) break; 


    for(int i = 0; i< H ;i++){
      for(int j = 0; j < W; j++){
	cin >> graph[i][j];
	if(graph[i][j] == '@'){
	  a1 = i;
	  a2 = j;
	}
      }
    }

    /*
      for(int i = 0; i< H ;i++){
      for(int j = 0; j < W; j++){
      cout << graph[i][j];
      }
      cout << endl;
      }
    */

    bfs(a1,a2);

    cout << cou+1 << endl;
    cou = 0;
  }
  return 0;
}

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

using namespace std;

int main(){
  int w,h,count;
  queue<int> que;
  int xx[4]={1,0,-1,0};
  int yy[4]={0,1,0,-1};
  int pos;
  string tmap;
  while(true){
    cin >> w >> h;
    if(!w&&!h)
      break;
    int mmap[w+2][h+2];
    for(int x=0;x<w+2;x++){
      mmap[x][0] = -1;
      mmap[x][h+1] = -1;
    }
    for(int y=0;y<h+2;y++){
      mmap[0][y] = -1;
      mmap[w+1][y] = -1;
    }
        
    for(int y=0;y<h;y++){
      cin >> tmap;
      for(int x=0;x<w;x++){
	switch(tmap[x]){
	case '.':
	  mmap[x+1][y+1] = 0;
	  break;
	case '#':
	  mmap[x+1][y+1] = -1;	  
	  break;
	case '@':
	  mmap[x+1][y+1] = 1;
	  que.push(y*w+x);
	  break;
	}
      }
    }
    count = 1;
    while(!que.empty()){
      pos = que.front();
      for(int dir=0;dir<4;dir++){
	if (mmap[pos%w+xx[dir]+1][pos/w+yy[dir]+1]==0){
	  que.push(pos+yy[dir]*w+xx[dir]);
	  mmap[pos%w+xx[dir]+1][pos/w+yy[dir]+1] = 1;
	  count++;
	}
      }
      que.pop();
    }
    cout << count << endl;
  }
  return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;

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

int main() {
	int W, H;
	while (cin >> W >> H) {
		if (W == 0 && H == 0) return 0;
		vector<string> S(H);
		for (int i = 0; i < H; i++) cin >> S[i];
		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;
			}
		}
		queue<pair<int, int> > Q;
		Q.push({ SX, SY });
		while (!Q.empty()) {
			pair<int, int> P = Q.front();
			Q.pop();
			for (int i = 0; i < 4; i++) {
				int NX = P.first + dx[i], NY = P.second + dy[i];
				if (0 <= NX && NX < H && 0 <= NY && NY < W) {
					if (S[NX][NY] == '.') {
						S[NX][NY] = '@';
						Q.push({ NX, NY });
					}
				}
			}
		}
		int ANS = 0;
		for (int i = 0; i < H; i++) {
			for (int j = 0; j < W; j++) {
				if (S[i][j] == '@') ANS++;
			}
		}
		cout << ANS << endl;
	}
}