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>

#define ll long long

using namespace std;


int W,H;

char r[1000][1000];

int sh,sw;
int bnum=0;

int solve(int i,int j){
    int ans=0;

    if(i <0 || H <= i || j < 0 || W <= j)
        return 0;
    if(r[i][j] == '#'){
        return 0;
    }
    ans = 1;
    r[i][j] = '#';
    ans += solve(i-1,j);
    ans += solve(i+1,j);
    ans += solve(i,j-1);
    ans += solve(i,j+1);

    return ans;
}


int main(){

    while(1){
        scanf("%d %d",&W,&H);
        if(W == 0 && H == 0) break;

        for(int i=0;i<H;i++){
            for(int j=0;j<W;j++){
                cin >> r[i][j];
                if(r[i][j] == '@'){
                    sh = i;
                    sw = j;
                    r[i][j] = '.';
                }
            }
        }
        cout << solve(sh,sw) << endl;
    }

}

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

int h,w;
char s[20][20];

void dfs(int y,int x){
	if((x<0)||(w<=x)||(y<0)||(h<=y)){
		return;
	}
	if((s[y][x]=='.')||(s[y][x]=='@')){
		s[y][x]='x';
	}else{
		return;
	}
	dfs(y,x+1);
	dfs(y,x-1);
	dfs(y+1,x);
	dfs(y-1,x);
}

int main(){
	while(cin >> w >> h,h){
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				cin >> s[i][j];
			}
		}
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(s[i][j]!='@');
				else{
					dfs(i,j);
					j=w;
					i=h;
				}
			}
		}
		int count=0;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(s[i][j]=='x')
					count++;
			}
		}
		cout << count << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;

int w, h, ans;
int d[4][2] = { { 1, 0 }, { -1, 0 }, {0, 1}, {0, -1} };
char map[20][20];

void search_dfs(int x, int y){
	if( x < 0 || x >= w || y < 0 || y >= h ) return;
	if( map[y][x] != '.' ) return;
	ans ++;
	map[y][x] = '*';
	for(int i = 0; i < 4; i++)
		search_dfs( x + d[i][0], y + d[i][1] );
}

int main(void){

	while( 1 ) {
		cin >> w >> h;
		if( w == 0 && h == 0 ) break;
		ans = 0;

		for(int i = 0; i < h; i++)
			cin >> map[i];

		for(int i = 0; i < h; i++)
			for(int j = 0; j < w; j++)
				if( map[i][j] == '@' ) {
					map[i][j] = '.';
					search_dfs(j, i);
				}

		cout << ans << endl;
	}

	return 0;
}

C++: #ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
// #include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif

using namespace std;
typedef pair<int, int> P;

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

int main()
{
  int N, M;
  int sx, sy;
  while (cin >> M >> N)
  {
    if (M == 0 && N == 0)
      return 0;
    int count = 0;
    queue<P> que;
    char d[21][21];
    for (int i = 0; i < N; i++)
    {
      for (int j = 0; j < M; j++)
      {
        cin >> d[i][j];
        if (d[i][j] == '@')
        {
          sx = j;
          sy = i;
          d[i][j] = '#';
          count++;
        }
      }
    }
    que.push(P(sx, sy));

    while (que.size())
    {
      P p = que.front();
      que.pop();
      for (int i = 0; i < 4; i++)
      {
        int nx = p.first + dx[i], ny = p.second + dy[i];
        if (0 <= nx && nx < M && 0 <= ny && ny < N && d[ny][nx] != '#')
        {
          que.push(P(nx, ny));
          d[ny][nx] = '#';
          count++;
        }
      }
    }
    cout << count << endl;
  }
  return 0;
}

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

using namespace std;

vector<string> v;
int w, h;

int dfs(int i, int j) {
	int cnt = 0;
	if (i < 0 || j < 0 || i >= h || j >= w) return 0;
	if (v[i][j] == '#') return 0;
	v[i][j] = '#';
	cnt++;
	cnt += dfs(i, j - 1) + dfs(i, j + 1) + dfs(i - 1, j) + dfs(i + 1, j);
	return cnt;
}

int main()
{
	int di, dj;

	while (cin >> w >> h, w) {
		v = vector<string>(h);
		for (int i = 0; i < h; i++) {
			cin >> v[i];
			for (int j = 0; j < w; j++) {	
				if (v[i][j] == '@') {
					di = i;
					dj = j;
				} 
			}
		}
		cout << dfs(di, dj) << endl;
	}
	return 0;
}