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

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

int main(){
    while(true){
        int w, h; cin >> w >> h;
        if(w == 0 && h == 0) break;
        int sx, sy; //スタート地点の場所
        vector<vector<char>> mp(h, vector<char>(w)); //入力を受け取る二次元配列
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                cin >> mp[i][j];
                if(mp[i][j] == '@'){
                    sx = j;
                    sy = i;
                }
            }
        }
        vector<vector<int>> ok(h, vector<int>(w)); //到達可能なら1を代入する
        queue<pair<int, int>> que;
        que.push({sx, sy});
        ok[sy][sx] = 1;
        while(!que.empty()){
            int x = que.front().first, y = que.front().second; //現在地を取得する
            que.pop();
            for(int i = 0; i < 4; i++){
                if(x + dx[i] < 0 || x + dx[i] >= w || y + dy[i] < 0 || y + dy[i] >= h) continue; //mapの範囲外はcontinue
                if(mp[y+dy[i]][x+dx[i]] == '#' || ok[y+dy[i]][x+dx[i]] == 1) continue; //障害物で行けないor一度行ったところはスルー
                //それ以外の場合
                ok[y+dy[i]][x+dx[i]] = 1; //okに1を代入する
                que.push({x+dx[i], y+dy[i]}); //現在地からまだ行ってない場所のうち,行ける場所を新たに追加
            }
        }
        int ans = 0; 
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){ //okの要素を全て足すと行ける場所の個数がわかる
                ans += ok[i][j];
            }
        }
        cout << ans << endl;
    }
}

C++: #include<iostream>
#define MAX (256)
using namespace std;

int N, M;
char field[MAX][MAX];
int sx, sy;
int ans = 0;

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

void dfs (int x, int y){
	if(0 <= x && x < N && 0 <= y && y < M && field[x][y] == '.'){
		field[x][y] = '#';
		ans++;
		
		for(int i =0; i < 4; i++){
			int nx = x + dx[i], ny = y + dy[i];
			dfs(nx, ny);
		
		}
	}
}

int main(){

	while(1){
	ans = 0;
	cin >> M >> N;
	
	if(M == 0 && N == 0) break;
	
	for(int i =0; i < N; i++){
		for(int j =0; j < M; j++){
			cin >> field[i][j];
		}
	}
	
	for(int i =0; i < N; i++){
		for(int j =0; j < M; j++){
			if(field[i][j] == '@'){
				sx = i;
				sy = j;
				field[sx][sy] = '.';
			}
		}
	}
		
	dfs(sx, sy);
	
	cout << ans << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
#include<stdio.h>
#include<queue>
#include <climits>
#include <map>
#include <set>
const long long mod = 1e9 + 7;
const int INF = 1 << 20;
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

char f[30][30];
int w,h;
int dx[4] = {-1,0,0,1};
int dy[4] = {0,-1,1,0};
int ans;

void dfs(int x,int y)
{
    f[y][x] = '#';
    ans++;
    for(int i = 0; i < 4; i++){
        int ny = y+dy[i];
        int nx = x+dx[i];
        if(ny >= 0 && ny < h && nx >= 0 && nx < w && f[ny][nx] == '.'){
            dfs(nx,ny);
        }
    }
    return ;
    
}

int main()
{
    while(true){
        cin >> w >> h;
        ans = 0;
        if(w == 0 && h == 0) break;
        int sx,sy;
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                cin >> f[i][j];
                if(f[i][j] == '@'){
                    sx = j;
                    sy = i;
                }
            }
        }
        dfs(sx,sy);
        cout << ans << endl;
    }
}

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

using namespace std;

char graph[20][20];
int cou = 0;
int W,H;

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< H ;i++){
      for(int j = 0; j < W; j++){
	cout << graph[i][j];
      }
      cout << endl;
      }*/
  }
}


int main(){

  int 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;
    for(int i = 0;i < H ;i++){
      for(int j = 0;j < W; j++){
	graph[i][j] = 0;
      }
    }

  }
  return 0;
}

C++: # include <iostream>
# include <algorithm>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <utility>
# include <stack>
# include <queue>
# include <list>
# include <unordered_map>
constexpr int MOD = 1000000000 + 7;
constexpr int INF = 2000000000;
using namespace std;
using LL = long long;

char maze[21][21];
bool ok[21][21];
int h, w;
int sx, sy;

void dfs(int hpos, int wpos) {
	ok[hpos][wpos] = true;
	maze[hpos][wpos] = '#';
	int dx[4] = { 0,1,0,-1 }, dy[4] = { -1,0,1,0 };
	for (int i = 0; i < 4; i++) {
		int nx = wpos + dx[i], ny = hpos + dy[i];
		if (0 <= nx&&nx < w && 0 <= ny&&ny < h&&maze[ny][nx] == '.')dfs(ny, nx);
	}
}

int main() {
	while (cin >> w >> h&&w&&h) {
		for (int i = 0; i < 20; i++) {
			for (int j = 0; j < 20; j++) {
				ok[i][j] = false;
			}
		}
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				cin >> maze[i][j];
				if (maze[i][j] == '@')sx = j, sy = i;
			}
		}
		dfs(sy, sx);
		int ans = 0;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (ok[i][j])ans++;
			}
		}
		cout << ans << endl;
	}
}