Go: package main

import (
	"bufio"
	"encoding/json"
	"fmt"
	"os"
	"strconv"
	"strings"
)

var (
	sc = bufio.NewScanner(os.Stdin)
)

type Color int

const (
	ColorBlack Color = iota
	ColorRed
)

var strByColor = map[Color]string{
	ColorBlack: "black",
	ColorRed:   "red",
}

func (c Color) String() string {
	return strByColor[c]
}

type Node struct {
	ID      int
	X, Y    int
	Color   Color
	NextIDs map[int]struct{}
}

func (n *Node) String() string {
	d, _ := json.Marshal(n)
	return string(d)
}

type Map struct {
	Width, Height int
	Nodes         map[int]*Node
}

func (m *Map) GetByID(id int) *Node {
	return m.Nodes[id]
}

func (m *Map) Get(x, y int) *Node {
	return m.Nodes[m.getID(x, y)]
}

func (m *Map) SetColor(x, y int, c Color) {
	m.Nodes[m.getID(x, y)].Color = c
}

func (m *Map) getID(x, y int) int {
	return x + y*m.Width
}

func (m *Map) init() {
	for x := 1; x <= m.Width; x++ {
		for y := 1; y <= m.Height; y++ {
			id := m.getID(x, y)
			m.Nodes[id] = &Node{ID: id, X: x, Y: y}
		}
	}
	for x := 1; x <= m.Width; x++ {
		for y := 1; y <= m.Height; y++ {
			nextIDs := map[int]struct{}{}
			if x > 1 {
				nextIDs[m.getID(x-1, y)] = struct{}{}
			}
			if x < m.Width {
				nextIDs[m.getID(x+1, y)] = struct{}{}
			}
			if y > 1 {
				nextIDs[m.getID(x, y-1)] = struct{}{}
			}
			if y < m.Height {
				nextIDs[m.getID(x, y+1)] = struct{}{}
			}
			m.Get(x, y).NextIDs = nextIDs
		}
	}
}

func NewMap(w, h int) *Map {
	m := &Map{Width: w, Height: h, Nodes: map[int]*Node{}}
	m.init()
	return m
}

type Searcher struct {
	Map        *Map
	queue      []*Node
	touchedIDs map[int]struct{}
	visitedIDs map[int]struct{}
}

func NewSearcher(m *Map) *Searcher {
	return &Searcher{
		Map:        m,
		queue:      []*Node{},
		touchedIDs: map[int]struct{}{},
		visitedIDs: map[int]struct{}{},
	}
}

func (s *Searcher) Search(start *Node) {
	s.enqueue(start)

	for {
		if len(s.queue) == 0 {
			break
		}
		for nextID := range s.dequeue().NextIDs {
			n := s.Map.GetByID(nextID)
			if n.Color == ColorRed || s.hasTouched(n) {
				continue
			}
			s.enqueue(n)
		}
	}
}

func (s *Searcher) VisitedNodeCount() int {
	return len(s.visitedIDs)
}

func (s *Searcher) hasTouched(n *Node) bool {
	_, ok := s.touchedIDs[n.ID]
	return ok
}

func (s *Searcher) enqueue(n *Node) {
	s.queue = append(s.queue, n)
	s.touchedIDs[n.ID] = struct{}{}
}

