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 <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <map>
#include <vector>
#include <bitset>
using namespace std;
#define f(i, x, y) for (int i = x; i <= y; ++i)
#define fd(i, x, y) for (int i = x; i >= y; --i)
#define rep(i, x, y) for (int i = x; i <= y; ++i)
#define repd(i, x, y) for (int i = x; i >= y; --i)

const int INF = 1e9 + 7;
const int N = 50008;
int n, m, q;

int tb[10][10];
int x[6], y[6];
int calc(const int x[], int len)
{
	int tmp = 0;
	for (int i = 1; i <= len; ++i)
		tmp = tb[tmp][x[i]];
	return tmp;
}
bool check(const int x[6])
{	
	for (int i = 1; i <= 5; ++i) y[i] = x[i];
	for (int i = 1; i <= 5; ++i)
	{
		for (int j = 0; j <= 9; ++j)
			if (x[i] != j)
			{
				y[i] = j;
				if (calc(y, 5) == 0) return false;
			}
		y[i] = x[i];
	}
	for (int i = 1; i <= 4; ++i)
		if (y[i] != y[i + 1])
	{
		swap(y[i], y[i + 1]);
		if (calc(y, 5) == 0) return false;
		swap(y[i], y[i + 1]);
	}
	return true;
}

void init()
{
	for (int i = 0; i < 10; ++i)
		for (int j = 0; j < 10; ++j)
			cin >> tb[i][j];
	int ans = 0;
	for (x[1] = 0; x[1] <= 9; ++x[1])
		for (x[2] = 0; x[2] <= 9; ++x[2])
			for (x[3] = 0; x[3] <= 9; ++x[3])
				for (x[4] = 0; x[4] <= 9; ++x[4])
				{
					x[5] = calc(x, 4);
					if (!check(x)) ans++; 
				}
	cout << ans << endl;
}
void solve()
{

}
int main()
{	
	cin.sync_with_stdio(0);
	init();
	solve();
}