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++: #define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
bool c[20][20] = {} ;
int cnt,w, h, p[20][20] = {};

int main() {
	int j, f,cx,cy;
	char in[20];
	while (1) {
		for (int i = 0; i < 400; i++) {
			c[i / 20][i % 20] = p[i / 20][i % 20] = 0;
		}
		cnt = 0;
		f = 1;
		scanf("%d %d", &w, &h);
		if (w ==0&& h == 0)
			break;
		scanf("\n", &j);
		for (int i = 0; i < h; i++) {
			scanf("%s", &in);
			for (j = 0; j < w; j++) {
				switch (in[j]) {
				case '#':
					c[j][i] = 0;
					p[j][i] = -1;
					break;
				case '@':
					cx = j;
					cy = i;
					p[j][i] = 1;
				case '.':
					c[j][i] = 1;
				}
			}
		}

		/*for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++)
				printf("%3d", c[j][i]);
			printf("\n");
		}*/

		while (f == 1) {
			f = 0;
			if (w != 1 && h != 1) {
				for (int i = 0; i < w*h; i++) {
					if (p[i / h][i%h] == 0 && (p[i / h + 1][i % h] == 1 || p[i / h - 1][i % h] == 1 || p[i / h][i % h + 1] == 1 || p[i / h][i % h - 1] == 1)) {
						p[i / h][i%h] = 1;
						f = 1;
					}
				}
			}
			if (w == 1 && h != 1) {
				for (int i = 0; i < h; i++) {
					if (p[0][i] == 0 && (p[0][i + 1] == 1 || p[0][i - 1] == 1)) {
						p[0][i] = 1;
						f = 1;
					}
				}
			}

			if (w != 1 && h == 1) {
				for (int i = 0; i < w; i++) {
					if (p[i][0] == 0 && (p[i + 1][0] == 1 || p[i - 1][0] == 1)) {
						p[i][0] = 1;
						f = 1;
					}
				}
			}


			/*for (int i = 0; i < h; i++) {
				for (int j = 0; j < w; j++)
					printf("%3d", p[j][i]);
				printf("\n");
			}
			printf("\n");*/
		}

		for (int i = 0; i < w*h; i++) {
			if (p[i / h][i%h] == 1)
				cnt++;
		}
		printf("%d\n", cnt);
	}
	return 0;
}

C++: #include<iostream>
#include<utility>
#include<queue>
#define INF 10000000000
using namespace std;

typedef pair<int, int>P;

char maze[20][20];
int H,W;
int sx,sy,gx,gy;
int d[20][20];

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

int bfs(){
  int rel=1;
  queue<P> que;

  for(int i=0;i<H;i++)
    for(int j=0;j<W;j++) d[i][j]=0;

  que.push(P(sx,sy));
  d[sx][sy]=1;
 
  while(que.size()){
    P p=que.front();
    que.pop();

    for(int i=0;i<4;i++){
      int nx=p.first+dx[i],ny=p.second+dy[i];
      if(0<=nx&&nx<H&&0<=ny&&ny<W&&maze[nx][ny]!='#'&&d[nx][ny]==0){
	que.push(P(nx,ny));
	d[nx][ny]=1;
	rel++;
      }
    }
  }
  return rel;
}

void solve(){
  int res=bfs();
  cout<<res<<endl;
}

