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 <bits/stdc++.h>
using namespace std;
using ll = long long;

using P = pair<ll, ll>;

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

int dfs(vector<string> const& v, vector<vector<bool>>& visited, int y, int x) {
    visited[y][x] = true;
    int res = 1;
    for(int i=0; i<4; ++i) {
        int ny = y + dy[i], nx = x + dx[i];
        if(ny < 0 || v.size() <= ny || nx < 0 || v[0].size() <= nx || v[ny][nx] == '#' || visited[ny][nx]) {
            continue;
        }
        res += dfs(v, visited, ny, nx);
    }
    return res;
}


int main() {
    int w, h;
    while(cin >> w >> h, w) {
        vector<string> v(h);
        for(int i=0; i<h; ++i) {
            cin >> v[i];
        }
        int sy, sx;
        for(int i=0; i<h; ++i) {
            for(int j=0; j<w; ++j) {
                if(v[i][j] == '@') {
                    sy = i;
                    sx = j;
                }
            }
        }
        vector<vector<bool>> visited(h, vector<bool>(w));
        cout << dfs(v, visited, sy, sx) << endl;
    }
}

C++: #include<iostream>
#include<map>
#include<string>
#include<algorithm>
using namespace std;
int sum = 1;
void solve(bool a[21][21], int i, int j,int n,int m) {
	if (i + 1 < n&&a[i + 1][j]) {
		sum++;
		a[i][j] = false;
		solve(a,i+1,j, n, m);
	}
	if (i - 1 >= 0 && a[i - 1][j]) {
		sum++;
		a[i][j] = false;
		solve(a, i-1, j, n, m);
	}
	if (j + 1 < m&&a[i][j + 1]) {
		sum++;
		a[i][j] = false;
		solve(a, i, j+1, n, m);
	}
	if (j - 1 >= 0 && a[i][j - 1]) {
		sum++;
		a[i][j] = false;
		solve(a, i, j-1, n, m);
	}
}
int main() {
	int n, m;
	while (cin >> m >> n&&n != 0) {
		bool a[21][21];
		int b[2];
		for (int i = 0; i < n; i++) {
			string u;
			cin >> u;
			for (int j = 0; j < m; j++) {
				if (u[j] == '@') {
					a[i][j] = true;
					b[0] = i;
					b[1] = j;
				}
				else if (u[j] = '.')
					a[i][j] = true;
				else
					a[i][j] = false;
			}
		}
		sum = 1;
		solve(a, b[0], b[1],n,m);
		cout << sum << endl;
	}
}

C++: #include<iostream>
#include<string>
using namespace std;
int ans;
int w,h;
string map[40];
int vec[4][2]={{0,1},{0,-1},{-1,0},{1,0}};
void move(int x,int y){
	ans++;
	map[x][y]='#';
	for(int i=0;i<4;i++){
		if((x+vec[i][0]>=0)&&(y+vec[i][1]>=0)&&(x+vec[i][0]<h)&&(y+vec[i][1]<w)){
			if(map[x+vec[i][0]][y+vec[i][1]]=='.'){
				move(x+vec[i][0],y+vec[i][1]);
			}
		}
	}
	return;
}

int main(){
	while(cin>>w>>h,w){
		ans=0;
		int fx,fy;
		for(int i=0;i<h;i++){
			cin>>map[i];
		}
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(map[i][j]=='@'){
					fx=i;
					fy=j;
					break;
				}
			}
		}
		move(fx,fy);
		cout<<ans<<endl;
		
	}
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)n; i++)
#define PI 3.14159265358979323846
#define vll vector<long long>
#define vvll vector<vector<long long>>
using namespace std;
using ll = long long;
using ld = long double;

// ----本問について----
// 問題：Red and Black
// 問題文URL：https://onlinejudge.u-aizu.ac.jp/problems/1130
// 所要時間：
// 学んだこと：
// ------------------

// ----ここから本問のソースコード----
const ll dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

// グローバル変数（入力）
ll w, h, state[25][25], tiles[25][25];
pair<ll, ll> start;

// 入力
void input() {
  rep(y, h) {
    string s;
    cin >> s;
    rep(x, w) {
      if (s[x] == '.') {
        tiles[y][x] = 0;
      } else if (s[x] == '#') {
        tiles[y][x] = 1;
      } else {
        tiles[y][x] = 0;
        start = make_pair(x, y);
      }
      // cout << tiles[y][x];
      // if (x != w-1){cout << " ";}
    }
  }
}

// (x,y)にいけるかどうか
bool valid(ll x, ll y) {
  if ((0 <= x && x < w) && (0 <= y && y < h)) {
    if (tiles[y][x] == 0) { return true; }
  }
  return false;
}

// 深さ優先探索
void dfs(ll x, ll y) {
  state[y][x] = 1; // 訪問済み
  // cout << "x:" << x << " y:" << y << endl;
  rep(i, 4) {
    ll x2 = x + dx[i];
    ll y2 = y + dy[i];
    if (valid(x2, y2) && state[y2][x2]==0) { 
      dfs(x2, y2); 
    }
  }
}

void solve() {
  // state初期化
  rep(i, h) {
    rep(j, w) { state[i][j] = 0; }
  }

  // 始点から探索
  dfs(start.first, start.second);

  // 出力
  ll ans = 0;
  rep(i, h) {
    rep(j, w) {
      if (state[i][j] == 1) { ans++; }
    }
  }
  cout << ans << endl;
}

int main() {
  while (cin >> w >> h && w > 0) {
    input();
    solve();
  }
  return 0;
}