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 <iostream>
#include <cstdio>
#include <vector>
#include <string>
#include <algorithm>

#define rep(i,n) for(int i=0; i<(n); i++)
#define rrep(i,n) for(int i=(n)-1; i>=0; i--)
#define all(X) (X).begin(),(X).end()

using namespace std;
typedef long long int ll;
typedef pair<int,int> pii;

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }

template<class A, size_t N, class T> void Fill(A (&a)[N], const T &v){ fill( (T*)a, (T*)(a+N), v ); }

const int INF = 0x3fffffff;


int mp[10][10], ans=0;
int check(vector<int> v){
	if( v.empty() ) return 0;
	int k = v.back(); v.pop_back();
	return mp[check(v)][k];
}

bool solve(vector<int> v){
	rep(i,5){
		auto u = v;
		rep(k,10){
			u[i] = k;
			if( u != v && check(u) == 0 ) return true;
		}
	}
	rep(i,4){
		auto u = v;
		swap( u[i], u[i+1] );
		if( u != v && check(u) == 0 ) return true;
	}
	return false;
}

void dfs(vector<int> v={}){
	if( v.size() == 4 ){
		v.push_back( check(v) );
		ans += solve(v);
	}else{
		v.emplace_back();
		rep(i,10){
			v.back() = i;
			dfs(v);
		}
	}
}

int main(){

	rep(i,10) rep(j,10) cin >> mp[i][j];

	dfs();

	cout << ans << endl;

	return 0;
}