int main(){
  char s;

  while(1){
    cin>>W>>H;
    if(W+H==0)break;
    for(int i=0;i<H;i++){
      for(int j=0;j<W;j++){
	cin>>maze[i][j];
	if(maze[i][j]=='@'){
	  sx=i;
	  sy=j;
	}
      }
    }
    solve();
  }
  return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
bool c[22][22] = {} ;
int cnt,w, h, p[22][22] = {};

int main() {
	int j, f,cx,cy;
	char in[21];
	while (1) {
		for (int i = 0; i < 484; i++) {
			c[i / 22][i % 22] = p[i / 22][i % 22] = 0;
		}
		cnt = 0;
		f = 1;
		scanf("%d %d", &w, &h);
		if (w ==0&& h == 0)
			break;
		scanf("\n", &j);
		for (int i = 0; i < h; i++) {
			scanf("%s", &in);
			for (j = 0; j < w; j++) {
				switch (in[j]) {
				case '#':
					c[j+1][i+1] = 0;
					p[j+1][i+1] = -1;
					break;
				case '@':
					cx = j;
					cy = i;
					p[j+1][i+1] = 1;
				case '.':
					c[j+1][i+1] = 1;
				}
			}
		}

		/*for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++)
				printf("%3d", c[j][i]);
			printf("\n");
		}*/

		while (f == 1) {
			f = 0;
/*			for (int i = 0; i < h; i++) {
				for (int j = 0; j < w; j++)
					printf("%3d", p[j+1][i+1]);
				printf("\n");
			}
			printf("\n");
*/			if (w != 1 && h != 1) {
				for (int i = 0; i < w*h; i++) {
					if (p[i / h+1][i%h+1] == 0 && (p[i / h + 2][i % h+1] == 1 || p[i / h][i % h+1] == 1 || p[i / h+1][i % h + 2] == 1 || p[i / h+1][i % h] == 1)) {
						p[i / h+1][i%h+1] = 1;
						f = 1;
					}
				}
			}
			if (w == 1 && h != 1) {
				for (int i = 0; i < h; i++) {
					if (p[1][i+1] == 0 && (p[1][i + 2] == 1 || p[0][i] == 1)) {
						p[1][i+1] = 1;
						f = 1;
					}
				}
			}

			if (w != 1 && h == 1) {
				for (int i = 0; i < w; i++) {
					if (p[i+1][1] == 0 && (p[i + 2][1] == 1 || p[i][1] == 1)) {
						p[i+1][1] = 1;
						f = 1;
					}
				}
			}
		}

		for (int i = 0; i < w*h; i++) {
			if (p[i / h+1][i%h+1] == 1)
				cnt++;
		}
		printf("%d\n", cnt);
	}
	return 0;
}

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

int vis[25][25];

void dfs(int i,int j){
  if(vis[i][j]==0 || vis[i][j]==2)return;
  vis[i][j]=0;

    dfs(i-1,j);
    dfs(i,j-1);
    dfs(i,j+1);
    dfs(i+1,j);
}

int main(){
  int w,h;
  int i,j;
  char ch;
  int cnt;

  while(1){
    cnt=0;
    cin >> w >> h;
    if(w==0 && h==0)break;

    for(i=1;i<=h;i++){
      for(j=1;j<=w;j++){
	cin >> ch;
	if(ch=='.'){
	  vis[i][j]=1;
	}else if(ch=='#'){
	  vis[i][j]=2;
	}else{
	  vis[i][j]=5;
	}
      }
    }
     for(i=1;i<=h;i++){
      for(j=1;j<=w;j++){
	if(vis[i][j]==5)dfs(i,j);
      }
     }
      for(i=1;i<=h;i++){
      for(j=1;j<=w;j++){
	if(vis[i][j]==0)cnt++;
      }
     }
      cout << cnt << endl;
  }
}

C++: #include <iostream>

using namespace std;

int w, h, ans;
char map[20][20];

void ex(int x, int y) {
  if(x < 0 || y < 0 || x >= w || y >= h) return;
  if(map[x][y] == '#') return;

  if(map[x][y] == '@' || map[x][y] == '.') {
    map[x][y] = ':';
    ans ++;
  }
  else {
    return;
  }
  
  ex(x-1, y);
  ex(x+1, y);
  ex(x, y-1);
  ex(x, y+1);
  
  return;
}

int main() {
  while(cin >> w >> h) {
    if(w == 0 && h == 0) break;
    char tmp;
    int s[2];
    for(int j = 0; j < h; j ++) {
      for(int i = 0; i < w; i ++) {
	cin >> tmp;
	map[i][j] = tmp;
	if(tmp == '@') {
	  s[0] = i;
	  s[1] = j;
	}
      }
    }
    
    ans = 0;
    //cout << s[0] << s[1] << endl;
    ex(s[0], s[1]);
    
    cout << ans << endl;
  }
  return 0;
}