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 <vector>
#include <string>
#include <queue>

using namespace std;


int frip(vector<vector<char>>& map, int h, int w, int H, int W)
{
    int cnt = (map[h][w] == '.') ? 1 : 0;
    map[h][w] = 'x';
    if(w != 0 && map[h][w-1] == '.'){
        //++cnt;
//        map[h-1][w-1] = 'x';
        cnt += frip(map, h, w-1, H, W);
    }
    if(w != W-1 && map[h][w+1] == '.'){
        //++cnt;
//        map[h-1][w+1] = 'x';
        cnt += frip(map, h, w+1, H, W);
    }
    if(h != H-1 && map[h+1][w] == '.'){
        //++cnt;
//        map[h+1][w-1] = 'x';
        cnt += frip(map, h+1, w, H, W);
    }
    if(h != 0 && map[h-1][w] == '.'){
        //++cnt;
//        map[h+1][w+1] = 'x';
        cnt += frip(map, h-1, w, H, W);
    }
    
    return cnt;
}


int solve(vector<vector<char>>& map, int H, int W)
{
    int ci, cj;
    for(int i=0;i<H;++i) for(int j=0;j<W;++j) if(map[i][j] == '@') { ci = i; cj = j; }
    
    return frip(map, ci, cj, H, W) + 1;
}


int main()
{
    int H, W;
    while(cin >> W >> H, H){
        vector<vector<char>> map(H);
        for(int i=0;i<H;++i){
            string str{};
            cin >> str;
            map[i] = vector<char>(str.begin(), str.end());
        }
        
        cout << solve(map, H, W) << endl;
//        for(int i=0;i<H;++i){
//            for(int j=0;j<W;++j) cout << map[i][j];
//            cout << endl;
//        }
    }
    
    return 0;
}

C++: #include<stdio.h>
#define SIZE 100000
typedef struct{
  int x;
  int y;
}Zahyo;
Zahyo  z[SIZE];
int dx[]={0,1,0,-1};
int dy[]={-1,0,1,0};
main(){
  int h,w;
  int head=0;
  int tail=0;
  int cnt=0;
  char maps[30][30];
  int i,j;
  scanf("%d %d",&w,&h);
  for(i=0;i<h;i++){
    for(j=0;j<w;j++){
      scanf("%c",&maps[i][j]);
      if(maps[i][j]=='@'){
	z[0]=(Zahyo){j,i};
	tail=(tail+1)%SIZE;
      }
    }
  }
  while(head != tail){
    int x = z[head].x; int y = z[head].y; head = (head+1) % SIZE;
    if(0 <= x && 0 <= y && x < w && y < h && maps[y][x] != '#'){
      maps[y][x] = '#';
      cnt++;
      for(i=0;i<4;i++){
	int nx = x + dx[i]; int ny = y + dy[i];
	z[(tail+i) % SIZE] = (Zahyo){ nx,ny };
      }
      tail = (tail+4) % SIZE;
    }
  }
  printf("%d\n",cnt);
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <utility>

using namespace std;


char map[30][30];
pair<int,int> s;
int w,h;
int cnt;
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
class Node{
public:
  int x,y;
  int cnt;
  Node(int _x,int _y,int _cnt):x(_x),y(_y),cnt(_cnt){

  }

};
int sx,sy;
void show(){
  for(int i=0;i<h;i++){
    cout<<map[i]<<endl;

  }
}
int bfs(){
  queue<Node> q;
  q.push(Node(sx,sy,0));
  map[sy][sx]='#';
  int ans=0;
  while(!q.empty()){
    Node cur=q.front();q.pop();
    //    show();
    ans=max(ans,cur.cnt);
    for(int i=0;i<4;i++){
      int nx=cur.x+dx[i];
      int ny=cur.y+dy[i];
      if(nx<0||nx>=w||ny<0||ny>=h||map[ny][nx]=='#')continue;
      map[ny][nx]='#';
      cnt++;
      q.push(Node(nx,ny,cur.cnt+1));
    }
  }
  return cnt;
    
}void solve(){
  cnt=0;

  cout<<bfs()+1<<endl;
}
int main(){
  while(cin>>w>>h&&(w||h)){
    //    for(int i=0;i<h;i++)for(int j=0;j<w;j++)map[i][j]='.';
    for(int i=0;i<h;i++){
      cin>>map[i];
      for(int j=0;j<w;j++){
	if(map[i][j]=='@'){
	  sx=j;sy=i;
	}
      }
    }
    solve();
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int ans=0;
vector<vector<int>> search(int x,int y,vector<vector<int>> umi){
    umi[y][x]=-1;
    ans++;
    
    if(umi[y+1][x]!=-1){
        umi=search(x,y+1,umi);
    }
    if(umi[y][x-1]!=-1){
        umi=search(x-1,y,umi);
    }
    if(umi[y][x+1]!=-1){
        umi=search(x+1,y,umi);
    }
    if(umi[y-1][x]!=-1){
        umi=search(x,y-1,umi);
    }
    return umi;
}


int main(void){
    int h,w;
    char c;
    int x;
    int y;
    vector<vector<int>> umi;
    while(cin>>w>>h,h){
        vector<vector<int>> umi;
        vector<int> nul;
        for(int j=0;j<w+2;j++){
            nul.push_back(-1);
        }
        umi.push_back(nul);
        for(int i=0;i<h;i++){
            vector<int> tmp;
            tmp.push_back(-1);
            for(int j=0;j<w;j++){
                if(cin>>c,c=='@'){
                    tmp.push_back(1);
                    x=j+1;
                    y=i+1;
                }
                else if(c=='.'){
                    tmp.push_back(0);
                }
                else{
                    tmp.push_back(-1);
                }
            }
            tmp.push_back(-1);
            umi.push_back(tmp);
            getchar();
        }
        umi.push_back(nul);
        for(int i=0;i<h+2;i++){
            for(int j=0;j<w+2;j++){
                //cout<<umi[i][j]+1;
            }
            //cout<<endl;
        }
        search(x,y,umi);

        
        cout<<ans<<endl;
        ans=0;
    }
    
}

C++: 6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.