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 main(){
    int w,h;
    while(1){
        cin>>w>>h;  if(w==0)break;
        char tile[h][w];    //タテｘヨコ
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++)cin>>tile[i][j];
        }
//        for(int i=0;i<h;i++){
//            for(int j=0;j<w;j++)cout<<tile[i][j]<<" ";
//            cout<<endl;
//        }

        int ans=1;

        for(int a=0;a<w*h;a++){

            for(int i=0;i<h;i++){
                for(int j=0;j<w;j++){

                    if(tile[i][j]=='@'){
                        tile[i][j]='#';

                        if(j<w-1 && tile[i][j+1]=='.'){
                            ans++;
                            tile[i][j+1]='@';
                        }
                        if(j>0 && tile[i][j-1]=='.'){
                            ans++;
                            tile[i][j-1]='@';
                        }
                        if(i<h-1 && tile[i+1][j]=='.'){
                            ans++;
                            tile[i+1][j]='@';
                        }
                        if(i>0 && tile[i-1][j]=='.'){
                            ans++;
                            tile[i-1][j]='@';
                        }

//                        for(int b=0;b<h;b++){
//                            for(int c=0;c<w;c++)cout<<tile[b][c];
//                            cout<<endl;
//                        }
//                        cout<<endl;
                        break;

                    }
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;

int main(){
  int w, h;
  
  int qi[10000], qj[10000];
  for(;;){
     char m[30][30];
     for(int i=0; i<30; i++){
       for(int j=0; j<30; j++){
         m[i][j] = '#';
       }
     }
    cin >> w >> h;
    if(w==0 && h==0) break;
    for(int i=0; i<h; i++){
      string s;
      cin >> s;
      for(int j=0; j<w; j++){
        m[i+1][j+1] = s[j];
        if(s[j]=='@'){
          qi[0] = i+1;
          qj[0] = j+1;
          m[i+1][j+1] = '.';
        }
      }
    }
    int nl=0, nr=1;
    int count = 0;
    while(nr-nl>0){
      int i = qi[nl];
      int j = qj[nl];
      if(m[i][j]=='@'){
        nl++;
      }else{
        m[i][j] = '@';
        count++;
        if(m[i+1][j]=='.'){
          qi[nr] = i+1;
          qj[nr] = j;
          nr++;
        }
        if(m[i-1][j]=='.'){
          qi[nr] = i-1;
          qj[nr] = j;
          nr++;
        }
        if(m[i][j+1]=='.'){
          qi[nr] = i;
          qj[nr] = j+1;
          nr++;
        }
        if(m[i][j-1]=='.'){
          qi[nr] = i;
          qj[nr] = j-1;
          nr++;
        }
        nl++;
      }
    }
    /*
    for(int i=0; i<h+2; i++){
      for(int j=0; j<w+2; j++){
        cout << m[i][j];
      }
      cout << endl;
    }
  */
    
    cout << count << endl;
  }

	return 0;
}

C++: #include<iostream>
#include<string>
#include<queue>
#include<vector>
using namespace std;
#define frp(i,n) for((i)=0;(i)<(n);i++)
int vx[4]={0,1,0,-1};
int vy[4]={1,0,-1,0};
int H,W;
vector<int> red1;
vector<int> red2;
int check(int n1,int n2){
  for(int i=0;i<red1.size();i++){
    if(n1==red1[i]&&n2==red2[i]||n1<0||n2<0||n1>H||n2>W)
      return 0;
  }
  return 1;
}
int main(){
  int i,j;
  string str2;
  vector<string> str;
  while(cin>>W>>H,W+H!=0){
    int num=1;
    frp(i,H){
      cin>>str2;
      str.push_back(str2);
    }
    frp(i,H){
      frp(j,W){
	if(str[i][j]=='#'){
	  red1.push_back(i);
	  red2.push_back(j);
	}
      }
    }
    queue<pair<int,int> > qii;
    frp(i,H){
      frp(j,W){
	if(str[i][j]=='@')
	  qii.push(make_pair(i,j));
      }
    }
    while(!qii.empty()){
      int now1=qii.front().first;
      int now2=qii.front().second;
      qii.pop();//?????????????´??????´?£?
      for(i=0;i<4;i++){
	if(check(now1+vx[i],now2+vy[i])==1){
	  red1.push_back(now1);
	  red2.push_back(now2);
	  qii.push(make_pair(now1+vx[i],now2+vy[i]));
	  num++;
	}
      }
    }
    cout<<num<<endl;
  }
  return 0;
}

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

#define ll long long
#define vvi vector<vector<int>>
#define vec vector
#define pq priority_queue
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (int (i) = (0); (i) < (n); ++(i))
#define repp(i, m, n) for (int (i) = (m); (i) < (n); ++(i))
#define debug(x) cerr << #x << ": " << x << endl;
#define debug2(x, y) cerr<<"("<<#x<<", "<<#y<<") = "<<"("<<x<<", "<<y<<")"<<endl;
#define debug3(x, y, z) cerr<<"("<<#x<<", "<<#y<<", "<<#z<<") = "<<"("<<x<<", "<<y<<", "<<z<<")"<<endl;
#define debugB(x, y) cerr<<#x<<": "<<bitset<y>(x) << endl;
#define line() cerr << "---------------" << endl;

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

template<typename T>
void printA(vector<T> &printArray, char between = ' ') {
   int paSize = printArray.size();
   for (int i = 0; i < paSize; i++) { cerr << printArray[i] << between;}
   if (between != '\n') {cerr << endl;}
}

// ------------------------------------------------------------------------------------------

int W, H;
char B[30][30];

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

void solve() {
   rep (i, H) {
      cin >> B[i];
   }
   rep (i, H) {
      rep (j, W) {
         if (B[i][j] == '@') {
            cout << dfs(i, j) << endl;
            return;
         }
      }
   }
}

int main() {
   while (1) {
      cin >> W >> H;
      if (H*W == 0) break;
      solve();
   }

   return 0;
}