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>
using namespace std;

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

int W, H, ans;
string maze[20];

bool check(int x, int y){
	return x >= 0 && x < W && y >= 0 && y < H && maze[y][x] == '.';
}

void dfs(int x, int y){
	
	for(int i = 0; i < 4; ++i){
		int nx = x + dx[i], ny = y + dy[i];
		if(check(nx, ny)){
			maze[ny][nx] = '#';
			ans++;
			dfs(nx, ny);
		}
	}
}

int main() {
	while(cin >> W >> H && (W || H)){
		for(int i = 0; i < H; ++i)
				cin >> maze[i];
		
		ans = 0;
		for(int i = 0; i < H; ++i){
			for(int j = 0; j < W; ++j){
				if(maze[i][j] == '@'){
					dfs(j, i);
				}
			}
		}
		cout << ans + 1 << endl;
	}
	return 0;
}

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

using ll = long long;
#define debug(x) cerr << #x << ": " << x << endl;
#define line() cerr << "---------------" << endl;

int W, H;
char board[30][30];
bool visited[30][30];

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

void dfs(int x, int y) {
   for (int i = 0; i < 4; i++) {
      int nx = x + dx[i], ny = y + dy[i];
      if (nx < 0 || nx >= H || ny < 0 || ny >= W) continue;
      if (board[nx][ny] == '#') continue;
      if (visited[nx][ny]) continue;
      visited[nx][ny] = true;
      dfs(nx, ny);
   }
}

int main() {
   while (cin >> W >> H, W || H) {
      memset(visited, false, sizeof(visited));
      int sx = 0, sy = 0;
      for (int i = 0; i < H; i++) {
         for (int j = 0; j < W; j++) {
            cin >> board[i][j];
            if (board[i][j] == '@') {
               sx = i, sy = j;
            }
         }
      }

      visited[sx][sy] = true;
      dfs(sx, sy);
      
      int cnt = 0;
      for (int i = 0; i < H; i++) {
         for (int j = 0; j < W; j++) {
            if (visited[i][j]) cnt++;
         }
      }
      cout << cnt << endl;
   }

   return 0;
}

C++: #include<iostream>
#include<complex>
#include<queue>
using namespace std;
const int dx[] = {1,0,-1,0};
const int dy[] = {0,1,0,-1};
int main(){

    int w,h;
    while(cin>>w>>h,w){
        string screen[30];
        bool flg[30][30]={{false}};

        complex<int> pos;
        for(int i = 0;i < h;i++){
            cin>>screen[i];
            for(int j = 0;j < w;j++){
                if(screen[i][j] == '@'){
                    pos.real() = i;
                    pos.imag() = j;
                }
            }
        }

        queue< complex < int > > Q;
        Q.push(pos);
        while(!Q.empty()){
            complex<int> D = Q.front();
            Q.pop();
            flg[D.real()][D.imag()] = true;
            for(int i = 0;i < 4;i++){
                int x = D.real()+dx[i],y = D.imag()+dy[i];
                if(0<=x&&x<h&&0<=y&&y<w){
                    if(!flg[x][y]&&screen[x][y] == '.'){
                        complex<int>E;
                        E.real() = x;
                        E.imag() = y;
                        Q.push(E);
                    }
                }
            }
        }

        int cnt = 0;
        for(int i = 0;i < h;i++){
            for(int j = 0;j < w;j++){
                cnt += flg[i][j];
            }
        }
        cout<<cnt<<endl;


    }


    return 0;
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<utility>
#include<queue>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<complex>
#define INF 2147483647
#define llINF 9223372036854775807
#define pb push_back
#define mp make_pair 
#define F first
#define S second
#define ll long long

using namespace std;
int main(){
  int w,h;
  int vx[4]={1,0,-1,0};
  int vy[4]={0,-1,0,1};
  while(cin>>w>>h,h+w){
    string str[h];
    for(int i=0;i<h;i++)cin>>str[i];
    bool Map[h][w]={};
    queue<pair<int,int> >que;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	if(str[i][j]=='@'){
	  que.push(mp(i,j));
	  Map[i][j]=true;
	}
      }
    }
    while(!que.empty()){
      pair<int,int>now=que.front();
      que.pop();
      for(int i=0;i<4;i++){
	if(now.F+vy[i]>=0&&now.S+vy[i]<w&&now.F+vx[i]>=0&&now.F+vx[i]<h&&(!Map[now.F+vx[i]][now.S+vy[i]])&&str[now.F+vx[i]][now.S+vy[i]]=='.'){
	  Map[now.F+vx[i]][now.S+vy[i]]=true;
	  que.push(mp(now.F+vx[i],now.S+vy[i]));
	}
      }
    }
    ll ans=0;
    for(int i=0;i<h;i++)
      for(int j=0;j<w;j++)
	if(Map[i][j])
	  ans++;
    cout<<ans<<endl;
  }
  return 0;
}

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

const int MAX_N = 20;

int W, H;
int sx, sy;                 // ?????????????????§?¨?
int dx[4] = {-1, 0, 0, 1};
int dy[4] = { 0,-1, 1, 0};

bool isVisited[MAX_N][MAX_N];

int dfs(const vector<vector<char>>& masu, int x, int y) {
    if (!isVisited[y][x] && masu[y][x] != '#') {
        isVisited[y][x] = true;

        int ret = 1;
        for (int i = 0; i < 4; i++) {
            if (0 <= x+dx[i] && x+dx[i] < W && 0 <= y+dy[i] && y+dy[i] < H) {
                // cout << "#: x=" << x+dx[i] << ", y=" << y+dx[i] << endl;
                ret += dfs(masu, x+dx[i], y+dy[i]);
            }
        }

        return ret;
    }
    return 0;
}

int main(void) {

    while (cin >> W >> H, W) {
        for (int i = 0; i < MAX_N; i++) {
            for (int j = 0; j < MAX_N; j++) {
                isVisited[i][j] = false;
            }
        }

        vector<vector<char>> masu(H, vector<char>(W));
        for (int i = 0; i < H; i++) {
            for (int j = 0; j < W; j++) {
                cin >> masu[i][j];
                if (masu[i][j] == '@') {
                    sx = j;
                    sy = i;
                }
            }
        }

        cout << dfs(masu, sx, sy) << endl;
    }

    return 0;
}