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 <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
#define REP(i,n) for(int i=0;i<int(n);i++)

char table[10][10];
char digit[10000];

string toString(int x)
{
  string s = to_string(x);
  string t = "";
  REP(i,4-s.size()) t += "0";
  return t+s;
}

char getDigit(string s)
{
  return table[table[table[table[0][s[0]-'0']-'0'][s[1]-'0']-'0'][s[2]-'0']-'0'][s[3]-'0'];
}

bool check(string s)
{
  return table[digit[stoi(s.substr(0,4))]-'0'][s[4]-'0'] == '0';
}

int main()
{
  REP(i,10) REP(j,10) cin >> table[i][j];
  vector<bool> state(10000,false);
  REP(i,10000) digit[i] = getDigit(toString(i));
  REP(i,10000){
    if(state[i]) continue;
    string s = toString(i)+digit[i];
    string s_error;
    REP(j,5){
      REP(k,10){
        s_error = s;
        s_error[j] = to_string(k)[0];
        if(s == s_error) continue;
        if(check(s_error)) state[i] = state[stoi(s_error.substr(0,4))] = true;
      }
    }
    REP(j,4){
      s_error = s;
      swap(s_error[j],s_error[j+1]);
      if(s == s_error) continue;
      if(check(s_error)) state[i] = state[stoi(s_error.substr(0,4))] = true;
    }
  }
  cout << count(begin(state),end(state),true) << endl;
}