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 <bits/stdc++.h>
using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef pair<LL, LL> PLL;

#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort((c).begin(),(c).end())

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

#define FF first
#define SS second
template<class S, class T>
istream& operator>>(istream& is, pair<S,T>& p){
  return is >> p.FF >> p.SS;
}

const double EPS = 1e-10;
const double PI  = acos(-1.0);
const LL MOD = 1e9+7;

int tab[10][10];
int to(char c){
  return c-'0';
}

int f(string& s){
  int l = 0;
  for(int i=0;i<4;++i)
	l = tab[l][to(s[i])];
  return l;
}

bool check(string& s){
  int x = f(s);
  x = tab[x][to(s[4])];
  return x == 0;
}

bool checkalter(string s){
  for(int i=0;i<5;++i){
	for(char c='0';c<='9';++c){
	  string tmp = s;
	  tmp[i] = c;
	  if(tmp == s) continue;
	  if(check(tmp)) return false;
	}
  }

  return true;
}

bool checktrans(string s){
  for(int i=0;i<4;++i){
	string tmp = s;
	swap(tmp[i], tmp[i+1]);
	if(s == tmp) continue;
	if(check(tmp)) return false;
  }
  return true;
}

int main(){
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  REP(i,10) REP(j,10) cin >> tab[i][j];

  int ans = 0;
  for(int i=0;i<10000;++i){
	string s = to_string(i);
	s = string(4-SZ(s), '0') + s;
	string enc = s + string(1, '0'+f(s));
	if(!checkalter(enc) || !checktrans(enc))
	  ++ans;
  }
  
  cout << ans << endl;
  
  return 0;
}