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;

#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(a) (a).begin(),(a).end()

int calc(int a, int b, int c, int d, const vector<vector<int>> &t) {
  return t[t[t[t[0][a]][b]][c]][d];
}

int main(){
  vector<vector<int>> t(10, vector<int>(10));
  REP(i,10)REP(j,10) cin>>t[i][j];
  int cnt = 0;
  REP(a,10)REP(b,10)REP(c,10)REP(d,10) {
    int e = calc(a, b, c, d, t);
    vector<int> v = {a, b, c, d, e};
    bool valid = true;
    REP(i,5) REP(j,10) {
      auto tmp = v;
      if (v[i] == j) continue;
      tmp[i] = j;
      if (t[calc(tmp[0], tmp[1], tmp[2], tmp[3], t)][tmp[4]] == 0) {
        valid = false;
      }
    }
    REP(i,4) {
      auto tmp = v;
      if (tmp[i] == tmp[i+1]) continue;
      swap(tmp[i], tmp[i+1]);
      if (t[calc(tmp[0], tmp[1], tmp[2], tmp[3], t)][tmp[4]] == 0) {
        valid = false;
      }
    }
    if (!valid) ++cnt;
  }
  cout << cnt << endl;
  return 0;
}