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 pb push_back
#define mp make_pair
#define fi first
#define se second
#define FOR(i, a, b) for(int i=(a);i<(b);i++)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for(int i=(a);i>=(b);i--)
#define RREP(i, n) RFOR(i, n, 0)
#define MFOR(i, m) for(auto i=(m).begin();i!=(m).end();i++)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((int)(x).size())

typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;

const double eps = 1e-10;
const int MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1 << 30;

template<typename T>
void printv(vector<T> const& s) {
  REP(i, SZ(s)) {
    cout << s[i] << " ";
  }
  cout << endl;
}

int calc[10][10];

int e(int a, int b, int c, int d) {
  return calc[calc[calc[calc[0][a]][b]][c]][d];
}

int check(int a, int b, int c, int d, int digit) {
  return calc[e(a, b, c, d)][digit];
}

int main () {
  cin.tie(0);
  cout << setprecision(10);

  REP(i, 10) {
    REP(j, 10) {
      cin >> calc[i][j];
    }
  }

  int cnt = 0;
  REP(i, 10000) {
    int a[5];
    int tmpi = i;
    a[3] = i % 10; i /= 10;
    a[2] = i % 10; i /= 10;
    a[1] = i % 10; i /= 10;
    a[0] = i % 10;
    a[4] = e(a[0], a[1], a[2], a[3]);
    i = tmpi;
    bool ok = true;
    REP(j, 5) {
      if(!ok) break;
      REP(k, 10) {
        if(a[j] != k) {
          int tmp = a[j];
          a[j] = k;
          int res = check(a[0], a[1], a[2], a[3], a[4]);
          a[j] = tmp;
          if(res == 0) {
            ok = false;
            break;
          }
        }
      }
    }
    if((a[0] != a[1] && check(a[1], a[0], a[2], a[3], a[4]) == 0) ||
        (a[1] != a[2] && check(a[0], a[2], a[1], a[3], a[4]) == 0) ||
        (a[2] != a[3] && check(a[0], a[1], a[3], a[2], a[4]) == 0) ||
        (a[3] != a[4] && check(a[0], a[1], a[2], a[4], a[3]) == 0)) ok = false;

    if(!ok) cnt++;
  }
  cout << cnt << endl;
}