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++: //C++14 (Clang 3.8.0)

#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string>
//#include<fstream>

#define rp(i,a,b) for(int (i)=(int)(a);i<(int)(b);++i)


typedef long long ll;

int mx[]={-1,1,0,0};
int my[]={0,0,-1,1};
using namespace std;
int n=0;
void dfs(int startx,int starty,vector<vector<bool> > &map){
    n++;
    map[starty][startx]=false;
    for(int i=0;i<4;i++){
        int x=startx+mx[i],y=starty+my[i];
        if(x>=0 and y>=0 and y<map.size() and x<map[0].size() and map[y][x]==true){
        dfs(x,y,map);}
    }
    
}

int main(){
    int x,y;
    //ofstream outputfile("test.txt");
    while(cin>>x>>y,x|y){
        int startx,starty;
        vector<vector<bool> > map;

        if(x==0 and y==0) return 0;
        for(int i=0;i<y;i++){
            vector<bool> tmp;
            string s;
            cin>>s;
            for(int j=0;j<x;j++){

                if(s[j]=='@') {startx=j;starty=i;tmp.push_back(true);}
                else if(s[j]=='.') tmp.push_back(true);
                else tmp.push_back(false);    
            }
            map.push_back((tmp));
        }
        n=0;
        dfs(startx,starty,map);
        //outputfile<<n;
        //outputfile<<"\n";
        cout<<n<<endl;
        
    }
    //outputfile.close();
    return 0;
};

C++: #include <iostream>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)

char table[20][20];
	int W,H;

void dfs(int x,int y){
	if(table[x][y]!= '#')
		table[x][y]='#';
	int dx[4]={1,0,-1,0};
	int dy[4]={0,1,0,-1};
	rep(i,4)
		if(0<=x+dx[i]&&x+dx[i]<W&&0<=y+dy[i]&&y+dy[i]<H&&table[x+dx[i]][y+dy[i]]!='#')
			dfs(x+dx[i],y+dy[i]);
	return ;
}


int main(void){
	while(cin >> W >> H,W|H){
		int pre=0,sx,sy;
		rep(y,H){
			rep(x,W){
				cin >> table[x][y];
				if(table[x][y]=='#')
					pre++;
				if(table[x][y]=='@')
					sx=x,sy=y;
			}
		}
		dfs(sx,sy);
		int post=0;
		rep(y,H)
			rep(x,W)
				if(table[x][y]=='#')
					post++;
		cout << post-pre << endl;
	}
	return 0;
}

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

using namespace std;

struct locate{
  int x;
  int y;
};
 
char graph[20][20];
int cou = 0;
int H,W;

void bfs(locate z){
  queue<locate> Q;
  locate u;
  locate v;
  Q.push(z);
 

  while(!Q.empty()){

    u = Q.front();Q.pop();
    if(u.y < H - 1){
      if(graph[u.y + 1][u.x] == '.' && graph[u.y + 1][u.x] != 'N'){
	graph[u.y + 1][u.x] = 'N';
	cou++;
	v.y = u.y + 1;
	v.x = u.x;
	Q.push(v);
      }
    }
    if(u.x < W - 1){
      if(graph[u.y][u.x + 1] == '.' && graph[u.y][u.x + 1] != 'N'){
	graph[u.y][u.x + 1] = 'N';
	cou++;
	v.x  = u.x + 1;
	v.y = u.y;
	Q.push(v);
      }
    }
    if(u.y > 0){
      if(graph[u.y - 1][u.x] == '.' && graph[u.y - 1][u.x] != 'N'){
	graph[u.y - 1][u.x] = 'N';
	cou++;
	v.x = u.x;
	v.y = u.y - 1;
	Q.push(v);
      }
    }
    if(u.x > 0){
      if(graph[u.y][u.x - 1] == '.' && graph[u.y][u.x - 1] != 'N'){
	graph[u.y][u.x - 1] = 'N';
	cou++;
	v.x = u.x - 1;
	v.y = u.y;
	Q.push(v);
      }
    }

    /* cout << endl;
       for(int i = 0; i< H ;i++){
       for(int j = 0; j < W; j++){
       cout << graph[i][j];
       }
       cout << endl;*/
  }
}
  


int main(){

  int a1,a2;
  locate a;
  while(1){

    cin >> W >> H;

    if(W == 0 && H == 0) break; 


    for(int i = 0; i< H ;i++){
      for(int j = 0; j < W; j++){
	cin >> graph[i][j];
	if(graph[i][j] == '@'){
	  a1 = i;
	  a2 = j;
	}
      }
    }

    /*
      for(int i = 0; i< H ;i++){
      for(int j = 0; j < W; j++){
      cout << graph[i][j];
      }
      cout << endl;
      }
    */

    a.y = a1;
    a.x = a2;
    bfs(a);

    cout << cou+1 << endl;

    cou = 0;
    for(int i = 0;i < H ;i++){
      for(int j = 0;j < W; j++){
	graph[i][j] = 'N';
      }
    }
    cou = 0;
  }
  return 0;
}

C++: #include<iostream>

using namespace std;

int count;
int W = 0, H = 0;
char tyle[22][22];


void check(int x, int y);


int main(){

	int i, j;
	int startH, startW;


	while(1){

		count = 0;

		cin >> W >> H;

		if(W != 0 && H != 0){

			for(i = 0; i <= W; i++){
				tyle[i][0] = '#';
				tyle[i][H+1] = '#';
			}

			for(i = 0; i <= H; i++){
				tyle[0][i] = '#';
				tyle[W+1][i] = '#';
			}

			for(i = 1; i <= H; i++){
				for(j = 1; j <= W; j++){
					cin >> tyle[j][i];
					if(tyle[j][i] == '@'){
						startH = i;
						startW = j;
					}
				}
			}


			check(startW, startH);

			cout << count << endl;

		}

		else{break;}

	}

	return 0;
}




void check(int x, int y){

	count++;

	tyle[x][y] = '#';

	if(tyle[x][y-1] == '.'){
		check(x, y-1);
	}
	if(tyle[x-1][y] == '.'){
		check(x-1, y);
	}
	if(tyle[x][y+1] == '.'){
		check(x, y+1);
	}
	if(tyle[x+1][y] == '.'){
		check(x+1, y);
	}
}

C++: #include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
typedef long long ll;
int H, W;
bool visited[20][20];
int calc(vector<string> &vs, int y, int x) {
  if(y < 0 || y >= H || x < 0 || x >= W) return 0;
  if(visited[y][x] || vs[y][x] == '#') return 0;
  visited[y][x] = true;
  int ret = 1;
  ret += calc(vs, y+1,x);
  ret += calc(vs, y-1,x);
  ret += calc(vs, y,x+1);
  ret += calc(vs, y,x-1);
  return ret;
}

int main()
{
  while(cin >> W >> H) {
    if(W == 0) return 0;
    vector<string> vs(H);
    FOR(i,0,H) cin >> vs[i];
    int sx, sy;
    FOR(y,0,H) {
      FOR(x,0,W) {
        if(vs[y][x] == '@') {
          sx = x;
          sy = y;
        }
      }
    }
    FOR(i,0,H) FOR(j,0,W) visited[i][j] = false;
    cout << calc(vs, sy, sx) << endl;
  }
  return 0;
}