50164. Dice

I'm a slow walker, but I never walk backwards.

Task Description

We have a two dimensional array filled with numbers from one to six. Write a program to find out the number of ways to select six neighboring array elements to form a dice. A dice is a cube that has numbers from 1 to 6 on its six surfaces, and the sums of all three pairs of numbers on the opposite faces are all 7. To simplify this task, we only consider dices that have four numbers on the same row, as in the following figures.

Input Format

The first line has the number of rows $R$ and columns $C$ of the two dimensional array. Each of the following $R$ lines has $C$ numbers from 1 to 6, indicating the numbers of the array.

  • $5 \leq R,C \leq 1000$

Output Format

The output has only one integer - the numbers of dices that can be formed according to the rules described earlier.

Sample Input 1

5 5
1 1 1 1 1
1 1 4 4 3
1 2 1 5 6
1 4 3 3 2
1 1 1 1 1

Sample Output 1

5



1
2
3
5
4
6


(3D Interactive cube by Jordan Leigh, https://codepen.io/jordizle/pen/haIdo/)

Discussion