URI Online Judge | 1901


By Thalyson Nepomuceno, Universidade Estadual do Ceará BR Brazil

Timelimit: 1

The forests of planet called Binox have several rare species of butterflies. Bino is a butterfly hunter and he wants collect the max of different species of butterflies. The forest of Binox is represented by a square grid of size NxN. Each cell of the grid can have one butterfly. The image below represents the first example of input. The species collected were: 1, 2, 3, 4 and 8.

Your task is determine the amount of species of butterflies, which Bino is able to collect, and the only information that you have is the map of the forest and all places visited by Bino. For an unknown reason, Bino always searches in the 2*N positions in the forest.


The input consists of multiples rows. The first row have an integer N (0 < N ≤ 200) representing the size of the forest. The following N rows have N integers Kij (0 < Kij ≤ 1000) each one representing one butterfly specie. The following N*2 rows, each row have 2 integers, representing the cells visited by Bino.


Print out one row with an amount of different species which Bino collected.

Input Samples Output Samples

1 1 2
2 3 4
8 7 1
1 1
1 2
2 1
2 2
2 3
3 1