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;

#ifdef LOCAL
#include "../dump.hpp"
#else
#define dump(...)
#endif

#define rep(i,n) for(ll i=0,i##_cond=(n);i<i##_cond;++i)
#define FOR(i,a,b) for(ll i=(a),i##_cond=(b);i<i##_cond;++i)
#define ROF(i,a,b) for(ll i=(a)-1,i##_cond=(b);i>=i##_cond;--i)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend() //sortで大きい順
#define UNIQUE(v) v.erase(unique(all(v)),v.end())
#define SUM(a) accumulate(all(a),0)
#define sz(x) ((ll)(x).size())
#define pb push_back
#define fst first
#define snd second
#define mp make_pair

typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<ll,ll> pii;

const ll inf = 1ll<<62;
const ll mod = 1e9+7;

#define int ll

vi dx = {1, 0, -1, 0, 1, -1, -1, 1}, dy = {0, 1, 0, -1, 1, 1, -1, -1};

main()
{
  while(1){
    int w,h; cin >> w >> h;
    if(w == 0) break;

    vs maze(h); rep(i,h) cin >> maze[i];
    vvi look(h,vi(w));
    int sx, sy;
    rep(i,h) rep(j,w) if(maze[i][j] == '@'){
      sx = i; sy = j;
    }
    
    queue<pii> q;

    q.push(pii(sx,sy));
    int ans = 1;
    look[sx][sy] = 1;

    while(not q.empty()){
      pii p = q.front();
      q.pop();

      //if(p.fst == gx and p.snd == gy) break;

      rep(i, 4){
	int nx = p.fst + dx[i], ny = p.snd + dy[i];
	if(0 <= nx and nx < h and 0 <= ny and ny < w and maze[nx][ny] != '#' and look[nx][ny] == 0){
	  q.push(pii(nx, ny));
	  look[nx][ny] = 1;
	  ans++;
	}
      }
    }
    cout << ans << endl;
  }
}

C++: #include<iostream>
#include<queue>
using namespace std;
struct PAD{
    char M;
    int x;
    int y;
    int flag = 0; 
}; 
int main(){
    queue<struct PAD> que;
    struct PAD pad[20][20+1];
    int W,H,count;
    int start_x,start_y;
    char baf_char;
    int dx[4] = {1,0,-1,0};
    int dy[4] = {0,1,0,-1};
    while(1){
        count = 0;
        cin >> W >> H;
        if(W==0&&H==0)break;
        for(int i=0;i<H;i++){
            for(int k=0;k<W;k++){
                cin >> baf_char;
                if(baf_char=='@'){
                    start_x = k;
                    start_y = i;
                    pad[i][k].flag = 1;
                }else if(baf_char=='#'){
                    pad[i][k].flag = 1;
                }else{
                    pad[i][k].flag = 0;
                }
                pad[i][k].x=k;
                pad[i][k].y=i;
                pad[i][k].M = baf_char;
            }
        }
        //pad[start_y][start_x].flag = 1;
        que.push(pad[start_y][start_x]);
        while(que.size()!=0){
            struct PAD p = que.front();
            que.pop();
            for(int j = 0;j<4;j++){
                int nx = p.x + dx[j];
                int ny = p.y + dy[j];
                if(ny>=0&&nx>=0&&ny<=(H-1) && nx<=(W-1) && pad[ny][nx].flag == 0 && pad[ny][nx].M == '.'){
                    pad[ny][nx].flag = 1;
                    que.push(pad[ny][nx]);
                    count++;
                }
            }
        }
        cout << count+1 << endl;
    }


    return 0;
}

C++: #include <iostream>

using namespace std;

int W, H;
char map[25][25];
int sx, sy;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

int dfs(int x, int y)
{
  int ret = 1;
  map[x][y] = '#';
  for(int i = 0; i < 4; i++){
    int nx = x + dx[i], ny = y + dy[i];
    if(nx < 0 || nx >= W || ny < 0 || ny >= H) continue;
    if(map[nx][ny] == '#') continue;
    ret += dfs(nx, ny);
  }
  return ret;
}

int main(void)
{
  ios::sync_with_stdio(false);
  cin.tie(0);

  while(1){
    cin >> W >> H;
    if(W == 0 && H == 0) break;
    for(int y = 0; y < H; y++){
      for(int x = 0; x < W; x++){
        cin >> map[x][y];
        if(map[x][y] == '@'){
          sx = x, sy = y;
          map[x][y] = '.';
        }
      }
    }
    cout << dfs(sx, sy) << endl;
  }
  return 0;
}

C++: //??¨??¢?´¢:??±???????????¢?´¢??????????????¢?´¢ 

//??????????????¢?????¬?????????
 
//?????°??¢??°???????????????(?????????)????????\???(?????????)
#include <stdio.h>
#include <queue>
#include <string.h>
using namespace std;	
int W,H;
char c[101][101];
bool visited[100][100];
 
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
 	
void compute();//????????????????????£?¨? 
	
int main(){
	
	while(scanf("%d %d",&W,&H),W |H){
	    int i;
		
		for(i=0;i<H;i++){
			scanf(" %s",&c[i][0]);
		}
	}
	return 0;
}

void compute(){
	
	memset(visited,false,sizeof visited);  //{????????????????????????1bit??????????????????1bit????????§?????????} 
	
	queue<int> que;
	int i,j;
	for(j=0;j<W;j++){ 
	 for(i=0;i<H;i++){
	  if(c[i][j]=='@'){
	  	que.push(j+i*H);
	  	visited[i][j]=true;
	  	 break;
	  }
	 }
	}
  int answer=1;
  while(!que.empty()){
	int cur=que.front();
	        que.pop();
	        int x=cur%W;
	        int y=cur/W;
    

  for(i=0;i<4;i++){
	int nx=x+dx[i],ny=y+dy[j];
	
  
  
  if(!(0<=nx&&nx<W&&0<=ny))continue;
  if(c[ny][nx]=='#'||visited[ny][nx])continue;

  que.push(nx+ny*W);
  visited[ny][nx]=true;

    }
   }
 
 }

C++: #include <iostream>
#include <cstring>
using namespace std;
#define MAX 21
int w,h;
char map[MAX][MAX];
int dx[]={ 0, 1, 0,-1};
int dy[]={ 1, 0,-1, 0};
int count;
void search(int y,int x){
	//cout<<"now:("<<x<<","<<y<<")"<<endl;
	map[y][x]='#';
	for(int i=0;i<4;i++){
		int nextX=x+dx[i];
		int nextY=y+dy[i];
		//cout<<"next:("<<nextX<<","<<nextY<<")"<<endl;
		if(nextY<0||nextX<0||nextY>=h||nextX>=w || map[nextY][nextX]=='#')continue;
		++count;
		search(nextY,nextX);
	}
	return;
}
int main(){
	while(cin>>w>>h && (w||h)){
		int st_w,st_h;
		memset(map,' ',sizeof(map));
		for(int i=0;i<h;i++){
			cin>>map[i];
			for(int j=0;j<w;j++){
				if(map[i][j]=='@'){
					st_w=j;st_h=i;
				}
			}
		}
		count=1;
		search(st_h,st_w);
		cout<<count<<endl;
	}
	return 0;
}