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>
#include<vector>
#include<queue>
#include<string>


int main(){
    std::vector<int> tiles;
    std::queue<int> unvisited_tile_q;
    int width, height, counter, pos;
    std::string line;

    while(true){
        std::cin >> width >> height;
        if( width == 0 && height == 0 ) break;
        tiles.resize(width*height);
        pos = 0;
        for( int h = 0; h < height; h++ ){
            std::cin >> line;
            for(std::string::iterator it = line.begin(); it != line.end(); ++it) {
                switch(*it){
                    case '#':
                        tiles[pos] = 0;
                        break;
                    case '@':
                        unvisited_tile_q.push(pos);
                    case '.':
                        tiles[pos] = 1;
                }
                pos++;
            }
        }

        counter = 0;
        while( !unvisited_tile_q.empty() ){
            pos = unvisited_tile_q.front();
            unvisited_tile_q.pop();
            if( tiles[pos] == 1 ){
                counter++;
                tiles[pos] = -1;
            } else continue;
            //left
            if ( pos - 1 >= 0 && pos % width > 0 && tiles[pos-1] == 1 ) unvisited_tile_q.push(pos-1); 
             //right
            if ( pos + 1 <= tiles.size() && pos % width < width - 1 && tiles[pos+1] == 1 ) unvisited_tile_q.push(pos+1);
            //up
            if ( pos - width >= 0 && tiles[pos-width] == 1 ) unvisited_tile_q.push(pos-width);
            //down
            if ( pos + width < tiles.size() && tiles[pos+width] == 1 ) unvisited_tile_q.push(pos+width);
        }
	    //output result
	    std::cout << counter << std::endl;
    }   
    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <tuple>
#include <queue>

using namespace std;

class Solver {
public:
	static void solve() {
		int w, h;
		cin >> w >> h;
		vector<int> res;
		while (w > 0 && h > 0) {
			vector<vector<bool>> grid(h, vector<bool>(w, true));
			tuple<int, int> start;
			for (int i = 0; i < h; i++) {
				string s;
				cin >> s;
				for (int j = 0; j < w; j++) {
					if (s[j] == '#')grid[i][j] = false;
					if (s[j] == '@')start = make_tuple(i, j);
				}
			}

			queue<tuple<int, int>> que;
			que.push(start);
			int cnt = 0;
			
			while (!que.empty()) {
				auto pos = que.front();
				que.pop();
				int y = get<0>(pos);
				int x = get<1>(pos);
				if (y < 0 || h <= y || x < 0 || w <= x || !grid[y][x])continue;

				grid[y][x] = false;
				cnt++;
				que.push(make_tuple(y - 1, x));
				que.push(make_tuple(y + 1, x));
				que.push(make_tuple(y, x - 1));
				que.push(make_tuple(y, x + 1));
			}
			res.emplace_back(cnt);

			cin >> w >> h;
		}

		for (int i = 0; i < res.size(); i++) {
			cout << res[i] << endl;
		}
	}
};

int main() {
	Solver::solve();
}

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

C++: #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <stdio.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for (int i=0; i < (n); i++)

const int MAX_W = 23;
bool visited[MAX_W][MAX_W];
char map[MAX_W][MAX_W] = {};
int dx[]={0,1,0,-1};
int dy[]={-1,0,1,0};

int dfs(int x, int y) {
  if (map[y][x] != '.') return 0;
  int ret = 1;
  map[y][x] = '#';
  rep(i,4) {
    ret += dfs(x + dx[i], y + dy[i]);
  }
  return ret;
}

void solve(int w, int h) {
  int sx, sy;
  rep(i, MAX_W) rep(j, MAX_W) map[i][j] = 0;
  rep(i,h) {
    string s;
    cin >> s;
    rep(j,w) {
      if (s[j] == '@') {
        sx = j+1;
        sy = i+1;
        s[j] = '.';
      }
      map[i+1][j+1] = s[j];
    }
  }
  cout << dfs(sx, sy) << endl;
}

int main() {
  int w, h;
  while(true) {
    cin >> w >> h;
    if (w == 0) return 0;
    solve(w, h);
  }
}

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

const int dx[] = {1,0,-1,0};
const int dy[] = {0,-1,0,1};
queue<pair<int,int> > M;
int W,H;
int q[20+5][20+5] = {};	
char p[20+5][20+5];
int counter;
int x,y;

int main() {
	while(cin >> W >> H && W != 0){
		counter = 0;
		for (int i = 1; i <= 22; i++){
			for (int j = 1; j <= 22; j++){
					q[i][j] = 0;
					p[i][j] = ' ';
				
			}
		}
		for (int i = 1; i <= H; i++){
			for (int j = 1; j <= W; j++){
				cin >> p[i][j];
				if (p[i][j] == '@' ){
					M.push(make_pair(i,j));
					counter = 1;
					q[i][j] = 1;
				}
			}
		}
		while(!M.empty()){
			x = M.front().first;
			y = M.front().second;
			M.pop();
			for (int k = 0; k <= 3; k++)
			{		
				if (p[x+dx[k]][y+dy[k]] == '.' && q[x+dx[k]][y+dy[k]] == 0)
				{
					M.push(make_pair(x+dx[k], y+dy[k]));
					q[x+dx[k]][y+dy[k]] = 1;
					counter += 1;
				}
			}
		}
		cout << counter << endl;
	}
}