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 <queue>
#include <iostream>
using namespace std;
int H, W;

int main () {
  const int dx[]={1,0,-1,0}, dy[]={0,-1,0,1}; 
  while(cin >> W >> H && W != 0 && H != 0) {
    int srcy, srcx;
    int G[W][H];
    int D[W][H];
    for (int i = 0; i < H; i++) {
      for (int k = 0; k < W; k++) {
        D[k][i] = -1;
        char input;
        cin >> input;
        if (input == '.') {
          G[k][i] = 0;
        } else if (input == '#') {
          G[k][i] = 1;
        } else {
          G[k][i] = 0;
          srcx = k;
          srcy = i;
        }
      }
    }
    // for (int i = 0; i < H; i++) {
    //   for (int k = 0; k < W; k++) {
    //     cout << G[k][i] << " ";
    //   }
    //   cout << endl;
    // }
    // for (int i = 0; i < H; i++) {
    //   for (int k = 0; k < W; k++) {
    //     cout << D[k][i] << " ";
    //   }
    //   cout << endl;
    // }
    
    queue<pair<int, int> > Q; // 整数を管理するキューの定義
    pair<int, int> src = make_pair(srcx, srcy);
    Q.push(src);
    D[srcx][srcy] = 0; // 出発点
    // cerr << "src " << srcx << ", " << srcy  << endl;
    while (! Q.empty()) {
      pair<int, int> cur = Q.front(); // 先頭要素を取り出す
      Q.pop();
      int x = cur.first;
      int y = cur.second;
      // cerr << "src " << x << ", " << y  << endl;
      for (int i = 0; i < 4; i++) {
        int dstx = x+dx[i];
        int dsty = y+dy[i];
        // cerr << "visiting " << dstx << ", " << dsty << ", " << G[dstx][dsty] << ", " <<  D[dstx][dsty]  << endl;
        if (
          (dstx >= 0 && dstx <= W - 1)
          && (dsty >= 0 && dsty <= H - 1)
          && G[dstx][dsty] == 0
          && D[dstx][dsty] == -1
        ) { // cur から dst に辺があり，dst が未訪問なら
          D[dstx][dsty] = D[x][y]+1; //
          Q.push(make_pair(dstx, dsty));
        }
      }
    }
    // for (int i = 0; i < H; i++) {
    //   for (int k = 0; k < W; k++) {
    //     cout << D[k][i] << " ";
    //   }
    //   cout << endl;
    // }
    int r = 0;
    for (int i = 0; i < W; i++) {
      for (int k = 0; k < H; k++) {
        if (D[i][k] != -1) {
          r++;
        }
      }
    }
    cout << r << endl;
  }
}

C++: #include <bits/stdc++.h>
using namespace std;
int main(void){
    int w,h;
    while (cin>>w>>h) {
        if (w==0&&h==0) {
            break;
        } else {
            std::vector<vector<char>> vec(h,vector<char>(w));
            bool seen[h][w];
            char c;
            int now_H,now_W;
            for (int i=0;i<h;i++) {
                for (int j=0;j<w;j++) {
                    cin>>c;
                    vec[i][j]=c;
                    if (c=='@') {
                        now_H=i;
                        now_W=j;
                        seen[i][j]=true;
                    } else {
                        seen[i][j]=false;
                    }
                }
            }
            int count=1;
            std::deque<pair<int,int>> place;
            place.emplace_back(make_pair(now_H,now_W));
            while (place.size()!=0) {
                int tmp_H=place.front().first;
                int tmp_W=place.front().second;
                if (tmp_H+1<h&&seen[tmp_H+1][tmp_W]==false&&vec[tmp_H+1][tmp_W]!='#'&&vec[tmp_H+1][tmp_W]!='@') {
                    tmp_H++;
                    seen[tmp_H][tmp_W]=true;
                    place.emplace_back(make_pair(tmp_H,tmp_W));
                    count++;
                } else if (tmp_H-1>=0&&seen[tmp_H-1][tmp_W]==false&&vec[tmp_H-1][tmp_W]!='#'&&vec[tmp_H-1][tmp_W]!='@') {
                    tmp_H--;
                    seen[tmp_H][tmp_W]=true;
                    place.emplace_back(make_pair(tmp_H,tmp_W));
                    count++;
                } else if (tmp_W+1<w&&seen[tmp_H][tmp_W+1]==false&&vec[tmp_H][tmp_W+1]!='#'&&vec[tmp_H][tmp_W+1]!='@') {
                    tmp_W++;
                    seen[tmp_H][tmp_W]=true;
                    place.emplace_back(make_pair(tmp_H,tmp_W));
                    count++;
                } else if (tmp_W-1>=0&&seen[tmp_H][tmp_W-1]==false&&vec[tmp_H][tmp_W-1]!='#'&&vec[tmp_H][tmp_W-1]!='@') {
                    tmp_W--;
                    seen[tmp_H][tmp_W]=true;
                    place.emplace_back(make_pair(tmp_H,tmp_W));
                    count++;
                } else {
                    place.pop_front();
                }
            }
            cout<<count<<endl;
        }
    }
}

C++: #include<iostream>
#include<string.h>
#include<string>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#include<set>
#define ll long long int
#define ld long double
#define INF 1000000000
#define EPS 0.0000000001
#define rep(i,n) for(i=0;i<n;i++)
using namespace std;
typedef pair<int, int> pii;

int main()
{
  int i,j,k;
  int h,w;
  int dx[4]={0,1,0,-1};
  int dy[4]={1,0,-1,0};
  bool used[25][25];
  char c[25][25];

  while(1){

    cin>>w>>h;
    if(w==0 && h==0)break;

    int sum=1;
    rep(i,25)rep(j,25)
      used[i][j]=false;

    rep(i,h)
      scanf("%s",c[i]);

    queue<pii> q;
    rep(i,h)rep(j,w)
      if(c[i][j]=='@'){
	q.push(pii(i,j));//y,x
	used[i][j]=true;
	break;
      }

    while(q.size()){
      i=q.front().first;
      j=q.front().second;
      q.pop();
      rep(k,4){
	int y=i+dy[k];
	int x=j+dx[k];
	if(y>=0 && y<h && x>=0 && x<w && used[y][x]==false && c[y][x]=='.'){
	    q.push(pii(y,x));
	    used[y][x]=true;
	    sum++;
	  }
      }
    }

    cout<<sum<<endl;

  }
}