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<vector>
#include<string>


int main(){
    std::vector<int> row;
    int prev_col, width, height, max = 0, last = 0;
    bool connect_prev_row;
    
    std::cin >> width >> height;
    row.resize(width+1);
    for( int i = 0; i <= width; i++ ) row[i] = 0;

    std::string line;
    while( true ){
        std::cin >> line;
        if( line == "00" ) {
            std::cout << max;
            break;
        }
        connect_prev_row = false;
        for(std::string::size_type i = 0; i < line.size(); i++ ){
            if( line[i] == '.' ){
                if( connect_prev_row ){
                    if( row[i] > 0 ) row[i+1] = row[i] + 1;
                    else row[i+1] = row[i] - 1;
                } else {
                    connect_prev_row = true;
                    if( row[i] <= 0 && row[i+1] <= 0 ) row[i+1] = row[i+1] + row[i] - 1;
                    else row[i+1] = std::abs(row[i+1]) + std::abs(row[i]) + 1;
                }
            } else if( line[i] == '@' ) {
                if ( connect_prev_row ) row[i+1] = - row[i] + 1;
                else {
                    connect_prev_row = true;
                    row[i+1] = - row[i+1] - row[i] + 1;
                }
            } else {
                row[i+1] = 0;
                connect_prev_row = false;
            }
            max = max > row[i+1] ? max : row[i+1];
        }
        last = 0;
        for( int i = line.size(); i > 0; i-- ) {
            if( row[i] == 0 ) last = 0;
            else if( row[i] > 0 ){
                if(  last == 0 ) last = row[i];
                else row[i] = last;
            } else {
                if(  last == 0 ) last = row[i];
                else row[i] = last;
            } 
        }
    }
    return 0;
}

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

char a[22][22];
int w, h;
int yy[4] = {-1, 0, 0, 1}, xx[4] = {0, 1, -1, 0};
int solve(int y, int x){
	if(a[y][x] == '#')return 0;
	a[y][x] = '#';
	int ans = 0;
	for(int i = 0;i < 4;i++){
		ans += solve(y+yy[i], x+xx[i]);
	}
	return ans+1;
}

int main(){
	int y, x;
	while(cin >> w >> h, w){
		fill((char*)a, (char*)(a+22), '#');
		for(int i = 1;i <= h;i++)
		for(int j = 1;j <= w;j++){
			cin >> a[i][j];
			if(a[i][j] == '@'){y = i;x = j;}
		}
		cout << solve(y, x) << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <stack>
#include <queue>
using namespace std;

#define REP(i, b, n) for (int i = (b); i < (n); i++)
#define rep(i, n) REP(i, 0, n)

#define IS_IN_RANGE(x, w, y, h) ((0 <= x) && (x < w) && (0 <= y) && (y < h))

#define MAX 21
int w, h;
char brd[MAX][MAX];

int sx, sy;

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

int solve(int y, int x)
{
	if (!IS_IN_RANGE(y, h, x, w)) return 0;
	if (brd[y][x] == '#') return 0;
	brd[y][x] = '#';
	int ans = 1;
	rep(i, 4)
		ans += solve(y+dy[i], x+dx[i]);
	return ans;
}

int main()
{
	while (cin >> w >> h, w|h) {
		rep(j, h) rep(i, w) {
			cin >> brd[j][i];
			if (brd[j][i] == '@') {
				sy = j, sx = i;
				brd[j][i] = '.';
			}
		}
		cout << solve(sy, sx) << endl;
	}
	return 0;
}

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

const int dx[] = {1,0,-1,0};
const int dy[] = {0,-1,0,1};
int W,H;
int q[22][22] = {};
char p[22][22];

int main() {
	queue<pair<int,int> > M;
	while(cin >> W >> H){
		if (W == 0) return 0;
		int count = 0;
		for (int i = 1; i <= H; i++){
			for (int j = 1; j <= W; j++){
				cin >> p[i][j];
				if (p[i][j] == '@' ){
					M.push(make_pair(i,j));
					count = 1;
					q[i][j] = 1;
				}
			}
		}
		while(!M.empty()){
			W = M.front().first;
			H = M.front().second;
			M.pop();
			for (int i = 0; i <= 3; i++)
			{		
				if (p[W+dx[i]][H+dy[i]] == '.' && q[W+dx[i]][H+dy[i]] == 0)
				{
					M.push(make_pair(W + dx[i], H + dy[i]));
					q[W + dx[i]][H + dy[i]] = 1;
					count += 1;
				}
			}
		}
		cout << count << endl;
	}
}

C++: #include<iostream>
#include<vector>
#include<cstdio>
#include<string>
#include<algorithm>
#include<numeric>
#include<map>
#include<math.h>

using namespace::std;

char gMap[22][22];
bool MapFlg[22][22];
int gRedCount;
void initMap(void)
{
  for(int i = 0; i < 22; i++)
    {
      for(int j = 0; j < 22; j++)
	{
	  gMap[i][j] = '#';
	  MapFlg[i][j] = false;
	}
    }
  gRedCount = 0;
}

void showMap(void)
{
  for(int i = 0; i < 22; i++)
    {
      for(int j = 0; j < 22; j++)
	{
	  cout << gMap[i][j];
	}
      cout << endl;
    } 
}

int countMap(int x , int y)
{
  int dx[] = { 0  , 1 , -1 , 0 };
  int dy[] = { -1 , 0 ,  0 , 1 };

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

      if( gMap[my][mx] != '@' && gMap[my][mx] != '#' && MapFlg[my][mx] == false)
	{
	  gRedCount++;
	  MapFlg[my][mx] = true;
	  countMap(mx , my);
	}
    }

  return gRedCount;

}

void calcAns(int sx, int sy)
{
  cout << countMap(sx,sy) + 1 << endl;
}

int main(void)
{

  int w,h;

  while( cin >> w >> h  && (w != 0 && h != 0))
    {
      int sx,sy;
      initMap();
      for(int i = 1; i <= h; i++)
	{
	  for(int j = 1; j <= w; j++)
	    {
	      cin >> gMap[i][j];
	      if( gMap[i][j] == '@')
		{
		  sy = i;
		  sx = j;
		}
	    }
	}
      calcAns(sx,sy);
    }

  return 0;
}