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 <stdio.h>

int main()
{
	int W,H,ans;
	char map[100][100];
	scanf("%d %d",&W,&H);
	while(W != 0 && H != 0){
		for(int i = 0; i < H; i++){
			scanf("%s",map[i]);
		}
		for(int i = 0; i < 1000; i++){
			for(int j = 0; j < H - 1; j++){
				for(int k = 0; k < W; k++){
					if((map[j][k] == '.' && map[j + 1][k] == '@') || (map[j][k] == '@' && map[j + 1][k] == '.')){
						map[j][k] = '@';
						map[j + 1][k] = '@';
					}
				}
			}
			for(int j = 0; j < H; j++){
				for(int k = 0; k < W - 1; k++){
					if((map[j][k] == '.' && map[j][k + 1] == '@') || (map[j][k] == '@' && map[j][k + 1] == '.')){
						map[j][k] = '@';
						map[j][k + 1] = '@';
					}
				}
			}
		}

		ans = 0;

		for(int j = 0; j < H; j++){
			for(int k = 0; k < W; k++){
				if(map[j][k] == '@'){
					ans++;
				}
			}
		}

		printf("%d\n",ans);
		scanf("%d %d",&W,&H);
	}
	return 0;
}

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

int h, w;

int main (void){
	while(true){
		cin >> w >> h;
		if(!h&&!w)break;
		vector<string> s(h);
		for(int i = 0; i < h; ++i){
			cin >> s[i];
		}
		queue<pair<int, int>> q;
		for(int i = 0; i < h; ++i){
			for(int j = 0; j < w; ++j){
				if(s[i][j] == '@'){
					q.push(make_pair(i,j));
				}
			}
		}
		int dx[] = {0,1,0,-1};
		int dy[] = {1,0,-1,0};
		int cnt = 1;
		while(!q.empty()){
			int x = q.front().first;
			int y = q.front().second;
			q.pop();
			for(int dir = 0; dir < 4; ++dir){
				int nx = x + dx[dir];
				int ny = y + dy[dir];
				if(nx<0||nx>=h||ny<0||ny>=w) continue;
				if(s[nx][ny]=='.'){
					q.push(make_pair(nx,ny));
					s[nx][ny] = '!';
					cnt++;
				}
			}
		}
		cout << cnt << endl;
	}
	return 0;
}

C++: #include<iostream>
using namespace std;
int w,h;
char tail[21][21];
int a[]={-1,0,1,0};
int b[]={0,1,0,-1};
int cnt=0;
void func(int x,int y);
main(){
  int sx,sy;
  while(1){
    cin>> w>> h
      if(w==0&&h==0) break;
    cnt=0;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	cin>> tail[i][j];
	if(tail[i][j]=='@'){
	  sx=j;
	  sy=i;
	}
      }
    }
    func(sx,sy);
    cout<< cnt<< endl;
  }
}

void func(int x,int y){
  tail[y][x]='#';
  cnt++;
  for(int i=0;i<4;i++){
    int nx=x+a[i];
    int ny=y+b[i];
    if(nx>=0 && nx<w && ny>=0 && ny<h && tail[ny][nx]!='#'){
      func(nx,ny);
    }
  }
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define REP(i,a) for(int i=0;i<(a);i++)
#define MOD 1000000007

int main(){
  while(1){
    int h,w;
    cin>>w>>h;
    if(h==0&&w==0) break;
    char a[h][w];
    queue<pair<int,int>> q;
    bool used[h][w];
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
        cin>>a[i][j];
        used[i][j]=0;
        if(a[i][j]=='@') q.push(make_pair(i,j));
      }
    }
    int ans=0;
    while(!q.empty()){
      pair<int,int> p=q.front();
      q.pop();
      int x=p.first, y=p.second;
      if(used[x][y]) continue;
      used[x][y]=1;
      ans++;
      if(x>0){
        if(!used[x-1][y]&&a[x-1][y]=='.'){
          q.push(make_pair(x-1,y));
        }
      }
      if(x<h-1){
        if(!used[x+1][y]&&a[x+1][y]=='.'){
          q.push(make_pair(x+1,y));
        }
      }
      if(y>0){
        if(!used[x][y-1]&&a[x][y-1]=='.'){
          q.push(make_pair(x,y-1));
        }
      }
      if(y<w-1){
        if(!used[x][y+1]&&a[x][y+1]=='.'){
          q.push(make_pair(x,y+1));
        }
      }
    }
    cout<<ans<<endl;
  }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int w,h;
