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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int res[10000];
int table[10][10];

int check(int n){
	int d = n / 1 % 10;
	int c = n / 10 % 10;
	int b = n / 100 % 10;
	int a = n / 1000 % 10;
	int r1 = table[0][a];
	int r2 = table[r1][b];
	int r3 = table[r2][c];
	int r4 = table[r3][d];
	return r4;
}

bool f(int n){
	vector<int> a(5);
	a[4] = n / 1 % 10;
	a[3] = n / 10 % 10;
	a[2] = n / 100 % 10;
	a[1] = n / 1000 % 10;
	a[0] = n / 10000 % 10;
	for(int i = 0; i < 5; i++){
		for(int j = 0; j < 10; j++){
			if(a[i] == j) continue;
			vector<int> b = a;
			b[i] = j;
			int m = b[0] * 1000 + b[1] * 100 + b[2] * 10 + b[3];
			if(table[res[m]][b[4]] == 0){
				return false;
			}
		}
	}
	for(int i = 0; i < 4; i++){
		if(a[i] == a[i + 1]) continue;
		vector<int> b = a;
		swap(b[i], b[i + 1]);
		int m = b[0] * 1000 + b[1] * 100 + b[2] * 10 + b[3];
		if(table[res[m]][b[4]] == 0){
			return false;
		}
	}
	return true;
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
#ifdef LOCAL
	std::ifstream in("in");
	std::cin.rdbuf(in.rdbuf());
#endif

	for(int i = 0; i < 10; i++){
		for(int j = 0; j < 10; j++){
			cin >> table[i][j];
		}
	}

	for(int i = 0; i < 10000; i++){
		res[i] = check(i);
	}

	int ans = 0;
	for(int i = 0; i < 10000; i++){
		if(!f(i * 10 + res[i])) ans++;
	}
	cout << ans << endl;
}