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;

#define PI 4*atan(1)
#define INF 1e8

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

int W, H;

int ans = 0;

void bfs(int x, int y, vector<vector<int>> &M){
  M[x][y] = INF;
  ans++;

  for(int i = 0; i < 4; i++){
    for(int j = 0; j < 4; j++){
      if(dx[i] == 0 || dy[j] == 0){
        int nx = x + dx[i], ny = y + dy[j];
        if(0 <= nx && nx < H && 0 <= ny && ny < W && M[nx][ny] == -1)bfs(nx, ny, M);
      }
    }
  }
  return;
}

int main(){
  while(cin >> W >> H, W||H){
    ans = 0;
    vector<string> S(H);
    vector<vector<int>> M(H, vector<int>(W, -1));
    for(int i = 0; i < H; i++){
      cin >> S[i];
    }
    for(int i = 0; i < H; i++){
      for(int j = 0; j < W; j++){
        if(S[i][j] == '#')M[i][j] = INF;
      }
    }

    pair<int, int> start;
    for(int i = 0; i < H; i++){
      for(int j = 0; j < W; j++){
        if(S[i][j] == '@'){
          start.first = i; start.second = j;
        }
      }
    }
    bfs(start.first, start.second, M);
    cout << ans << endl;
  }
}

C++: // 現在地から四方向みて、移動先が黒いタイル.ならカウントをインクリメントしていく。

#include <bits/stdc++.h>
using namespace std;

// タイルの行数
int w;
int h;

// 現在地（初期位置）
char field[22][22];

// カウント
int cnt;

// 移動　　　右　左　上　下
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};


void dfs(int x, int y) {
  // カウント追加
  cnt++;
  // 確認済みの印として赤いタイル#に変更
  field[x][y] = '#';
  // 四方向を確認する
  for (int i = 0; i < 4; ++i) {
    // 確認する座標
    int nx = x + dx[i];
    int ny = y + dy[i];
    // 確認する座標が、タイル範囲からはみ出していない&&黒いタイル.である
    if (0 <= nx &&
        nx < w &&
        0 <= ny &&
        ny < h &&
        field[nx][ny] == '.') {
      // 確認する座標へ移動して、そこからまた四方向を確認する
      dfs(nx, ny);
    }
  }
}

int main() {
  // 入力データセットがある限り繰り返す
  while(cin >> w >> h, w) {
    cnt = 0;
    int x;
    int y;
    // 一行ずつタイルを調べて人@を探す [0][0], [1][0], [2][0]...
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> field[j][i];
        if (field[j][i] == '@') {
          x = j;
          y = i;
        }
      }
    }
    // 人@のタイルからdfs実行
    dfs(x, y);
    cout << cnt << endl;
  }
}

C++: #include <bits/stdc++.h>
using namespace std;
const int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
struct grid{
	int x,y;
};
int main(){
	while(true){
		int w,h;	cin>>w>>h;
		if(w==0)	return 0;
		vector<string> s(h);
		for(int i=0;i<h;i++)	cin>>s[i];
		queue<grid> q;
		bool visited[h][w];
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				visited[i][j]=false;
				if(s[i][j]=='@'){
					visited[i][j]=true;
					q.push((grid){j,i});
				}
			}
		}
		while(!q.empty()){
			grid c=q.front();	q.pop();
			for(int i=0;i<4;i++){
				int ny=c.y+dy[i],nx=c.x+dx[i];
				if(ny<0||h<=ny)	continue;
				if(nx<0||w<=nx)	continue;
				if(s[ny][nx]=='#')	continue;
				if(!visited[ny][nx]){
					visited[ny][nx]=true;
					q.push((grid){nx,ny});
				}
			}
		}
		int cnt=0;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(visited[i][j])	cnt++;
			}
		}
		cout<<cnt<<endl;
	}
}

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

using namespace std;

struct locate{
  int x;
  int y;
};
 
char graph[20][20];
int cou = 0;
int H,W;

void bfs(locate z){
  queue<locate> Q;
  locate u;
  locate v;
  Q.push(z);
 

  while(!Q.empty()){

    u = Q.front();Q.pop();
    if(u.y < H - 1){
      if(graph[u.y + 1][u.x] == '.' && graph[u.y + 1][u.x] != 'N'){
	graph[u.y + 1][u.x] = 'N';
	cou++;
	v.y = u.y + 1;
	v.x = u.x;
	Q.push(v);
      }
    }
    if(u.x < W - 1){
      if(graph[u.y][u.x + 1] == '.' && graph[u.y][u.x + 1] != 'N'){
	graph[u.y][u.x + 1] = 'N';
	cou++;
	v.x  = u.x + 1;
	v.y = u.y;
	Q.push(v);
      }
    }
    if(u.y > 0){
      if(graph[u.y - 1][u.x] == '.' && graph[u.y - 1][u.x] != 'N'){
	graph[u.y - 1][u.x] = 'N';
	cou++;
	v.x = u.x;
	v.y = u.y - 1;
	Q.push(v);
      }
    }
    if(u.x > 0){
      if(graph[u.y][u.x - 1] == '.' && graph[u.y][u.x - 1] != 'N'){
	graph[u.y][u.x - 1] = 'N';
	cou++;
	v.x = u.x - 1;
	v.y = u.y;
	Q.push(v);
      }
    }

    /* cout << endl;
    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;
  locate a;
  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;
      }
    */

    a.y = a1;
    a.x = a2;
    bfs(a);

    cout << cou+1 << endl;

    cou = 0;
    for(int i = 0;i < H ;i++){
      for(int j = 0;j < W; j++){
	graph[i][j] = 'N';
      }
    }
    cou = 0;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> p;

int field[22][22];
bool visited[22][22];
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int sx, sy;

int main(){
  int w, h;
  char c;
  while(cin >> w >> h, w|h){
    memset(field, 0, sizeof(field));
    memset(visited, false, sizeof(visited));
    for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++){
        cin >> c;
        if(c == '.') field[i][j] = 1;
        if(c == '@') sx = i, sy = j;
      }
    }
    queue<p> que;
    int sum = 1;
    visited[sx][sy] = true;
    que.push(p(sx, sy));
    while(!que.empty()){
      p cur = que.front(); que.pop();
      for(int i = 0; i < 4; i++){
        int nx = cur.first + dx[i];
        int ny = cur.second + dy[i];
        if(nx < 0 || nx > 20) continue;
        if(ny < 0 || ny > 20) continue;
        if(visited[nx][ny]) continue;
        if(field[nx][ny] == 0) continue;
        visited[nx][ny] = true;
        sum++;
        que.push(p(nx, ny));
      }
    }
    cout << sum << endl;
  }
  return 0;
}