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<stdio.h>
#include<iostream>
#include<math.h>
#include<algorithm>
#include<string>
#define pi 3.14159
using namespace std;

int a;
char map[20][20];
int w, h;
void dfs(int y, int x);

int main() {
	while(1) {
		a = 1;
		cin >> w >> h;
		if(w == 0 && h == 0) break;
		int x, y;
		for(int i = 0; i < h; i++) {
			for(int j = 0; j < w; j++) {
				cin >> map[i][j];
				if(map[i][j] == '@') {
					y = i;
					x = j;
					map[y][x] = 0;
				}
				else if(map[i][j] == '.') { map[i][j] = -1;}
			}
		}
		dfs(y+1, x);
		dfs(y, x-1);
		dfs(y, x+1);
		dfs(y-1, x);
		cout << a <<endl;
	}
	return 0;
}

void dfs(int y, int x) {
	if(y == -1 || x == -1 || y >= h || x >= w) {return ;}
	if(map[y][x] == '#') {return ;}
	if(map[y][x] == 0) {return ;}
	map[y][x] = 0;
	a++;
	dfs(y-1, x);
	dfs(y, x-1);
	dfs(y, x+1);
	dfs(y+1, x);
	return;
}

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>
#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){
    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){
        map[y][x]=false;
        n++;
        dfs(x,y,map);}
        
    }
    
}

int main(){
    int x,y;
    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);
        cout<<n<<endl;
        
    }

    return 0;
};

C++: #include <bits/stdc++.h>
using namespace std;

int W, H;
char board[30][30];
bool is[30][30];

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

void dfs(int x, int y) {
   for (int i = 0; i < 4; i++) {
      int nx = x + dx[i], ny = y + dy[i];
      if (nx < 0 || nx >= H || ny < 0 || ny >= W) continue;
      if (board[nx][ny] == '#') continue;
      if (is[nx][ny]) continue;
      is[nx][ny] = true;
      dfs(nx, ny);
   }
}

int main() {
   while (cin >> W >> H, W && H) {
      int x, y;
      for (int i = 0; i < H; i++) {
         for (int j = 0; j < W; j++) {
            cin >> board[i][j];
            if (board[i][j] == '@') {
               x = i, y = j;
            }
         }
      }

      is[x][y] = true;
      dfs(x, y);
      int cnt = 0;
      for (int i = 0; i < H; i++) {
         for (int j = 0; j < W; j++) {
            if (is[i][j]) cnt++;
         }
      }
      cout << cnt << endl;
   }

   return 0;
}

C++: #include <algorithm>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <map>
#include <set>

#define rep(i,n) for (int i = 0; i < (n); ++i)

using namespace std;

typedef long long ll;

int h, w;
char grid[30][30];
int ans;

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

void dfs(int x, int y) {
  grid[y][x] = '#';
  ans++;
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (nx < 0 || ny < 0 || nx >= w || ny >= h ) {
      continue;
    }
    if ( grid[ny][nx] == '#' ) {
      continue;
    }
    dfs(nx, ny);
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << setprecision(20) << fixed;

  int sx, sy;

  while ( true ) {
    cin >> w >> h;

    if (w == 0 && h == 0) { break; }

    rep(y, h) {
      cin >> grid[y];
    }

    rep(y, h) {
      rep(x, w) {
        if (grid[y][x] == '@') {
          sx = x; sy = y;
        }
      }
    }
    ans = 0;
    dfs(sx, sy);
    cout << ans << endl;
  }

  return 0;
}

C++: // 横型探索

#include <iostream>
#include <string>
#include <vector>
#include <queue>

using namespace std;

int w, h;
int mx[] = {0, 0, -1, 1};
int my[] = {-1, 1, 0, 0};

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

        vector<string> v(h);
        queue<int> s;

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

            // @の座標を記録
            for ( int j = 0; j < v[i].size(); j++ ) {
                if ( v[i][j] == '@' ) {
                    y = i;
                    x = j;
                }
            }
        }

        // キューにスタート地点を追加
        s.push(x);
        s.push(y);

        while(!s.empty()) {
            x = s.front(); s.pop();
            y = s.front(); s.pop();

            for ( int i = 0; i < 4; i++ ) {
                int nx = x + mx[i], ny = y + my[i];
                if ( 0 <= nx && nx < w && 0 <= ny && ny < h && v[ny][nx] == '.' ) {
                    v[ny][nx] = '#';
                    s.push(nx);
                    s.push(ny);
                }
            }
            cnt++;
        }
        cout << cnt << endl;
    }
    return 0;
}