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>
#include <vector>
#include <queue>
#include <map>
#include <cstring>

using std::cin;
using std::cout;
using std::cerr;
using std::endl;

const int vx[4] = {0, 1, 0, -1}, vy[4] = {1, 0, -1, 0};

inline bool check(int x, int y, int H, int W) {
  return (0 <= x && x < H && 0 <= y && y < W);
}

int solve(int& H, int& W, std::vector<std::string>& v) {
  int ret = 1, sx, sy;

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; ++j) {
      if (v[i][j] == '@') {
        sx = i, sy = j;
      }
    }
  }

  bool is_searched[21][21];

  std::memset(is_searched, false, sizeof is_searched);

  std::queue<std::pair<int, int> > que;
  que.push(std::make_pair(sx, sy));
  is_searched[sx][sy] = true;

  while (!que.empty()) {
    auto p = que.front();
    que.pop();

    for (int i = 0; i < 4; ++i) {
      int nx = p.first + vx[i], ny = p.second + vy[i];

      if (check(nx, ny, H, W) && !is_searched[nx][ny] && v[nx][ny] == '.') {
        ret++;
        is_searched[nx][ny] = true;
        que.push(std::make_pair(nx, ny));
      }
    }
  }

  return ret;
}

int main() {
  int H, W;

  while (cin >> W >> H, W | H) {
    std::vector<std::string> v(H);

    for (int i = 0; i < H; ++i) {
      cin >> v[i];
    }

    int ans = solve(H, W, v);

    cout << ans << endl;    
  }

  return 0;
}

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

int h,w,cnt;
int color[21][21];
char m[21][21];
int dx[4] = {1,-1,0,0};
int dy[4] = {0,0,1,-1};

void dfs(int x,int y){
    color[y][x] = 1;
    for(int i=0; i<4; i++){
        int vx = x+dx[i];
        int vy = y+dy[i];
        if(m[vy][vx] == '.' && color[vy][vx] == 0 && vx<w && vx >= 0 && vy<h && vy >=0){
            dfs(vx,vy);
        }
    }
}

int main(){
    while(1){    
        cin>>w>>h;
        cnt=0;
        if(w==0 && h==0) break;
        for(int i=0; i<21; i++){
            for(int j=0; j<21; j++){
                color[i][j]  =0;
            }
        }

        int x,y;

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

        dfs(x,y);

        for(int i=0; i<h; i++){
            for(int j=0; j<w; j++){
                if(color[i][j] != 0) cnt++;
            }
        }

        cout<<cnt<<endl;
    }


}

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;
						printf("a");
					}
				}
			}

			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<cstdio>
#include<stack>

using namespace std;

struct Coord {
  int row;
  int col;
};

int solve (int W, int H);
  
int main () {
  int W, H;

  while(W!=0 && H!=0)
    {
      scanf("%d%d", &W, &H);
      if(W!=0 && H!=0)
	{
          printf("%d\n", solve(W,H));
	}
    }
  return 0;
}

int solve (int W, int H) {
  int n=0;
  char M[H+2][W+2];
  Coord start;
  for (int i=0; i<=W+1; i++) { M[0][i] = '#'; M[H+1][i] = '#'; }
  for (int i=1; i<=H; i++) { M[i][0] = '#'; M[i][W+1] = '#'; } 
  for (int i=1; i<=H; i++) {
    for (int j=1; j<=W; j++) {
      scanf(" %c", &M[i][j]);
      if (M[i][j]=='@') {
	start.row = i;
	start.col = j;
      }
    }
  }

  
  stack<Coord> S;
  S.push(start);
  while (!S.empty()) {
    Coord current = S.top(); S.pop();
    n++;
    int D1[]={1,0,-1,0}; int D2[]={0,-1,0,1};
    for (int d=0; d<4; d++) {
      Coord nc = current;
      nc.row += D1[d];
      nc.col += D2[d];
      if(M[nc.row][nc.col] != '*' && M[nc.row][nc.col] != '#' && M[nc.row][nc.col] != '@')
	{
	  S.push(nc);
	  M[nc.row][nc.col] = '*';
	}
    } 
    
  }
  return n;
}

C++: #include "bits/stdc++.h"
using namespace std;
#define int long long
#define endl '\n'
int mod=1e9+7;
int mod2=998244353;

signed main(){
  while(true){
    int h,w;
    cin>>w>>h;
    if(!h&&!w)break;
    vector<string> hw(h);
    for(int i=0;i<h;i++)cin>>hw[i];
    queue<int> x;
    queue<int> y;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
        if(hw[i][j]=='@'){
          x.push(j);
          y.push(i);
        }
      }
    }
    int ans=0;
    int px[]={1,0,-1,0};
    int py[]={0,-1,0,1};

    while(x.size()){
      int nx=x.front();
      int ny=y.front();
      x.pop();
      y.pop();
      ans++;
      for(int i=0;i<4;i++){
        if(nx+px[i]>=0&&nx+px[i]<w&&ny+py[i]>=0&&ny+py[i]<h&&hw[ny+py[i]][nx+px[i]]=='.'){
          x.push(nx+px[i]);
          y.push(ny+py[i]);
          hw[ny+py[i]][nx+px[i]]='@';
        }
      }
    }
    cout<<ans<<endl;
  }
}