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 "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "ctime"

using namespace std;

constexpr long long int MOD = 1000000007;
//constexpr int MOD = 1000000007;
//constexpr int MOD = 998244353;
//constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-12;

//int N, M, K, T, H, W, L, R;
long long int N, M, K, T, H, W, L, R;

vector<vector<int>>v(10, vector<int>(10));

int check(string s) {
	int ret = 0;
	for (auto i : s) {
		ret = v[ret][i];
	}
	return ret;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	int ans = 0;
	for (auto &i : v)for (auto &j : i)cin >> j;
	for (char i = 0; i < 10; i++) {
		for (char j = 0; j < 10; j++) {
			for (char k = 0; k < 10; k++) {
				for (char l = 0; l < 10; l++) {
					string s;
					s.push_back(i);
					s.push_back(j);
					s.push_back(k);
					s.push_back(l);
					char add = 0;
					for (auto e : s) {
						add = v[add][e];
					}
					s.push_back(add);
					bool ok = true;
					for (int a = 0; a < 5; a++) {
						for (char b = 0; b < 10; b++) {
							if (s[a] == b)continue;
							string t = s;
							t[a] = b;
							if (!check(t))ok = false;
						}
					}
					for (int a = 0; a < 4; a++) {
						if (s[a] == s[a + 1])continue;
						swap(s[a], s[a + 1]);
						if (!check(s))ok = false;
						swap(s[a], s[a + 1]);
					}
					if (!ok)ans++;
				}
			}
		}
	}
	cout << ans << endl;
}