#include <iostream>
#include <vector>
#include <tuple>
#include <cstdint>
#include <algorithm>
#include <limits>
typedef std::tuple < std::uint32_t, std::uint32_t> Vec2;
typedef std::vector<std::vector<std::uint32_t>> PType;
typedef std::tuple<PType, std::uint32_t> RType;
static const std::uint32_t W = std::numeric_limits<std::uint32_t>::max();//てきとー。
static const std::uint32_t F = W - 1;//テキトー。
bool Fill(PType& P,Vec2& V,std::uint32_t N){
std::uint32_t A = 0, B = 0;
std::uint32_t X = 0, Y = 0;
std::vector<Vec2> Vec;
std::vector<Vec2> Dir{ std::make_tuple(-1, -1), std::make_tuple(0, -1), std::make_tuple(1, -1), std::make_tuple(-1, 0), std::make_tuple(1, 0), std::make_tuple(-1, 1), std::make_tuple(0, 1), std::make_tuple(1, 1), };
std::tie(A,B) = V;
if (P[B][A] == F) Vec.push_back(std::make_tuple(A,B));
for (std::size_t i = 0; i < Vec.size(); i++){
for (std::size_t j = 0; j < Dir.size(); j++){
std::tie(X, Y) = Dir[j];
std::tie(A, B) = Vec[i];
if (Y + B >= P.size())continue;
if (X + A >= P[Y + B].size())continue;
if (P[Y + B][X + A] == W)continue;
Vec2 T = std::make_tuple(X + A, Y + B);
if (std::find(Vec.begin(), Vec.end(), T) != Vec.end()) continue;
Vec.push_back(T);
}
}
for (auto& o : Vec){
std::tie(X, Y) = o;
P[Y][X] = N;
}
return true;
}
RType MakeHoge(PType P){
std::uint32_t N = 0;
for (std::size_t i = 0; i < P.size(); i++){
for (std::size_t j = 0; j < P[i].size(); j++){
if (P[i][j] == F){
Vec2 v = std::make_tuple(j, i);
Fill(P, v,N++);
}
}
}
return std::make_tuple(P, N);
}
bool Show(RType& R){
PType P;
std::uint64_t N;
std::tie(P, N) = R;
for (auto & oo : P){
for (auto& o : oo){
if (o == W)
{
std::cout << 'W';
}
else
{
std::cout << o;
}
}
std::cout<<std::endl;
}
std::cout << N << " Room(s?)" << std::endl;
return true;
}
int main(){
PType P = {
{ W, W, F, F, W, W },
{ W, F, W, W, W, F },
{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