using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("1 0 0 0");
WillReturn.Add("0 0 1 0");
WillReturn.Add("0 0 0 0");
WillReturn.Add("1 0 0 0");
//1272
}
else if (InputPattern == "Input2") {
WillReturn.Add("1 1 1 1");
WillReturn.Add("1 1 1 1");
WillReturn.Add("1 1 1 1");
WillReturn.Add("1 1 1 1");
//1
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static int[,] mBaseBanArr;
const int UB = 3;
struct PointDef
{
internal int X;
internal int Y;
}
static List<PointDef> mMuraPointList = new List<PointDef>();
static void Main()
{
List<string> InputList = GetInputList();
mBaseBanArr = CreateBanArr(InputList);
// 村の座標のListを求める
for (int LoopX = 0; LoopX <= UB; LoopX++) {
for (int LoopY = 0; LoopY <= UB; LoopY++) {
if (mBaseBanArr[LoopX, LoopY] == 1) {
PointDef WillAdd;
WillAdd.X = LoopX;
WillAdd.Y = LoopY;
mMuraPointList.Add(WillAdd);
}
}
}
List<char[,]> ResultBitZentansaku = ExecBitZentansaku();
// 必要条件1 村の座標が堀であること
ResultBitZentansaku = ResultBitZentansaku.FindAll(pX => Check1(pX));
// 必要条件2 全ての堀が連結であること
ResultBitZentansaku = ResultBitZentansaku.FindAll(pX => Check2(pX));
// 必要条件3 堀の内部に■がないこと
ResultBitZentansaku = ResultBitZentansaku.FindAll(pX => Check3(pX));
Console.WriteLine(ResultBitZentansaku.Count);
}
// ビット全探索で堀と■を返す
static List<char[,]> ExecBitZentansaku()
{
var WillReturn = new List<char[,]>();
int BitCnt = (UB + 1) * (UB + 1);
int AllBit = (1 << BitCnt) - 1;
for (int I = 0; I <= AllBit; I++) {
char[,] BanArr = new char[UB + 1, UB + 1];
int CurrBit = 1;
for (int LoopY = 0; LoopY <= UB; LoopY++) {
for (int LoopX = 0; LoopX <= UB; LoopX++) {
if ((CurrBit & I) > 0) {
BanArr[LoopX, LoopY] = '堀';
}
else {
BanArr[LoopX, LoopY] = '■';
}
CurrBit <<= 1;
}
}
WillReturn.Add(BanArr);
}
return WillReturn;
}
// 必要条件1 村の座標が堀であること
static bool Check1(char[,] pBanArr)
{
foreach (PointDef EachMuraPoint in mMuraPointList) {
int MuraX = EachMuraPoint.X;
int MuraY = EachMuraPoint.Y;
if (pBanArr[MuraX, MuraY] != '堀') {
return false;
}
}
return true;
}
struct JyoutaiDef
{
internal int CurrX;
internal int CurrY;
}
// 必要条件2 全ての堀が連結であること
static bool Check2(char[,] pBanArr)
{
var Stk = new Stack<JyoutaiDef>();
JyoutaiDef WillPush;
WillPush.CurrX = mMuraPointList[0].X;
WillPush.CurrY = mMuraPointList[0].Y;
Stk.Push(WillPush);
var VisitedSet = new HashSet<int>();
VisitedSet.Add(GetHash(WillPush.CurrX, WillPush.CurrY));
while (Stk.Count > 0) {
JyoutaiDef Popped = Stk.Pop();
Action<int, int> PushAct = (pNewX, pNewY) =>
{
if (pNewX < 0 || UB < pNewX) return;
if (pNewY < 0 || UB < pNewY) return;
if (pBanArr[pNewX, pNewY] != '堀') return;
int Hash = GetHash(pNewX, pNewY);
if (VisitedSet.Add(Hash)) {
WillPush.CurrX = pNewX;
WillPush.CurrY = pNewY;
Stk.Push(WillPush);
}
};
PushAct(Popped.CurrX, Popped.CurrY - 1);
PushAct(Popped.CurrX, Popped.CurrY + 1);
PushAct(Popped.CurrX - 1, Popped.CurrY);
PushAct(Popped.CurrX + 1, Popped.CurrY);
}
return VisitedSet.Count == pBanArr.Cast<char>().Count(pX => pX == '堀');
}
static int GetHash(int pX, int pY)
{
return pX * 4 + pY;
}
// 必要条件3 堀の内部に■がないこと
static bool Check3(char[,] pBanArr)
{
for (int LoopX = 0; LoopX <= UB; LoopX++) {
for (int LoopY = 0; LoopY <= UB; LoopY++) {
if (pBanArr[LoopX, LoopY] != '■') {
continue;
}
if (Check3Helper(pBanArr, LoopX, LoopY) == false) {
return false;
}
}
}
return true;
}
static bool Check3Helper(char[,] pBanArr, int pStaX, int pStaY)
{
bool IsOK = false;
var Stk = new Stack<JyoutaiDef>();
JyoutaiDef WillPush;
WillPush.CurrX = pStaX;
WillPush.CurrY = pStaY;
Stk.Push(WillPush);
var VisitedSet = new HashSet<int>();
VisitedSet.Add(GetHash(WillPush.CurrX, WillPush.CurrY));
while (Stk.Count > 0) {
JyoutaiDef Popped = Stk.Pop();
Action<int, int> PushAct = (pNewX, pNewY) =>
{
if (pNewX < 0 || UB < pNewX
|| pNewY < 0 || UB < pNewY) {
IsOK = true;
return;
}
if (pBanArr[pNewX, pNewY] != '■') return;
int Hash = GetHash(pNewX, pNewY);
if (VisitedSet.Add(Hash)) {
WillPush.CurrX = pNewX;
WillPush.CurrY = pNewY;
Stk.Push(WillPush);
}
};
PushAct(Popped.CurrX, Popped.CurrY - 1);
PushAct(Popped.CurrX, Popped.CurrY + 1);
PushAct(Popped.CurrX - 1, Popped.CurrY);
PushAct(Popped.CurrX + 1, Popped.CurrY);
}
return IsOK;
}
////////////////////////////////////////////////////////////////
// IEnumerable<string>をintの2次元配列に設定する
////////////////////////////////////////////////////////////////
static int[,] CreateBanArr(IEnumerable<string> pStrEnum)
{
var StrList = pStrEnum.ToList();
if (StrList.Count == 0) {
return new int[0, 0];
}
int[] IntArr = { };
Action<string> SplitAct = pStr =>
IntArr = pStr.Split(' ').Select(pX => int.Parse(pX)).ToArray();
SplitAct(StrList[0]);
int UB_X = IntArr.GetUpperBound(0);
int UB_Y = StrList.Count - 1;
int[,] WillReturn = new int[UB_X + 1, UB_Y + 1];
for (int Y = 0; Y <= UB_Y; Y++) {
SplitAct(StrList[Y]);
for (int X = 0; X <= UB_X; X++) {
WillReturn[X, Y] = IntArr[X];
}
}
return WillReturn;
}
}