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

Source: aizu | Difficulty: UNKNOWN_DIFFICULTY


Solutions:
from itertools import product
T = [list(map(int, input().split())) for i in range(10)]

def calc(v):
	val = 0
	for e in v:
		val = T[val][e]
	return val
ans = 0
for code in product(range(10), repeat=4):
	e = calc(code)
	ok = 1
	code = list(code) + [e]
	for i in range(5):
		d = code[:]
		for j in range(10):
			if code[i] == j:
				continue
			d[i] = j
			if calc(d) == 0:
				ok = 0
	for i in range(4):
		d = code[:]
		if d[i] == d[i + 1]:
			continue
		(d[i], d[i + 1]) = (d[i + 1], d[i])
		if calc(d) == 0:
			ok = 0
	if ok == 0:
		ans += 1
print(ans)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 998244353

def LI():
	return [int(x) for x in sys.stdin.readline().split()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def main():
	rr = []
	while True:
		a = [LI() for _ in range(10)]
		sm = {}
		for i in range(10 ** 4):
			c = 0
			t = 10 ** 3
			while t > 0:
				k = i // t % 10
				c = a[c][k]
				t //= 10
			sm[i] = c
		e = collections.defaultdict(set)
		for i in range(10):
			for j in range(10):
				for k in range(10):
					for l in range(10):
						t = i * 1000 + j * 100 + k * 10 + l
						for m in range(10):
							if i != m:
								u = m * 1000 + j * 100 + k * 10 + l
								e[t].add(u)
							if j != m:
								u = i * 1000 + m * 100 + k * 10 + l
								e[t].add(u)
							if k != m:
								u = i * 1000 + j * 100 + m * 10 + l
								e[t].add(u)
							if l != m:
								u = i * 1000 + j * 100 + k * 10 + m
								e[t].add(u)
							if i != j:
								u = j * 1000 + i * 100 + k * 10 + l
								e[t].add(u)
							if j != k:
								u = i * 1000 + k * 100 + j * 10 + l
								e[t].add(u)
							if k != l:
								u = i * 1000 + j * 100 + l * 10 + k
								e[t].add(u)
		r = 0
		for i in range(10 ** 4):
			t = sm[i]
			if i % 10 != t and a[sm[i - i % 10 + t]][i % 10] == 0:
				r += 1
				continue
			f = False
			for m in range(10):
				if m == t:
					continue
				if a[t][m] == 0:
					f = True
					r += 1
					break
			if f:
				continue
			for k in e[i]:
				if a[sm[k]][t] == 0:
					r += 1
					break
		rr.append(r)
		break
	return '\n'.join(map(str, rr))
print(main())