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<iomanip>
#include<algorithm>
#include<string>
#include<map>
#include<cmath>
#include<queue>	
#include<functional>

using namespace std;

namespace MyLib {
#define ALL(a) a.begin(),a.end()
#define REP(i,n) for(int i=0;i<n;i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)
#define debug(x) if(1)cout<<#x<<":"<<x<<endl;
#define DEBUG(x) if(1)cout<<#x<<":"<<x<<endl;
#define ll long long
}

using namespace MyLib;

int dfs(int i, int j);
int t[20][20];
int b[20][20];
int w, h;


int main() {

	int x, y;
	string buf;
	while (cin >> w >> h, w) {
		REP(i, 20)
			fill(b[i],b[i]+20, 0);
		REP(i, h) {
			cin >> buf;
			REP(j, w) {
				if (buf[j] == '#')
					t[i][j] = 0;
				else
					t[i][j] = 1;
				if (buf[j] == '@')
					y = i, x = j;
			}
		}
		cout << dfs(y, x)-1 << endl;



	}
	
}

int dfs(int i, int j) {
	int a, x, c, d;
	a = x = c = d = 0;
	if (0 <= i - 1 && b[i - 1][j] == 0 && t[i - 1][j] == t[i][j]) {
		b[i - 1][j] = 1;
		a = dfs(i - 1, j);
	}
	if (h > i + 1 && b[i + 1][j] == 0 && t[i + 1][j] == t[i][j]) {
		b[i + 1][j] = 1;
		x = dfs(i +1, j);
	}
	if (0<= j- 1 && b[i][j-1] == 0 && t[i][j-1] == t[i][j]) {
		b[i][j-1] = 1;
		c = dfs(i , j-1);
	}
	if (w > j + 1 && b[i ][j+1] == 0 && t[i ][j+1] == t[i][j]) {
		b[i ][j+1] = 1;
		d = dfs(i , j+1);
	}
	return a + x + c + d + 1;



}

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

using namespace std;

typedef pair<int, int> pii;

int height, width;
int sx, sy;


void bfs(vector<vector<char>>& field)
{
    int ans = 1;
    array<int, 4> dx = {0, 0, 1, -1};
    array<int, 4> dy = {1, -1, 0, 0};

    queue<pii> p;
    p.push({sx, sy});
    
    while (!p.empty()) {
        pii q = p.front(); p.pop();
        
        //??????
        int cx = q.first;
        int cy = q.second;

        for (int i = 0; i < 4; ++i) {
            int nx = cx + dx.at(i);
            int ny = cy + dy.at(i);

            if (nx >= 0 && nx < width && ny >= 0 && ny < height 
                    && field.at(ny).at(nx) == '.') {

                ans++;
                field.at(ny).at(nx) = '#';
                p.push({nx, ny});
            }
        }
    }

    std::cout << ans << std::endl;
}


int main()
{
    while (cin >> width >> height) {
        if (height == 0 && width == 0) break;

        vector<vector<char>> field(height, vector<char>(width));

        for (int y = 0; y < height; ++y) {
            for (int x = 0; x < width; ++x) {
                cin >> field.at(y).at(x);

                if (field.at(y).at(x) == '@') {
                    sx = x; sy = y;
                }
            }
        }

        bfs(field);
    }

    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int W, H;
char c[100][101];
bool visited[100][100];

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

void computer(){
    memset(visited,false,sizeof visited);
    queue<int> que;
    int i, j;
    for(i=0;i<H;i++){
        for(j=0;j<W;j++){
            if(c[i][j]=='@'){
                que.push(j+i*W);
                visited[i][j] = true;
                goto Skip;
            }
        }
    }
Skip:;
    
    int answer = 1;
    while( !que.empty()){
        int cur = que.front(); que.pop();
        int x = cur % W, y = cur / W;
        
        for(i=0;i<4;++i){
            int nx = x + dx[i],ny = y + dy[i];
            if(!(0<=nx && nx < W && 0<=ny && ny<H))continue;
            if(c[ny][nx]=='#' || visited[ny][nx])continue;
            que.push(nx+ny*W);
            visited[ny][nx]=true;
            ++answer;
        }
        
        
        
    }
    
    cout << answer << endl;
    
    
}

// abcdd
// efgeg

int main(){
    while(cin >> W >> H, W==0&&H==0){
        int i;
        for(i=0;i<H;++i){
            //cin >> c[i][0];
            scanf(" %s",&c[i][0]);
        }
        computer();
        
    }
    
    return 0;
}

C++: #include<iostream>
using namespace std;
typedef pair<int, int> pii;
int dx[]={0, 0, 1, -1};
int dy[]={1, -1, 0, 0};
char fld[20][20];
bool used[20][20];
int W, H;

void rec(int i, int j){
   used[i][j]=1;
   for(int k=0; k<4; k++){
      int ni=i+dy[k];
      int nj=j+dx[k];
      if((0<=ni&&ni<H&&0<=nj&&nj<W)&&fld[ni][nj]=='#'&&used[ni][nj]==0){
         rec(ni, nj);
      }
   }
}

int main(){

   while(1){
      cin>> W>> H;
      if(!(H||W)) break;
      int sx, sy;
      for(int i=0; i<H; i++){
         for(int j=0; j<W; j++){
            cin>> fld[i][j];
            if(fld[i][j]=='@'){
               sy=i;
               sx=j;
            }
         }
      }

      for(int i=0; i<20; i++)for(int j=0; j<20; j++) used[i][j]=0;
      used[sy][sx]=1;
      rec(sy, sx);
      int ans=0;
      for(int i=0; i<H; i++)for(int j=0; j<W; j++){
         ans+=used[i][j];
      }
      cout<< ans<< endl;

   }

   return 0;
}