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<string>
using namespace std;
int main(){
	bool dp[20][20];
	char r[20][20];
	int w,h;
	while(true){
	cin>>w>>h;
	if(w==0&&h==0)
		break;
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			cin>>r[i][j];
			dp[i][j]=false;
			if(r[i][j]=='@')
				dp[i][j]=true;
		}
	}
	int count=1,cop=1;
	while(true){
	cop=count;
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			bool t=false;
			if(i<h-1){
			if(dp[i+1][j]==true&&r[i][j]=='.')
				t=true;
			}
			if(i>0){
			if(dp[i-1][j]==true&&r[i][j]=='.')
				t=true;
			}
			if(j<w-1){
			if(dp[i][j+1]==true&&r[i][j]=='.')
				t=true;
			}
			if(j>0){
			if(dp[i][j-1]==true&&r[i][j]=='.')
				t=true;
			}
			if(t==true&&dp[i][j]==false){
				count++;
				dp[i][j]=true;
			}
		}
	}
	if(cop==count)
		break;
	}
	cout<<count<<endl;
	}
    return 0;
}

C++: #include <cstdio>

using namespace std;

int W, H;
char M[22][22];

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

int ans;

void dfs(int y, int x) {
    if (M[y][x] == '#' || M[y][x] == '@') { return; }

    ans++;
    M[y][x] = '@';
    for (int i = 0; i < 4; i++) {
        dfs(y + dy[i], x + dx[i]);
    }
}

int solve() {
    int sx, sy;
    for (sy = 1;; sy++) {
        for (sx = 1; sx <= W; sx++) {
            if (M[sy][sx] == '@') { goto found; }
        }
    }
found:

    ans = 0;
    M[sy][sx] = '.';
    dfs(sy, sx);
    return ans;
}

int main() {
    for (;;) {
        scanf("%d%d ", &W, &H);

        if (!W && !H) { break; }

        for (int i = 0; i < 22; i++) {
            for (int j = 0; j < 22; j++) {
                M[i][j] = '#';
            }
        }
        for (int i = 1; i <= H; i++) {
            for (int j = 1; j <= W; j++) {
                scanf("%c", &M[i][j]);
            }
            char d;
            scanf("%c", &d);
        }
        printf("%d\n", solve());
    }
}

C++: #include <iostream>
#include <cstdio>

using namespace std;

int width, depth;
int s_w, s_d;
bool is_visit[20 + 2][20 + 2];
char field[20 + 2][20 + 2];
int t_x[4] = {1, 0, -1, 0};
int t_y[4] = {0, -1, 0, 1};


int search(int w, int d)
{
  //  printf("(%d, %d)\n", w, d);
  if (is_visit[d][w]) return 0;
  if (field[d][w] == '#') return 0;
  is_visit[d][w] = true;
  int sum = 0;
  for (int i = 0; i < 4; i++) {
    sum += search(w + t_x[i], d + t_y[i]);
  }
  return sum + 1;
}

int main()
{
  while (scanf("%d %d ", &width, &depth) == 2) {
    if (width == 0 && depth == 0) {
      break;
    }
    for (int i = 0; i < 22; i++) {
      fill(is_visit[i], &is_visit[i][22], false);
      fill(field[i], &field[i][22], '#');
    }
    for (int i = 1; i <= depth; i++) {
      for (int j = 1; j <= width; j++) {
	scanf("%c ", &field[i][j]);
	if (field[i][j] == '@') {
	  s_w = j, s_d = i;
	}
	//	printf("%c", field[i][j]);
      }
      //      printf("\n");
    }
    
    printf("%d\n", search(s_w, s_d));
  }
}

C++: #include <iostream>
 
using namespace std;
 
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

void dfs(int x,int y);
int w,h;
 
int count;
bool flag;
char maze[21][21];
bool reached[501][501];
 
int main(){
        int w,h;

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

            for(int i=0;i<h;i++){
                for(int j=0;j<w;j++){
                    cin>>maze[i][j];
                }
            }

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

            // cout<<endl

            for(int i=0;i<h;i++){
                for(int j=0;j<w;j++){
                    if(maze[i][j]=='@'){
                        dfs(j,i);
                    }
                }
            }

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

            count=0;
            for(int i=0;i<h;i++){
                for(int j=0;j<w;j++){
                    if(maze[i][j]=='!')count++;
                }
            }

            cout<<count<<endl;




        }
    
    // int s_x=0;
    // int s_y=0;
    // int g_x=0;
    // int g_y=0;
 
    // cin>>w>>h;
 
 
    // for(int i=0;i<w;i++){
    //     for(int j=0;j<h;j++){
    //         if(maze[i][j]=='s'){
    //             s_x=i;
    //             s_y=j;
    //             dfs(i,j);
    //         }
    //     }
    // }

    // for(int i=0;i<h;i++){
    //     for(int j=0;j<w;j++){
    //         if(reached[i][j])count++;
    //     }
    // }
    

 
    return 0;
}
 
 
void dfs(int x,int y){
    maze[y][x]='!';

    for(int i=0;i<4;i++){
        int mx=x+dx[i];
        int my=y+dy[i];
        if(maze[my][mx]=='.'){
            dfs(mx,my);
        }
        if(mx<0||w<=mx||my<0||h<=my){
            continue;
        }//??????????????´?????´???->???????????????
        
    } 
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

int cnt;
int w, h;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
char tile[200][200];

void dfs(int x, int y);

int main()
{
	int x, y;

	while (true){
		scanf("%d %d", &w, &h);

		if (w == 0){
			break;
		}

		for (int i = 0; i < h; i++){
			scanf("%s", tile[i]);

			for (int j = 0; j < w; j++){
				if (tile[i][j] == '@'){
					y = i;
					x = j;
				}
			}
		}

		cnt = 0;
		dfs(x, y);

		printf("%d\n", cnt);
	}

	return (0);
}

void dfs(int x, int y)
{
	tile[y][x] = '#';
	cnt++;

	for (int i = 0; i < 4; i++){
		int nx = x + dx[i];
		int ny = y + dy[i];

		if (nx >= 0 && nx < w && ny >= 0 && ny < h && tile[ny][nx] == '.'){
			dfs(nx, ny);
		}
	}
}