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 <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using msi = map<string, int>;
using mii = map<int, int>;
using psi = pair<string, int>;
using pii = pair<int, int>;
using vlai = valarray<int>;
#define rep(i,n) for(int i=0;i<n;i++)
#define range(i,s,n) for(int i=s;i<n;i++)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define fs first
#define sc second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define INF 1E9
#define EPS 1E-9
#define MOD (1E9+7)
#define PI 3.1415926535897932384

std::ostream &operator<<(std::ostream &out, const vector<int> &tgt)
{
	string s;
	for (int i = 0; i<tgt.size(); i++) {
		s += (to_string(tgt[i]) + ((i != tgt.size() - 1) ? " " : ""));
	}
	out << s;
	return out;
}

int cas(int a, int b, int c, int d, int e) {
	return a * 10000 + b * 1000 + c * 100 + d * 10 + e;
}

int main() {
	int in;
	while (1) {
		//solve
		int ans = 0;
		int mas[10][10];

		rep(i, 10) {
			rep(j, 10) {
				if (!(cin >> in)) {
					return 0;
				}
				mas[i][j] = in;
			}
		}

		rep(i, 10000) {
			vi v;
			int a = i / 1000;
			int b = i / 100 % 10;
			int c = i / 10 % 10;
			int d = i % 10;
			int e = mas[mas[mas[mas[0][a]][b]][c]][d];
			rep(i, 10) {
				v.push_back(cas(i, b, c, d, e));
				v.push_back(cas(a, i, c, d, e));
				v.push_back(cas(a, b, i, d, e));
				v.push_back(cas(a, b, c, i, e));
				v.push_back(cas(a, b, c, d, i));
			}
			v.push_back(cas(b, a, c, d, e));
			v.push_back(cas(a, c, b, d, e));
			v.push_back(cas(a, b, d, c, e));
			v.push_back(cas(a, b, c, e, d));
			int y = cas(a, b, c, d, e);
			bool fl = true;
			for (int x : v) {
				int a = x / 10000;
				int b = x / 1000 % 10;
				int c = x / 100 % 10;
				int d = x / 10 % 10;
				int e = x % 10;
				if (y != cas(a, b, c, d, e)){
					if (mas[mas[mas[mas[mas[0][a]][b]][c]][d]][e] == 0) {
						fl = false; break;
					}
				}
			}
			if (fl)ans++;
		}
		cout << 10000-ans << endl;
		//break;
	}
}