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:
#define  _CRT_SECURE_NO_WARNINGS
#pragma comment (linker, "/STACK:526000000")

#include "bits/stdc++.h"

using namespace std;
typedef string::const_iterator State;
#define eps 1e-11L
#define MAX_MOD 1000000007LL
#define GYAKU 500000004LL

#define MOD 998244353LL
#define seg_size 262144*2LL
#define pb push_back
#define mp make_pair
typedef long long ll;
#define REP(a,b) for(long long (a) = 0;(a) < (b);++(a))
#define ALL(x) (x).begin(),(x).end()

unsigned long xor128() {
	static unsigned long x = 123456789, y = 362436069, z = 521288629, w = time(NULL);
	unsigned long t = (x ^ (x << 11));
	x = y; y = z; z = w;
	return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}

void init() {
	iostream::sync_with_stdio(false);
	cout << fixed << setprecision(20);
}

#define int ll
int going[10][10];

string calc(string a) {
	int now = 0;
	REP(q, a.length()) {
		now = going[now][a[q] - '0'];
	}
	if(a.length() == 4){
		a.push_back(now + '0');
		return a;
	}
	if (now != 0) {
		a.clear();
	}
	return a;
}

void solve() {
	REP(i, 10) {
		REP(q, 10) {
			cin >> going[i][q];
		}
	}
	int ans = 0;
	REP(i, 10000) {
		string s = to_string(i);
		reverse(ALL(s));
		while (s.size() != 4) {
			s.push_back('0');
		}
		reverse(ALL(s));
		s = calc(s);
		int cnt = 0;
		REP(i, s.length()) {
			REP(q, 10) {
				string b = s;
				if (b[i] == q + '0') continue;
				b[i] = q + '0';
				if (calc(b).empty() == false) {
					cnt = 1;
				}
			}
		}
		REP(i, s.length()) {
			for (int q = i + 1; q < s.length()&&q < i+2; ++q) {
				string b = s;
				if (b[i] == b[q]) continue;
				swap(b[i], b[q]);
				if (calc(b).empty() == false) {
					cnt = 1;
				}
			}
		}
		ans += cnt;
	}
	cout << ans << endl;
}

#undef int
int main() {
	init();
	solve();
}