Олимпиадный тренинг

Задача 27225. Modern Art #3


Задача

Темы:
Recently, the cow-artist Picowso has come into fashion.
Picowso draws in a special way. She starts on a blank NxN canvas, represented by an NxN matrix of zeros, where zero means an empty canvas cell. She then draws up to 9 rectangles on the canvas, each with one of 9 colors (consecutively numbered 1…9). For example, she might start drawing a rectangle with color 2, getting this intermediate canvas state:
 
2220 
2220 
2220 
0000
She can then draw a rectangle with color 7:
 
2220 
2777 
2777 
0000
She can then draw a rectangle with color 3:
 
2230 
2737 
2777 
0000
Each rectangle has sides parallel to the sides of the canvas, and the largest rectangle can be the size of the entire canvas, and the smallest one the size of one cell. Each color in 1…9 is used exactly once, although any color may subsequently completely cover some of the earlier colors.
 
Given the final position of the canvas, calculate how many of the other visible colors could have been the first color drawn.
 
INPUT FORMAT:
 
The first line of input contains N, the size of the canvas (1≤N≤10). The next N lines describe the final picture of the canvas, each containing N numbers in the interval 0*9. This input is guaranteed to have been drawn as described above using different colors.

OUTPUT FORMAT:
 
Output the number of colors that could have been used first, out of all the colors visible in the final drawing.

Enter Output
4
2230
2737
2777
0000
1