#include <bits/stdc++.h>
using namespace std;
const int nx[] = {0 , 1 , 0 , -1};
const int ny[] = {1 , 0 , -1 , 0};
char mp[6][6];
bool book[6][6];
int ans;
void dfs(int x , int y , vector<pair<int , int>> c)
{
if (c.size() == 7)
{
int sumj = 0 , sumh = 0;
for (auto i : c)
{
if (mp[i.first][i.second] == 'J') ++sumj;
else ++sumh;
}
if (sumj > sumh) ++ans;
return;
}
for (int i = 0 ; i < 4 ; i++)
{
int tx = x + nx[i] , ty = y + ny[i];
if (tx >= 1 && tx <= 5 && ty >= 1 && ty <= 5 && !book[tx][ty])
{
c.push_back(make_pair(tx , ty));
book[tx][ty] = true;
dfs(tx , ty , c);
book[tx][ty] = false;
c.pop_back();
}
}
return;
}
int main()
{
for (int i = 1 ; i <= 5 ; i++)
for (int j = 1 ; j <= 5 ; j++) cin >> mp[i][j];
for (int i = 1 ; i <= 5 ; i++)
for (int j = 1 ; j <= 5 ; j++)
{
memset(book , false , sizeof(book));
vector<pair<int , int>> tv;
tv.push_back(make_pair(i , j));
book[i][j] = true;
dfs(i , j , tv);
}
cout << ans;
return 0;
}