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>

int H, W;
int map[20][20];
int c;
using namespace std;

void printArray();
void check(int, int);

int main() {
  cin  >> W >> H;
  int sy, sx;
  for (int i = 0; i < H; i++) {
    string line;
    cin >> line;
    for (int j = 0; j < W; j++) {
      if (line[j] == '@') {
        sy = i;
        sx = j;
        map[i][j] = 0;
      } else if (line[j] == '.') {
        map[i][j] = 0;
      } else if (line[j] == '#') {
        map[i][j] = 1;
      }
    }
  }
  // printArray();
  c = 0;
  check(sy, sx);
  // printArray();
  cout << c << endl;
  return 0;
}

void check(int y, int x) {
  if (y < 0 || x < 0 || y >= H || x >= W || map[y][x] == 1) {
    return;
  }
  map[y][x] = 1;
  c++;
  check(y + 1, x);
  check(y - 1, x);
  check(y, x + 1);
  check(y, x - 1);
}

void printArray() {
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cout << map[i][j];
    }
    cout << endl;
  }
}

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

int ans = 0,w,h;
string s[100];

void solve(int x,int y){

  if(s[x][y] == '#') return;
  s[x][y] = '#';
  ans++;

  if(x+1 < h) solve(x+1,y);
  if(y+1 < w) solve(x,y+1);
  if(x-1 >= 0) solve(x-1,y);
  if(y-1 >= 0) solve(x,y-1);
   
  return;
}


int main(){

  while(true){
    cin >> w >> h;
    if(!w && !h) break;

    ans = 0;

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

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

    cout << ans << endl;
  }
}

C++: //Shunji Lin 08-144505
#include <iostream>
#include <string>

using namespace std;

int reachable = 0;

void DFS(bool grid[20][20], bool visited[20][20],  int row_pos, int col_pos, int H, int W);

void DFS_aux(bool grid[20][20], bool visited[20][20], int row_pos, int col_pos, int H, int W) {
  if (grid[row_pos][col_pos] && !visited[row_pos][col_pos]) {
    DFS(grid, visited, row_pos, col_pos, H, W);
  }
    
}
	 
void DFS(bool grid[20][20], bool visited[20][20], int row_pos, int col_pos, int H, int W) {
  visited[row_pos][col_pos] = true; //mark as visited
  reachable += 1; // increase reachable count

  //do all four actions (up, left, down, right), unless corner cases
  if (!(row_pos == 0)) {
    DFS_aux(grid, visited, row_pos-1, col_pos, H, W); //up
  }
  if (!(col_pos == 0)) {
    DFS_aux(grid, visited, row_pos, col_pos-1, H, W); //left
  }
  if (!(row_pos == H-1)) {
    DFS_aux(grid, visited, row_pos + 1, col_pos, H, W); //down
  }
  if (!(col_pos == W-1)) {
    DFS_aux(grid, visited, row_pos, col_pos + 1, H , W); //right
  }
}


int main() {
  while (true) {
    bool grid[20][20] = {{false}};
    bool visited[20][20] = {{false}};
    reachable = 0;
    int man_row = -1, man_col = -1;
    int W, H;
    cin >> W >> H;
    if (W == 0 && H == 0) break;
    for (int i = 0; i < H; i++) {
      string line;
      cin >> line;
      for (int j = 0; j < W; j++) {
	if (line[j] == '@') {
	  grid[i][j] = true;
	  man_row = i;
	  man_col = j;
	} else if (line[j] == '.') {
	  grid[i][j] = true;
	}
      }
    }
    /*
    //print out grid
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
	if (grid[i][j] == true) {
	  cout << 'b';
	} else if (grid[i][j] == false) {
	  cout << 'r';
	} 
      }
      cout << endl;
    }
    */
    
    DFS(grid, visited,  man_row, man_col, H, W );
    cout << reachable << endl;
  }
}

C++: #include <iostream>

using namespace std;

const int dx[]={1,0,-1,0}, dy[]={0,-1,0,1};
int H,W,count=0;
char **map;
bool **isCount;

bool valid(int x,int y)
{
    return x<=(W-1)&&x>=0&&y<=(H-1)&&y>=0;
}

void bfs(int x,int y)
{
    if(valid(x,y)&&map[y][x]!='#' && !isCount[y][x])
    {
        count++;
        isCount[y][x]=true;
        for(int i=0;i<4;i++)
            bfs(x+dx[i],y+dy[i]);
    }
  /*int D[100];
  for(int i=0;i<100;i++)
    D[i]=-1;
  queue<int>Q;
  Q.push(src);
  D[src]=0;
  while(!Q.empty())
    {
      int cur=Q.front();
      Q.pop();
       for(int dst=0;dst<n;dst++)
	{
	  if(M[cur][dst]==1&&D[dst]==-1)
	    {
	      D[dst]=D[cur]+1;
	      Q.push(dst);
	    }
    }
    }
    for(int i=0;i<n;i++)
        cout<<i+1<<' '<<D[i]<<endl;
        */
}



int main()
{
    int x,y;
    cin>>W>>H;
    while(W!=0&&H!=0)
    {
        isCount=new bool*[H];
        map=new char*[H];
        for(int i=0;i<H;i++)
        {
            isCount[i]=new bool[W];
            map[i]=new char[W];
        }
        for(int i=0;i<H;i++)
        {
            for(int j=0;j<W;j++)
                isCount[i][j]=false;
        }
        for(int i=0;i<H;i++)
        {
            for(int j=0;j<W;j++)
            {
                cin>>map[i][j];
                if(map[i][j]=='@')
                {
                    x=j;
                    y=i;
                }
            }
        }
        bfs(x,y);
        cout << count << endl;
        delete map;
        delete isCount;
        cin>>W>>H;
    }
    return 0;
}

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

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

int dfs(int y, int x, vector<string> &g){
  if(g[y][x] == '.')return 0;
  g[y][x] = '.';

  int res = 1;
  for(int i=0;i<4;i++){
    int ny = y + dy[i], nx = x + dx[i];
    if(ny<0 || nx<0 || ny>=(int)g.size() || nx>=(int)g[0].size())continue;
    res += dfs(ny,nx,g);
  }
  return res;
}

int main(){
  int w,h;
  while(cin >> w >> h){
    if(w==0 && h==0)break;

    vector<string> g(h);
    int y=0,x=0;
    for(int i=0;i<h;i++){
      cin >> g[i];
      for(int j=0;j<w;j++){
	if(g[i][j] == '@')y = i, x = j;
      }
    }

    cout << dfs(y,x,g) << endl;
  }
}