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 int _loop_int;
#define REP(i,n) for(_loop_int i=0;i<(_loop_int)(n);++i)
#define FOR(i,a,b) for(_loop_int i=(_loop_int)(a);i<(_loop_int)(b);++i)
#define FORR(i,a,b) for(_loop_int i=(_loop_int)(b)-1;i>=(_loop_int)(a);--i)

#define DEBUG(x) cout<<#x<<" : "<<x<<endl
#define ALL(v) (v).begin(),(v).end()

int mp[10][10];

bool check(int a,int b,int c,int d,int e){
  int x = 0;
  x = mp[x][a];
  x = mp[x][b];
  x = mp[x][c];
  x = mp[x][d];
  x = mp[x][e];
  return x==0;
}

int solve(int id){
  int a = id%10; id/=10;
  int b = id%10; id/=10;
  int c = id%10; id/=10;
  int d = id%10; id/=10;
  int e = id%10; id/=10;
  swap(a,e);
  swap(b,d);
  REP(i,10)if(a!=i && check(i,b,c,d,e))return 1;
  REP(i,10)if(b!=i && check(a,i,c,d,e))return 1;
  REP(i,10)if(c!=i && check(a,b,i,d,e))return 1;
  REP(i,10)if(d!=i && check(a,b,c,i,e))return 1;
  REP(i,10)if(e!=i && check(a,b,c,d,i))return 1;
  if(a!=b && check(b,a,c,d,e))return 1;
  if(c!=b && check(a,c,b,d,e))return 1;
  if(c!=d && check(a,b,d,c,e))return 1;
  if(e!=d && check(a,b,c,e,d))return 1;
  return 0;
}

int main(){
  REP(i,10)REP(j,10)scanf("%d",&mp[i][j]);
  int ans = 0;
  REP(id,10000){
    int a=id%10;
    int b=(id/10)%10;
    int c=(id/100)%10;
    int d=(id/1000);
    swap(a,d);
    swap(b,c);
    int e=mp[0][a];
    e=mp[e][b];
    e=mp[e][c];
    e=mp[e][d];
    ans += solve(id*10+e);
  }
  printf("%d\n",ans);
  return 0;
}