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 <fstream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;
 
#define REP(i,n) for(int i=0; i<n; ++i)
#define FOR(i,a,b) for(int i=a; i<=b; ++i)
#define FORR(i,a,b) for (int i=a; i>=b; --i)
#define pi M_PI
 
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;

int x[10][10];

int calc(VI a){
    int res = 0;
    REP(i,4) res = x[res][a[i]];
    return res;
}

int check(VI a){
    int res = 0;
    REP(i,5) res = x[res][a[i]];
    return res;
}

int main() {
    REP(i,10) REP(j,10) cin >> x[i][j];
    int ans = 0;
    REP(i,10000){
        VI a(5);
        int d = i;
        REP(j,4){
            a[3-j] = d % 10;
            d /= 10;
        }
        a[4] = calc(a);
        bool f = 0;
        REP(j,5) REP(p,10){
            VI b(a);
            b[j] = p;
            if (b != a){
                if (check(b) == 0) f = 1;
            }
        }
        REP(j,4){
            VI b(a);
            if (b[j] == b[j+1]) continue;
            swap(b[j], b[j+1]);
            if (check(b) == 0) f = 1;
        }
        if (f) ans++;
    }
    cout << ans << endl;

    return 0;
}