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
#include <bits/stdc++.h>
#define For(i, a, b) for(int (i)=(int)(a); (i)<(int)(b); ++(i))
#define rFor(i, a, b) for(int (i)=(int)(a)-1; (i)>=(int)(b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<int, lint> pil;
typedef pair<lint, lint> pll;
template<class T> bool chmax(T &a, const T &b){if(a<b){a=b; return true;} return false;}
template<class T> bool chmin(T &a, const T &b){if(a>b){a=b; return true;} return false;}
template<class T> T div_floor(const T a, const T b){return a>=0 ? a/b : (a+1)/b-1;}
template<class T> T div_ceil(const T a, const T b){return a>=0 ? (a-1)/b+1 : a/b;}

constexpr lint mod = 1e9+7;
constexpr lint INF = mod*mod;
constexpr int MAX = 100010;

int a[10][10];

bool check(string &s){
    int t = a[0][s[0]-'0'];
    For(i, 1, 5){
        t = a[t][s[i]-'0'];
    }
    return t == 0;
}

bool f(int n){
    string s = to_string(n);
    int len = s.size();
    rep(i, 4-len) s = "0" + s;
    int tmp = a[0][s[0]-'0'];
    For(i, 1, 4) tmp = a[tmp][s[i]-'0'];
    s += to_string(tmp);
    string t = s;
    rep(i, 5)rep(j, 10)if(s[i]-'0' != j){
        t[i] = j+'0';
        if(check(t)) return true;
        t[i] = s[i];
    }
    rep(i, 4)if(s[i] != s[i+1]){
        swap(t[i], t[i+1]);
        if(check(t)) return true;
        swap(t[i], t[i+1]);
    }
    return false;
}

int main(){
    rep(i, 10)rep(j, 10) scanf("%d", &a[i][j]);
    int ans = 0;
    rep(i, 10000){
        ans += f(i);
    }
    printf("%d\n", ans);
}