func (s *Searcher) dequeue() *Node {
	n := s.queue[0]
	s.queue = s.queue[1:]
	s.visitedIDs[n.ID] = struct{}{}
	return n
}

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])

		m := NewMap(w, h)

		// Set colors
		var start *Node
		for y := 1; y <= h; y++ {
			sc.Scan()
			for i, c := range sc.Text() {
				switch c {
				case '.', '@':
					m.SetColor(i+1, y, ColorBlack)
					if c == '@' {
						start = m.Get(i+1, y)
					}
				case '#':
					m.SetColor(i+1, y, ColorRed)
				}
			}
		}

		// Search
		s := NewSearcher(m)
		s.Search(start)
		fmt.Println(s.VisitedNodeCount())
	}
}

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 < w*h; i++) {
					if (p[i / h][i%h] == 0 && (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 < 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;
						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<cstdio>
#include<vector>

using namespace std;

int w,h;
int ans=1;
char tile[21][21];

void saiki(int x,int y){
	if(x<0 | x>h | y<0 | y>w)return;
	tile[x][y]='#';
	if(tile[x-1][y]=='.')ans++,saiki(x-1,y);
	if(tile[x+1][y]=='.')ans++,saiki(x+1,y);
	if(tile[x][y-1]=='.')ans++,saiki(x,y-1);
	if(tile[x][y+1]=='.')ans++,saiki(x,y+1);
}

int main(void){
	int a,b; 
	while(1){
		scanf("%d %d",&w,&h);
		if(w==0 && h==0)break;
		ans=1;
		memset(tile,0,sizeof(tile));
		for(a=0;a<h;a++){
			scanf("%s",tile[a]);
		}
		for(a=0;a<h;a++){
			for(b=0;b<w;b++){
				if(tile[a][b]=='@')saiki(a,b);
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}

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

char rb[21][21];
bool ch[21][21]={0};
int x,y,h,w,ans;
int d[4]={0,1,0,-1};

void solve(int nx,int ny);

int main(){
  int i,j;
  while(1){
    cin >> w >> h;
    if(w+h==0)break;
    ans = 0;
    for(i=0;i<h;i++)for(j=0;j<w;j++)ch[i][j]=0;
    for(i=0;i<h;i++)for(j=0;j<w;j++){
	cin >> rb[i][j];
	if(rb[i][j]=='@'){
	  x=i;
	  y=j;
	  ch[i][j]=1;
	  rb[i][j]='#';
	}
      }
    solve(x,y);
    cout << ans+1 << endl;
  }
  return 0;
}

void solve(int nx,int ny){
  int nex,ney,i;
  for(i=0;i<4;i++){
    nex = nx+d[i];
    ney = ny+d[i^1];
    if(nex>=0&&nex<h&&ney>=0&&ney<w&&rb[nex][ney]=='.'&&ch[nex][ney]==0){
      ans++;
      ch[nex][ney]=1;
      rb[nex][ney]='#';
      solve(nex,ney);
    }
  }
}

C++: #include<iostream>

using namespace std;

int main(void) {
	int x, y, ans;
	bool bit;
	
	while(1) {
		cin >> x >> y;
		if(x == 0 && y == 0) { break; }
		char map[y][x];
		
		for(int i=0; i<y; i++) { cin >> map[i]; }
		do {
			bit = false;
			for(int i=0; i<y; i++) {
				for(int j=0; j<x; j++) {
					if(map[i][j] == '@') {
						if(j>0 && map[i][j-1] != '#' && map[i][j-1] != '@') { map[i][j-1] = '@'; bit = true; }
						if(i>0 && map[i-1][j] != '#' && map[i-1][j] != '@') { map[i-1][j] = '@'; bit = true; }
						if(j<x-1 && map[i][j+1] != '#' && map[i][j+1] != '@') { map[i][j+1] = '@'; bit = true; }
						if(i<y-1 && map[i+1][j] != '#' && map[i+1][j] != '@') { map[i+1][j] = '@'; bit = true; }
					}
				}
			}
		} while(bit);
		ans = 0;
		for(int i=0; i<y; i++) {
			for(int j=0; j<x; j++) {
				if(map[i][j] == '@') { ans++; }
			}
		}
		cout << ans << endl;
	}
	
	return 0;
}

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

using namespace std;

struct node{
  node(){};
  int x,y;
  node(int y,int x):y(y),x(x){}
};

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

int main(){
  int w,h,i,j;
  while(cin >> w >> h && w){
    char tile[h][w];
    int ans = 0;
    struct node now,next;
    queue<struct node> que;
    for(i=0;i<h;i++){
      for(j=0;j<w;j++){
        cin >> tile[i][j];
        if(tile[i][j]=='@'){
          que.push(node(i,j));
          tile[i][j]='#';
        }
      }
    }
    while(!que.empty()){
      now = que.front();
      ans++;
      que.pop();
      for(i=0;i<4;i++){
        int ddx = dx[i] + now.x;
        int ddy = dy[i] + now.y;
        if(ddx>w-1 || ddx<0 || ddy>h-1 || ddy<0)continue;
        if(tile[ddy][ddx]=='.'){
          que.push(node(ddy,ddx));
          tile[ddy][ddx] = '#';
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}