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 <iostream>
#include <string>
#include <algorithm>
#include <queue>
#include <utility>
using namespace std;
int xd[]={1,0,0,-1};
int yd[]={0,1,-1,0};
int main()
{
	int W,H;
	while(cin>>W>>H,W!=0){
		vector<string> board(H);
		queue<pair<int,int> > q;
		for(int i=0;i<H;i++)
			cin>>board[i];
		for(int i=0;i<H;i++){
			string::iterator
			p=find(board[i].begin(),board[i].end(),'@');
			if(p!=board[i].end()){
				q.push(pair<int,int>(i,p-board[i].begin()));
				break;
			}
		}
		int count=0;
		while(!q.empty()){
			pair<int,int> v=q.front();
			q.pop();
			if(board[v.first][v.second]=='#')continue;
			board[v.first][v.second]='#';
			count++;
			for(int i=0;i<4;i++){
				int newx=xd[i]+v.first;
				int newy=yd[i]+v.second;
				if(0<=newx&&newx<H&&0<=newy&&newy<W
						&&board[newx][newy]=='.')
					q.push(pair<int,int>(newx,newy));

			}
		}
		cout<<count<<endl;
	}
	return 0;
}

C++: #include<iostream>
using namespace std;
bool b[21][21];
int w,h,count=0;

void dfs(int x, int y){
 b[y][x]=1;count++;
 int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
 int nx,ny;
 for(int i=0;i<4;i++){
  nx=dx[i]+x;
  ny=dy[i]+y;
  if(0<=nx&&nx<w&&0<=ny&&ny<h)
   if(b[ny][nx]==0)dfs(nx,ny);
  }
 }

int main(){
char a;
int sx,sy;
while(1){
cin>>w>>h;
count=0;
if(w+h==0)break;
for(int i=0;i<h;i++)
for(int j=0;j<w;j++){
cin>>a;
b[i][j]=0;
if(a=='#')b[i][j]=1;
if(a=='@'){sx=j;sy=i;}
}
dfs(sx,sy);
cout<<count<<endl;
}
 return 0;
}

C++: #include<iostream>
#include<queue>
 
using namespace std;
typedef pair<int, int> pii;
pii dxdy[]={{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
char fld[20][20];
int d[20][20];
const int inf=1e8;
 
int main(){
 
   while(1){
      int W, H;
      cin>> W>> H;
      if(!(H||W)) break;
      int sx, sy;
      for(int i=0; i<H; i++){
         for(int j=0; j<W; j++){
            cin>> fld[i][j];
            if(fld[i][j]=='@'){
               sy=i;
               sx=j;
            }
         }
      }
 
      for(int i=0; i<H; i++)for(int j=0; j<W; j++)d[i][j]=inf;
      queue<pii> Q;
      Q.push(pii(sy, sx));
      d[sy][sx]=0;
      while(!Q.empty()){
         pii now=Q.front(); Q.pop();
         for(pii dd: dxdy){
            int ni=now.first+dd.first;
            int nj=now.second+dd.second;
            if(!(0<=ni&&ni<H&&0<=nj&&nj<W)) continue;
            if(fld[ni][nj]=='#') continue;
            if(d[ni][nj]<=d[now.first][now.second]+1) continue;
            d[ni][nj]=d[now.first][now.second]+1;
            Q.push(pii(ni, nj));
         }
      }
 
      int ans=0;
      for(int i=0; i<H; i++)for(int j=0; j<W; j++){
         ans+=(d[i][j]!=inf);
      }
 
      cout<< ans<< endl;
   }
 
   return 0;
}

C++: #include <iostream>
#include <algorithm>
#define MH 20
#define MW 20
using namespace std;
char f[MH][MW];
bool v[MH][MW];
int dx[4] = { 1, 0, -1, 0 };
int dy[4] = { 0, 1, 0, -1 };
int w, h;
int c = 0;

void search(int x, int y) {
	++c;
	v[y][x] = true;
	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 && v[ny][nx] == false && f[ny][nx] != '#') {
			search(nx, ny);
		}
	}
	return;
}

int search2(int x, int y) {
	v[y][x] = true;
	int res = 1;
	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 && v[ny][nx] == false && f[ny][nx] != '#') {
			res += search2(nx, ny);
		}
	}
	return res;
}

int main() {
	int sx, sy;

	while (cin >> w >> h, !(w == 0 && h == 0)) {
		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;
				}
			}
		}
		c = 0;
		fill(v[0], v[MH], false);
		//search(sx, sy);
		//cout << c << endl;
		cout << search2(sx, sy) << endl;
		
	}
	return 0;
}

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

using namespace std;

struct Point{
	int x,y;
};

const Point DIR[4] = {{1,0},{0,1},{-1,0},{0,-1}};

bool inField(int w,int h, int x,int y){
	return 0<= x && x < w && 0<= y && y<h;
}

int main(){
	while(true){
		int w,h;
		cin >> w >> h;
		if(w == 0){
			break;
		}
		char map[20][20];
		bool visited[20][20];
		Point init;//初期位置
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				cin >> map[i][j];
				visited[i][j] = false;
				if(map[i][j] == '@'){
					init = (Point){j,i};
					map[i][j] = '.';
				}
			}
		}
		//入力終わり。探索開始

		int cnt=1;//初期位置の分を含める
		visited[init.y][init.x] = true;//初期位置の処理
		
		queue<Point> open;
		open.push(init);
		while(open.empty()==false){
			Point now = open.front();//先頭の要素をget
			open.pop();//front()とセットで使う。先頭の要素をremove
			for(int i=0;i<4;i++){
				Point next = {now.x + DIR[i].x, now.y + DIR[i].y};
				if(inField(w,h,next.x,next.y)==true){
					if(visited[next.y][next.x] == false && map[next.y][next.x] == '.'){
						visited[next.y][next.x] = true;
						cnt++;
						open.push(next);
					}
				}
			}
		}
		cout << cnt << endl;
	}

}
//(特にboolの)代入時にcheck == trueなんてしないように注意