#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

char table[20][6];

int ans;

void recursive(bool used[20],int yoko[5],int yoko_index,int tate[5],int tate_index,bool is_pre_yoko){

	if(is_pre_yoko == false && tate_index == 5){
		ans++;
		return;
	}

	if(is_pre_yoko == false){

		switch(yoko_index){
		case 0:
			for(int i = 0; i < 20; i++){
				if(used[i] == false){
					int next_yoko[5];
					next_yoko[0] = i;
					bool next_used[20];
					for(int k = 0; k < 20; k++)next_used[k] = used[k];
					next_used[i] = true;
					if(i <= 9)next_used[i+10] = true;
					else{
						next_used[i-10] = true;
					}
					recursive(next_used,next_yoko,yoko_index+1,tate,tate_index,true);
				}
			}
			break;
		case 1:
			for(int i = 0; i < 20; i++){
				if(used[i] == false && abs(table[tate[0]][1]-table[i][0]) == 1){
					int next_yoko[5];
					for(int k = 0; k < 1; k++)next_yoko[k] = yoko[k];
					next_yoko[1] = i;
					bool next_used[20];
					for(int k = 0; k < 20; k++)next_used[k] = used[k];
					next_used[i] = true;
					if(i <= 9)next_used[i+10] = true;
					else{
						next_used[i-10] = true;
					}
					recursive(next_used,next_yoko,yoko_index+1,tate,tate_index,true);
				}
			}
			break;
		case 2:
			for(int i = 0; i < 20; i++){
				if(used[i] == false && abs(table[tate[0]][2]-table[i][0]) == 1 &&  abs(table[tate[1]][2]-table[i][1]) == 1){
					int next_yoko[5];
					for(int k = 0; k < 2; k++)next_yoko[k] = yoko[k];
					next_yoko[2] = i;
					bool next_used[20];
					for(int k = 0; k < 20; k++)next_used[k] = used[k];
					next_used[i] = true;
					if(i <= 9)next_used[i+10] = true;
					else{
						next_used[i-10] = true;
					}
					recursive(next_used,next_yoko,yoko_index+1,tate,tate_index,true);
				}
			}
			break;
		case 3:
			for(int i = 0; i < 20; i++){
				if(used[i] == false && abs(table[tate[0]][3]-table[i][0]) == 1 &&  abs(table[tate[1]][3]-table[i][1]) == 1 &&
						abs(table[tate[2]][3]-table[i][2]) == 1 ){
					int next_yoko[5];
					for(int k = 0; k < 3; k++)next_yoko[k] = yoko[k];
					next_yoko[3] = i;
					bool next_used[20];
					for(int k = 0; k < 20; k++)next_used[k] = used[k];
					next_used[i] = true;
					if(i <= 9)next_used[i+10] = true;
					else{
						next_used[i-10] = true;
					}
					recursive(next_used,next_yoko,yoko_index+1,tate,tate_index,true);
				}
			}
			break;
		case 4:
			for(int i = 0; i < 20; i++){
				if(used[i] == false && abs(table[tate[0]][4]-table[i][0]) == 1 &&  abs(table[tate[1]][4]-table[i][1]) == 1 &&
						abs(table[tate[2]][4]-table[i][2]) == 1 && abs(table[tate[3]][4]-table[i][3]) == 1){
					int next_yoko[5];
					for(int k = 0; k < 4; k++)next_yoko[k] = yoko[k];
					next_yoko[4] = i;
					bool next_used[20];
					for(int k = 0; k < 20; k++)next_used[k] = used[k];
					next_used[i] = true;
					if(i <= 9)next_used[i+10] = true;
					else{
						next_used[i-10] = true;
					}
					recursive(next_used,next_yoko,yoko_index+1,tate,tate_index,true);
				}
			}
			break;
		}

	}else{

		switch(tate_index){
		case 0:
			for(int i = 0; i < 20; i++){
				if(used[i] == false && abs(table[yoko[0]][0] - table[i][0]) == 1){
					int next_tate[5];
					next_tate[0] = i;
					bool next_used[20];
					for(int k = 0; k < 20; k++)next_used[k] = used[k];
					next_used[i] = true;
					if(i <= 9)next_used[i+10] = true;
					else{
						next_used[i-10] = true;
					}
					recursive(next_used,yoko,yoko_index,next_tate,tate_index+1,false);
				}
			}
			break;
		case 1:
			for(int i = 0; i < 20; i++){
				if(used[i] == false && abs(table[yoko[0]][1] - table[i][0]) == 1 && abs(table[yoko[1]][1]-table[i][1]) == 1){
					int next_tate[5];
					for(int k = 0; k < 1; k++)next_tate[k] = tate[k];
					next_tate[1] = i;
					bool next_used[20];
					for(int k = 0; k < 20; k++)next_used[k] = used[k];
					next_used[i] = true;
					if(i <= 9)next_used[i+10] = true;
					else{
						next_used[i-10] = true;
					}
					recursive(next_used,yoko,yoko_index,next_tate,tate_index+1,false);
				}
			}
			break;
		case 2:
			for(int i = 0; i < 20; i++){
				if(used[i] == false && abs(table[yoko[0]][2] - table[i][0]) == 1 && abs(table[yoko[1]][2]-table[i][1]) == 1 &&
						abs(table[yoko[2]][2]-table[i][2]) == 1){
					int next_tate[5];
					for(int k = 0; k < 2; k++)next_tate[k] = tate[k];
					next_tate[2] = i;
					bool next_used[20];
					for(int k = 0; k < 20; k++)next_used[k] = used[k];
					next_used[i] = true;
					if(i <= 9)next_used[i+10] = true;
					else{
						next_used[i-10] = true;
					}
					recursive(next_used,yoko,yoko_index,next_tate,tate_index+1,false);
				}
			}
			break;
		case 3:
			for(int i = 0; i < 20; i++){
				if(used[i] == false && abs(table[yoko[0]][3] - table[i][0]) == 1 && abs(table[yoko[1]][3]-table[i][1]) == 1 &&
						abs(table[yoko[2]][3]-table[i][2]) == 1 && abs(table[yoko[3]][3]-table[i][3]) == 1){
					int next_tate[5];
					for(int k = 0; k < 3; k++)next_tate[k] = tate[k];
					next_tate[3] = i;
					bool next_used[20];
					for(int k = 0; k < 20; k++)next_used[k] = used[k];
					next_used[i] = true;
					if(i <= 9)next_used[i+10] = true;
					else{
						next_used[i-10] = true;
					}
					recursive(next_used,yoko,yoko_index,next_tate,tate_index+1,false);
				}
			}
			break;
		case 4:
			for(int i = 0; i < 20; i++){
				if(used[i] == false && abs(table[yoko[0]][4] - table[i][0]) == 1 && abs(table[yoko[1]][4]-table[i][1]) == 1 &&
						abs(table[yoko[2]][4]-table[i][2]) == 1 && abs(table[yoko[3]][4]-table[i][3]) == 1 && abs(table[yoko[4]][4]-table[i][4]) == 1){
					int next_tate[5];
					for(int k = 0; k < 4; k++)next_tate[k] = tate[k];
					next_tate[4] = i;
					bool next_used[20];
					for(int k = 0; k < 20; k++)next_used[k] = used[k];
					next_used[i] = true;
					if(i <= 9)next_used[i+10] = true;
					else{
						next_used[i-10] = true;
					}
					recursive(next_used,yoko,yoko_index,next_tate,tate_index+1,false);
				}
			}
			break;
		}
	}
}

void func(){

	for(int i = 1; i <= 9; i++)scanf("%s",table[i]);

	bool first_used[20];
	for(int i = 0; i < 20; i++)first_used[i] = false;

	for(int i = 0; i < 10; i++){
		for(int k = 0; k < 5; k++)table[i+10][k] = table[i][4-k];

		if(table[i][0] == table[i][4] && table[i][1] == table[i][3]){
			first_used[i+10] = true;
		}
	}

	ans = 0;

	int first_yoko[5],first_tate[5];

	recursive(first_used,first_yoko,0,first_tate,0,false);

	printf("%d\n",ans/8);
}

int main(){

	while(true){
		scanf("%s",table[0]);
		if(table[0][0] == 'E')break;

		func();
	}

	return 0;
}

Measured memory_bytes: 3168