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<bits/stdc++.h>
using namespace std;

using i64 = int64_t;

char room[22][22]={0};
bool visited[22][22]={0};

int di[4]={0,1,0,-1}, dj[4]={-1,0,1,0};

void init(){
    for(int i=0;i<=20;++i){
        for(int j=0;j<=20;++j){
            visited[i][j] = false;
            room[i][j] = '#';
        }
    }
}

int main(){
    int h, w;
    while(cin >> w >> h, w > 0){
        init();
        int si, sj;
        for(int i=0;i<h;++i){
            for(int j=0;j<w;++j){
                cin >> room[i][j];
                if(room[i][j] == '@'){
                    si = i;
                    sj = j;
                }
            }
        }
        queue<pair<int, int>> que;
        que.push({si, sj});
        visited[si][sj] = true;
        int ans = 0;
        while(!que.empty()){
            auto cur = que.front(); que.pop();
            ans++;
            for(int k=0;k<4;++k){
                int ni = cur.first+di[k], nj = cur.second+dj[k];
                if(ni < 0 || ni >= h || nj < 0 || nj >= w)continue;
                if(room[ni][nj] == '#')continue;
                if(visited[ni][nj])continue;
                visited[ni][nj] = true;
                que.push({ni, nj});
            }
        }
        cout << ans << endl;
    }

    return 0;
}

C++: //
//  RedAndBlack.cpp
//  
//
//  Created by ??°????????? on 2015/10/30.
//
//

#include <iostream>
using namespace std;

int w, h, num = 0, room[20][20];
int dx[] = {-1, 1, 0, 0};
int dy[] = { 0, 0,-1, 0};

void dfs(int x, int y) {
    num++;
    room[y][x] = 0;
    
    // ??????????????¢?´¢
    for (int i = 0; i < 4; i++) {
        int mx = x + dx[i];
        int my = y + dy[i];
        
        if (mx < 0 || w <= mx) continue;
        if (my < 0 || h <= my) continue;
        if (room[my][mx] == 0) continue;
        dfs(mx, my);
    }

    return;
}

int main() {
    while (cin >> w >> h, w || h) {
        int manX, manY;
        
        for (int i = 0; i < h; i++) {
            string s;
            cin >> s;
            for (int j = 0; j < w; j++) {
                if (s[j] == '.') {
                    room[i][j] = 1;
                } else if (s[j] == '#') {
                    room[i][j] = 0;
                } else if (s[j] == '@') {
                    room[i][j] = 1;
                    manX = j;
                    manY = i;
                }
            }
        }
        
        dfs(manX, manY);
        
        cout << num << endl;
        
        num = 0;
    }
    
    return 0;
}

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

using namespace std;

int main(){
    int w,h;
    while(cin >> w >> h && w!=0){
        // int t[100][100];
        int t[h+2][w+2];
        for(int i=0;i<h+2;i++){
            for(int j=0;j<w+2;j++){
                t[i][j] = 0;
            }
        }

        int start;
        for(int i=0;i<h+2;i++){
            for(int j=0;j<w+2;j++){
                if(i==0 || j==0 || i==h+1 || j==w+1)
                    t[i][j]=0;
                else{
                    char tmp; cin >> tmp;
                    if(tmp=='.'){
                        t[i][j] = 1;
                    }
                    else if(tmp=='#'){
                        t[i][j] = 0;
                    }
                    else{
                        t[i][j] = 1;
                        start = i*w+j;
                    }
                }
            }
        }

        //test
        // for(int i=0;i<h+2;i++){
        //     for(int j = 0;j<w+2;j++){
        //         if(j==0)
        //             cout << t[i][j];
        //         else
        //             cout << " " << t[i][j];
        //     }
        //     cout << endl;
        // }

        int ** matrix = new int*[(w+2)*(h+2)];
        for( int i=0; i<(w+2)*(h+2); i++ ) {
            matrix[i] = new int[(w+2)*(h+2)];
        }
        for(int i=0;i<w*h;i++){
            for(int j=0;j<w*h;j++){
                matrix[i][j] = 0;
            }
        }
        for(int i=0;i<h+2;i++){
            for(int j=0;j<w+2;j++){
                int id = i*(w+2)+j;
                if(i==0 || j==0 || i == h+1 || j == w+1 || t[i][j]==0)
                    continue;
                else{
                    if(t[(id-w-2)/(w+2)][(id-w-2)%(w+2)] == 1){
                        matrix[id][id-w-2] = 1;
                    }
                    if(t[(id+w+2)/(w+2)][(id+w+2)/(w+2)] == 1){
                        matrix[id][id+w+2] = 1;
                    }
                    if(t[(id-1)/(w+2)][(id-1)/(w+2)] == 1){
                        matrix[id][id-1] = 1;
                    }
                    if(t[(id+1)/(w+2)][(id+1)/(w+2)] == 1){
                        matrix[id][id+1] = 1;
                    }
                }
            }
        }

        // test
        // for(int i=0;i<(h+2)*(w+2);i++){
        //     for(int j = 0;j<(h+2)*(w+2);j++){
        //         if(j==0)
        //             cout << matrix[i][j];
        //         else
        //             cout << " " << matrix[i][j];
        //     }
        //     cout << endl;
        // }


        int count = 1;
        queue<int> Q;
        int came[(w+2)*(h+2)];
        for(int i=0;i<(w+2)*(h+2);i++){
            came[i] = 0;
        }
        Q.push(start);
        came[start] = 1;
        while(Q.size()!=0){
            int cur = Q.front();
            for(int i=0;i<(w+2)*(h+2);i++){
                if(matrix[cur][i]==1 && came[i] == 0){
                    Q.push(i);
                    came[i] = 1;
                    count++;
                }
            }
            Q.pop();
        }
        cout << count << endl;
        for( int i=0; i<(w+2)*(h+2); i++ ) {
            delete[] matrix[i];
        }
        delete[] matrix;

    }
}