Go: package main

import (
	"bufio"
	"encoding/json"
	"fmt"
	"os"
	"strconv"
	"strings"
)

var (
	sc = bufio.NewScanner(os.Stdin)
)

type Color int

const (
	ColorBlack Color = iota
	ColorRed
)

var strByColor = map[Color]string{
	ColorBlack: "black",
	ColorRed:   "red",
}

func (c Color) String() string {
	return strByColor[c]
}

type Node struct {
	ID      int
	X, Y    int
	Color   Color
	NextIDs map[int]struct{}
}

func (n *Node) String() string {
	d, _ := json.Marshal(n)
	return string(d)
}

type Map struct {
	Width, Height int
	Nodes         map[int]*Node
}

func (m *Map) GetByID(id int) *Node {
	return m.Nodes[id]
}

func (m *Map) Get(x, y int) *Node {
	return m.Nodes[m.getID(x, y)]
}

func (m *Map) SetColor(x, y int, c Color) {
	m.Nodes[m.getID(x, y)].Color = c
}

func (m *Map) getID(x, y int) int {
	return x + y*m.Width
}

func (m *Map) init() {
	for x := 1; x <= m.Width; x++ {
		for y := 1; y <= m.Height; y++ {
			id := m.getID(x, y)
			m.Nodes[id] = &Node{ID: id, X: x, Y: y}
		}
	}
	for x := 1; x <= m.Width; x++ {
		for y := 1; y <= m.Height; y++ {
			nextIDs := map[int]struct{}{}
			if x > 1 {
				nextIDs[m.getID(x-1, y)] = struct{}{}
			}
			if x < m.Width {
				nextIDs[m.getID(x+1, y)] = struct{}{}
			}
			if y > 1 {
				nextIDs[m.getID(x, y-1)] = struct{}{}
			}
			if y < m.Height {
				nextIDs[m.getID(x, y+1)] = struct{}{}
			}
			m.Get(x, y).NextIDs = nextIDs
		}
	}
}

func NewMap(w, h int) *Map {
	m := &Map{Width: w, Height: h, Nodes: map[int]*Node{}}
	m.init()
	return m
}

type Searcher struct {
	Map        *Map
	queue      []*Node
	touchedIDs map[int]struct{}
	visitedIDs map[int]struct{}
}

func NewSearcher(m *Map) *Searcher {
	return &Searcher{
		Map:        m,
		queue:      []*Node{},
		touchedIDs: map[int]struct{}{},
		visitedIDs: map[int]struct{}{},
	}
}

func (s *Searcher) Search(start *Node) {
	s.enqueue(start)

	for {
		if len(s.queue) == 0 {
			break
		}
		for nextID := range s.dequeue().NextIDs {
			n := s.Map.GetByID(nextID)
			if n.Color == ColorRed || s.hasTouched(n) {
				continue
			}
			s.enqueue(n)
		}
	}
}

func (s *Searcher) VisitedNodeCount() int {
	return len(s.visitedIDs)
}

func (s *Searcher) hasTouched(n *Node) bool {
	_, ok := s.touchedIDs[n.ID]
	return ok
}

func (s *Searcher) enqueue(n *Node) {
	s.queue = append(s.queue, n)
	s.touchedIDs[n.ID] = struct{}{}
}

func (s *Searcher) dequeue() *Node {
	n := s.queue[0]
	s.queue = s.queue[1:]
	s.visitedIDs[n.ID] = struct{}{}
	return n
}

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])

		m := NewMap(w, h)

		// Set colors
		var start *Node
		for y := 1; y <= h; y++ {
			sc.Scan()
			for i, c := range sc.Text() {
				switch c {
				case '.', '@':
					m.SetColor(i+1, y, ColorBlack)
					if c == '@' {
						start = m.Get(i+1, y)
					}
				case '#':
					m.SetColor(i+1, y, ColorRed)
				}
			}
		}

		// Search
		s := NewSearcher(m)
		s.Search(start)
		fmt.Println(s.VisitedNodeCount())
	}
}

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;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <cstring>

using std::cin;
using std::cout;
using std::cerr;
using std::endl;

const int vx[4] = {0, 1, 0, -1}, vy[4] = {1, 0, -1, 0};

inline bool check(int x, int y, int H, int W) {
  return (0 <= x && x < H && 0 <= y && y < W);
}

int solve(int& H, int& W, std::vector<std::string>& v) {
  int ret = 1, sx, sy;

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; ++j) {
      if (v[i][j] == '@') {
        sx = i, sy = j;
      }
    }
  }

  bool is_searched[21][21];

  std::memset(is_searched, false, sizeof is_searched);

  std::queue<std::pair<int, int> > que;
  que.push(std::make_pair(sx, sy));
  is_searched[sx][sy] = true;

  while (!que.empty()) {
    auto p = que.front();
    que.pop();

    for (int i = 0; i < 4; ++i) {
      int nx = p.first + vx[i], ny = p.second + vy[i];

      if (check(nx, ny, H, W) && !is_searched[nx][ny] && v[nx][ny] == '.') {
        ret++;
        is_searched[nx][ny] = true;
        que.push(std::make_pair(nx, ny));
      }
    }
  }

  return ret;
}

int main() {
  int H, W;

  while (cin >> W >> H, W | H) {
    std::vector<std::string> v(H);

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

    int ans = solve(H, W, v);

    cout << ans << endl;    
  }

  return 0;
}