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;
    
    while(true){
        std::cin >> width >> height;
        if( width == 0 && height == 0 ) break;
        row.resize(width+1);
        for( int i = 0; i <= width; i++ ) row[i] = 0;

        std::string line;
        for( int h = 0; h < height; h++ ){
            std::cin >> line;
            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>
#include<cstdio>
#include<string.h>
#define rep(i,n) for(int i =0;i<n;i++)
using namespace std;

string t[21];


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


	if((x<0||y<0)||t[x][y]!='.') return;
	t[x][y]='@';	
	rep(k,4){
	dfs(y+dy[k],x+dx[k]);
}
return;
}


int main(void){

	int w,h,sx,sy,cnt;
	rep(i,20)rep(j,20){
t[i][j]=='#';	
	}

while(true){
scanf("%d %d",&w,&h);
if(!w&&!h) {break;}
rep(i,h){
cin >> t[i];
}
rep(i,h)rep(j,w){

	if(t[i][j]=='@'){	
	sx =i;
	sy =j;

t[i][j]='.';
	}

}
dfs(sy,sx);
cnt =0;

rep(i,h){ 
	rep(j,w)if(t[i][j]=='@') cnt++;	
}

cout <<endl<<cnt;;


}

return 0;
}

C++: #include <iostream>
#include <fstream>
#include <stdio.h>

using namespace std;




void map_ser(int x, int y, int w, int h, int map[][30], int *cnt){
    
    if(map[y][x] == 1){
        *cnt+=1;
        map[y][x] = 2;
        if(0<=x-1 && x-1<w && 0<=y && y<h){
            map_ser(x-1, y, w, h, map, cnt);
        }
        if(0<=x+1 && x+1<w && 0<=y && y<h){
            map_ser(x+1, y, w, h, map, cnt);
        }
        if(0<=x && x<w && 0<=y-1 && y-1<h){
            map_ser(x, y-1, w, h, map, cnt);
        }
        if(0<=x && x<w && 0<=y+1 && y+1<h){
            map_ser(x, y+1, w, h, map, cnt);
        }
    }
    else if(map[y][x] != 1){
        return;
    }
}

int main(int argc, const char * argv[]) {
    // insert code here...
    
    
    /////////
    //write//
    /////////
    
    int map[30][30];
    int cnt;
    char tmp[100];
    int at_x,at_y;
    
    while(1){
        int w,h; cin >> w >> h;
        if(w==0 && h==0)break;
        cnt = 0;
        
        for(int i=0; i<h; i++){
            cin >> tmp;
            for(int j=0; j<w; j++){
                if(tmp[j] == '.')map[i][j] = 1;
                else if(tmp[j] == '#')map[i][j] = 0;
                else if(tmp[j] == '@'){
                    at_x = j;
                    at_y = i;
                    map[i][j] = 1;
                }
            }
        }
        map_ser(at_x, at_y, w, h, map, &cnt);
        cout << cnt << endl;
    }
    
    
    return 0;
}

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

char map[21][21];
int res;
int w,h;

void dfs(int sx, int sy) {
    if(sx<0 || sx>w-1 || sy<0 || sy>h-1) return;
    if(map[sy][sx] == '#') return;

    res++;
    map[sy][sx] = '#';

    dfs(sx+1,sy);
    dfs(sx,sy+1);
    dfs(sx-1,sy);
    dfs(sx,sy-1);
    return;
}

int main(void)
{
    int px,py;

    while(true)
    {
        res = 0;
        scanf("%d%d", &w, &h);
        if(!w && !h) break;
        for(int j=0; j < h; j++)
            scanf("%s", map[j]);

        for(int j=0; j < h; j++)
            for(int i=0; i < w; i++)
                if(map[j][i] == '@')
                    py = j, px =i;
        dfs(px, py);
        printf("%d\n", res);
    }

    return 0;
}

C++: #include <iostream>

int W, H;
int sx, sy;
char map[20][20];
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
int cnt = 0;

void dfs(int y, int x){
  map[y][x] = '#';
  cnt++;
  for(int i=0; i < 4; i++){
    if(0 <= y + dy[i] && y + dy[i] < H && 0 <= x + dx[i] && x + dx[i]< W && map[y + dy[i]][x + dx[i]] == '.'){
      dfs(y + dy[i], x + dx[i]);
    }
  }
}



int main(void){
  while(1){
    std::cin >> W >> H;
    if(W == 0 && H == 0)  return 0;

    for(int i=0; i < H; i++){
      for(int j=0; j < W; j++){
        std::cin >> map[i][j];
        if(map[i][j] == '@'){
          sx = j; sy = i;
        }
      }
    }

    cnt = 0;
    dfs(sy, sx);

    std::cout << cnt << std::endl; 
  }
}