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 <iostream>
#include <map>
#include <set>
#include <vector>
#include <algorithm>

using namespace std;

int main() {

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

	for (int i = 10; i < 100000; i++) {
		int a = i / 10;
		int b = i % 10;
		d[i] = dd[d[a]][b];
	}
	/*
	for (int i = 0; i < 100000; i++) {
		if (d[i] < 0 || 9 < d[i]) {
			cout << i << ' ' << d[i] << endl;
		}
	}
	*/
	int ans = 0;
	//cout << 'a' << endl;
	for (int i = 0; i < 10000; i++) {
		//cout << i << endl;
		int cos = 0;
		int kari = i*10+d[i];
		vector<int> p(5);
		p[0] = kari / 10000;
		kari %= 10000;
		p[1] = kari / 1000;
		kari %= 1000;
		p[2] = kari / 100;
		kari %= 100;
		p[3] = kari / 10;
		kari %= 10;
		p[4] = kari;
		
		kari = i * 10 + d[i];

		for (int j = 0; j < 5; j++) {
			vector<int> q(5);
			q = p;
			for (int k = 0; k < 10; k++) {
				q[j] = k;
				int n = 0;
				for (int l = 0; l < 5; l++) {
					n *= 10;
					n += q[l];
				}
				if (d[n] == 0 && n != kari) {
					cos++;
				}
			}
		}
		
		for (int j = 0; j < 4; j++) {
			vector<int> q(5);
			q = p;
			swap(q[j], q[j + 1]);
			int n = 0;
			for (int k = 0; k < 5; k++) {
				n *= 10;
				n += q[k];
			}
			if (d[n] == 0 && n != kari) {
				cos++;
			}
		}
		if (cos > 0) {
			ans++;
			//cout << i << endl;
		}
	}

	cout << ans << endl;
	return 0;
}