Problem: Example

Input

0 3 1 7 5 9 8 6 4 2
7 0 9 2 1 5 4 8 6 3
4 2 0 6 8 7 1 3 5 9
1 7 5 0 9 8 3 4 2 6
6 1 2 3 0 4 5 9 7 8
3 6 7 4 2 0 9 5 8 1
5 8 6 9 7 2 0 1 3 4
8 9 4 5 3 6 2 0 1 7
9 4 3 8 6 1 7 2 0 5
2 5 8 1 4 3 6 7 9 0


Output

0

Solution (in CPP):

Solution:
#include <utility> // pair make_pair
using std::swap; // c++11
using std::pair;
using std::make_pair;
#include <tuple> // tuple(c++11) make_tuple(c++11)
using std::tuple; // get<address>(variable)
using std::make_tuple;
#include <string> // string stoi(c++11) stoll(c++11) to_string(c++11)
using std::string;
using std::stoi;
using std::stoll;
using std::to_string;
#include <queue> // queue priority_queue
using std::queue;
using std::priority_queue;
#include <stack> // stack
using std::stack;
#include <vector> // vector
using std::vector;
#include <map> // map
using std::map;
#include <set> // set
using std::set; // insert erase find count
#include <algorithm> // min max
using std::sort;
using std::lower_bound; // iterator,  >=
using std::upper_bound; // iterator,  >
using std::min;
using std::max;
using std::min_element; // iterator
using std::max_element; // iterator
#include <complex> // complex
using std::complex; // real imag
#include <iostream> // cin cout
using std::cin;
using std::cout;
using std::endl;
#include <iomanip> // setprecision fixed(manipulator)
using std::setprecision;
using std::fixed;
#include <cmath>
#include <cstdio>

#define lli long long int
#define INF (int)1e09
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define EFOR(i, m, n) for (int i = m; i <= n; i++)
#define RFOR(i, m, n) for (int i = m; i > n; i--)
#define REFOR(i, m, n) for (int i = m; i >= n; i--)
#define VSORT(v) sort((v).begin(), (v).end()) // sort vector
typedef pair<int, int> PI;
typedef tuple<int, int, int> TI;
template <typename T> using Vec = vector<T>; // c++11

#define SIZE 10
Vec<Vec<int>> table(10, Vec<int>(10));
bool detection (int array[5]);

int main(void) {
    FOR(i, 0, 10) {
        FOR(j, 0, 10) {
            cin >> table[i][j];
        }
    }
    int count = 0;
    FOR(a, 0, 10) {
        FOR(b, 0, 10) {
            FOR(c, 0, 10) {
                FOR(d, 0, 10) {
                    int e = table[table[table[table[0][a]][b]][c]][d];
                    int array[5] = {a, b, c, d, e};
                    if (detection(array) == false) {
                        count++;
                    }
                }
            }
        }
    }
    cout << count << endl;
    return 0;
}

bool detection (int array[5]) {
    bool detect = true;
    FOR(i, 0, 10) {
        FOR(j, 0, 5) {
            if (i == array[j]) {
                continue;
            }
            int temp = array[j];
            array[j] = i;
            if (table[table[table[table[table[0][array[0]]][array[1]]][array[2]]][array[3]]][array[4]] == 0) {
                detect = false;
            }
            array[j] = temp;
        }
    }
    FOR (i, 0, 4) {
        if (array[i] == array[i + 1]) {
            continue;
        }
        swap(array[i], array[i + 1]);
        if (table[table[table[table[table[0][array[0]]][array[1]]][array[2]]][array[3]]][array[4]] == 0) {
            detect = false;
        }
        swap(array[i], array[i + 1]);
    }
    return detect;
}