char tbl[30][30];
int dx[] = {0,0,1,-1};
int dy[] = {1,-1,0,0};
bool visited[30][30];
int ans;

void dfs(int x,int y){
  visited[y][x] = 1;
  ++ans;
  for(int i=0;i<4;++i){
    int mx = x+dx[i];
    int my = y+dy[i];
    if(mx>=w || my>=h || mx<0 || my<0) continue;
    if(visited[my][mx] || tbl[my][mx]=='#') continue;
    dfs(mx,my);
  }
}

int main(){
  while(cin>>w>>h,w||h){
    memset(visited,0,sizeof(visited));
    ans = 0;
    for(int i=0;i<h;++i) cin >> tbl[i];
    int sx=-1,sy=-1;
    for(int i=0;i<h;++i)
      for(int j=0;j<w;++j)
	if(tbl[i][j]=='@')
	  sy=i,sx=j;

    dfs(sx,sy);
    cout << ans << endl;
}
  return 0;
}

C++: /* 1130: Red and Black */
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <algorithm>
#include <functional>

using namespace std;

#define rep(i,n) for((i)=0;(i)<(int)(n);(i)++)
#define foreach(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)

#define MAX_W 20
#define MAX_H 20

using namespace std;

/* 隣接タイルへの移動用配列 */
int move[4][2] = {{1,0},{0,-1},{-1,0},{0,1}};
char tiles[MAX_H][MAX_W]; /* タイルの並びを入れる配列 */
int w,h; /* 床の幅、高さ */
int sum; /* 移動可能なタイル数 */

int i,j,k,l,m,n;

int dfs(int y, int x) {

//  rep(i,h){ rep(j,w) cout << tiles[i][j]; cout << endl; } cout << endl;
	for(int i=0; i<4; i++) {
		int nx = x+move[i][0], ny = y+move[i][1];
		
		if(
			/* 床の範囲内で */
			0<=nx&&nx<w &&
			0<=ny&&ny<h &&
			/* 黒いタイルだったら */
			tiles[ny][nx] == '.'
		) {
			/* 二重カウントしないよう予め書き換え */
			tiles[ny][nx] = '#';
			sum++;
			dfs(ny,nx);
		}
	}
	return 0;
}

int main(void) {
	
	int yy,xx;
	
	while(1) {
		
		cin >> w >> h;
		if(w==0 && h==0) break;
		
		for(int i=0; i<h; i++) {
			for(int j=0; j<w; j++) {
				cin >> tiles[i][j];
			}
		}

//    rep(i,h){ rep(j,w) cout << tiles[i][j]; cout << endl; } cout << endl;
//    cout << "before xx " << xx << endl;
//    cout << "before yy " << yy << endl;
		
		/* @の位置を探す */
		for(yy=0; yy<h; yy++) {
//      cout << 'a' << endl;
			for(xx=0; xx<w; xx++) {
//        cout << 'b' << endl;
				if(tiles[yy][xx] == '@') break;
			}
//      cout << "yy " << yy << "xx " << xx << "tile " << tiles[yy][xx] << endl;
			if(0 <= yy && yy < h && 0 <= xx && xx < w && tiles[yy][xx] == '@') break;
		}

//    cout << "xx " << xx << endl;
//    cout << "yy " << yy << endl;
		
		sum = 1;
		dfs(yy,xx);
		
		cout << sum << endl;
	}
	
	return 0;
}