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>
using namespace std;
int W,H;
int t[20][20];
int c;
char ch;

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

bool inField(int y,int x){
  if( 0<=y && y<H && 0<=x && x<W ){
    return true;
  }else{
    return false;
  }
}

void visit(int y,int x){
  c++;
  t[y][x]=1;
  for(int i=0;i<4;i++){
    int ny=y+dy[i];
    int nx=x+dx[i];
    if( inField(ny,nx) && t[ny][nx]==0){
      visit(ny,nx);
    }
  }
}
int main(){
  while(1){
    cin>>W>>H;
    if(W==0&&H==0)break;
    int sy,sx;
    for(int i=0;i<H;i++){
      for(int j=0;j<W;j++){
        cin>>ch;
        if(ch=='.'){
          t[i][j]=0;
        }
        if(ch=='@'){
          sy=i;
          sx=j;
        }
        if(ch=='#'){
          t[i][j]=-1;
        }
      }
    }
    c=0;
    visit(sy,sx);
    cout<<c<<endl;
  }
  return 0; 
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <functional>
#include <utility>
using namespace std;
#define MOD 1000000007
#define ADD(X,Y) ((X) = ((X) + (Y)%MOD) % MOD)
typedef long long i64; typedef vector<int> ivec; typedef vector<string> svec; typedef pair<int, int> pi;
int dd[] = { 0, 1, 0, -1, 0 };


int main()
{
	while (true)
	{
		int W, H;
		cin >> W >> H;
		if (W == 0 && H == 0) break;

		char C[30][30];
		int B[30][30];

		int sx = -1; int sy = -1;
		for (int i = 0; i < H; i++)
		{
			for (int j = 0; j < W; j++)
			{
				cin >> C[i][j];
				B[i][j] = 0;
				if (C[i][j] == '@')
				{
					sy = i;
					sx = j;
				}
			}
		}

		queue<pi> Q;
		Q.push(pi(sy, sx));
		B[sy][sx] = 1;
		int ans = 1;
		while (!Q.empty())
		{
			int ty = Q.front().first; int tx = Q.front().second;
			Q.pop();

			for (int r = 0; r < 4; r++)
			{
				int y = ty + dd[r];
				int x = tx + dd[r + 1];
				if (y < 0 || y >= H || x < 0 || x >= W)continue;
				if (C[y][x] == '#')continue;
				if (B[y][x])continue;
				ans++;
				B[y][x] = 1;
				Q.push(pi(y, x));
			}
		}

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

C++: #include <iostream>
#include <string>
#include <algorithm>
using namespace std;
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REP(i,b) FOR(i,0,b)
#define INF 1e9

int W,H;
int t_situ[20][20];
int dx[] = {1,0,-1,0}, dy[] = {0,1,0,-1};
int x_start, y_start, Count;

void move(int x_standard, int y_standard){
  if(t_situ[y_standard][x_standard] == 2 && x_standard >=0 && x_standard < W
      && y_standard >=0 && y_standard < H){
    t_situ[y_standard][x_standard] = 1;
    Count++;
    REP(k,4) move(x_standard+dx[k], y_standard+dy[k]);
  }/*else if(t_situ[y_standard][x_standard] == 1 )
      REP(k,4) move(x_standard+dx[k]+dy[k], y_standard+dx[k]+dy[k]);*/
}

void solve(int X, int Y){
  Count = 0;
  char t_type[Y][X];
  REP(i,20) REP(j,20) t_situ[i][j] = 0;
  //0:red, 1:black&gone, 2:black&not_gone

  REP(i,Y){
    REP(j,X){
      cin>>t_type[i][j];
      switch (t_type[i][j]) {
        case '@':
            t_situ[i][j] = 2;
            x_start = j;
            y_start = i;
            break;
        case '.':
            t_situ[i][j] = 2;
            break;
        case '#':
            t_situ[i][j] = 0;
      }
    }
  }

  move(x_start, y_start);

  cout<<Count<<endl;
  /*REP(i,20){
    REP(j,20){
      cout<<t_situ[i][j];
    }
    cout<<endl;
  }*/
}

int main(){

  while(1) {
    cin>>W>>H;
    if(W == 0 && H == 0) break;
    solve(W,H);
  }

  return 0;
}

C++: #include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
#include <iomanip>
#include <iostream>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define REP(i,m,n) for (int i=m;i<(int)(n);i++)

const int INF = 10000000;
int dx[4]={1, 0, -1, 0}, dy[4]={0, 1, 0, -1};
using namespace std;
typedef long long  ll;
typedef pair<int, int> P;

/** Problem 1130 : Red and Black **/
int main()
{
	bool field[20][20];
	bool vis[20][20];
	int W, H, sx, sy; char c;
	
	while (cin >> W >> H, W||H) {
		int ans = 0;
		
		rep(y, H) {
			rep(x, W) {
				cin >> c;
				if (c=='.') {
					field[x][y]=true;
				} else if (c=='@') {
					field[x][y]=true;
					sx = x; sy = y;
				} else {
					field[x][y] = false;
				}
				vis[x][y] = false;
			}
		}
		
		queue<P> Q;
		Q.push(P(sx, sy));
		
		while (Q.size()!= 0) {
			P p = Q.front(); Q.pop();
			if (!vis[p.first][p.second]) {
				ans++;
				vis[p.first][p.second]=true;
			}
			
			rep(i, 4) {
				int nx = p.first+dx[i], ny=p.second+dy[i];
				if (0<= nx && nx < W && 0 <= ny && ny < H && field[nx][ny] && !vis[nx][ny])
					Q.push(P(nx, ny));
			}
		}
		cout << ans << endl;
	}
	return 0;
}

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

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

char field[20][20];
bool flag[20][20];
int W, H, ans;

void dfs(int x, int y) {
  //field[x][y] = '.';
  //cout << x << ' ' << y << endl;
  //cout << flag[x][y] << endl;
  //cout << ans << endl;
  for(int i = 0 ; i < 4 ; i++) {
    int nx = x + dx[i], ny = y + dy[i];
    if(0 <= nx && nx < H && 0 <= ny && ny < W && field[nx][ny] == '.' &&
       flag[nx][ny] == false) {
      ans++; flag[nx][ny] = true; dfs(nx, ny);
      //cout << "OK" << endl;
    }
    //else dfs(nx, ny);
  }
}

int main(void) {
  int x, y;
  int res;
  
  while(cin >> W >> H, W != 0 && H != 0) {
    memset(flag, false, sizeof(flag));
    ans = 1;
    for(int i = 0 ; i < H ; i++) {
      for(int j = 0 ; j < W ; j++) {
	cin >> field[i][j];
	if(field[i][j] == '@') x = i, y = j;
      }
    }
    /*for(int i = 0 ; i < H ; i++) {
      for(int j = 0 ; j < W ; j++) {
	cout << field[i][j];
      }
      cout << endl;
      }*/
    //printf("@x = %d, @y = %d\n", x, y);
    dfs(x, y);
    cout << ans << endl;
  }
  return 0;
}