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;

//#define int long long
#define DBG 1
#define dump(o) if(DBG){cerr<<#o<<" "<<(o)<<" ";}
#define dumpl(o) if(DBG){cerr<<#o<<" "<<(o)<<endl;}
#define dumpc(o) if(DBG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);
template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; }

//0??????
void fill(string& s, int length, char c = '0') {
	string prefix = "";
	for (int i = s.size(); i < length; i++)prefix += c;
	s = prefix + s;
}

int table[10][10];
char e(string &s) {
	int ret = 0;
	rep(i, 0, 4) {
		ret = table[ret][s[i] - '0'];
	}
	return (char)(ret + '0');
}

int check(string &s) {
	string abcd = s.substr(0, 4);
	return table[e(abcd) - '0'][s[4] - '0'];
}

signed main() {
	rep(i, 0, 10)rep(j, 0, 10)cin >> table[i][j];
	int ans = 0;
	rep(x, 0, 10000) {
		string abcd = to_string(x);
		fill(abcd, 4);
		string abcde = abcd + e(abcd);
		bool flag = false;
		rep(i, 0, 5) {
			rep(c, 0, 10) {
				if (c == abcde[i] - '0')continue;
				string tmp = abcde;
				tmp[i] = c + '0';
				if (check(tmp) == 0) {
					flag = true;
					i = INF;
					break;
				}
			}
		}
		if (flag) {
			ans++;
			continue;
		}
		rep(i, 1, 5) {
			string tmp = abcde;
			swap(tmp[i], tmp[i - 1]);
			if (tmp == abcde)continue;
			if (check(tmp) == 0) {
				flag = true;
				ans++;
				break;
			}
		}
	}
	cout << ans << endl;
	return 0;
}