Write a program that examines a square matrix of integers with dimensions N x N. The first input line contains the matrix size N, and the rows of the matrix follow on successive input lines. All numbers in each row will be separated by a single space.
Determine how many matrix columns contain a permutation of the numbers 1, 2, ..., N (i.e. each of the numbers 1, 2, ..., N appears exactly once in the column). Write this number to the output.
Note: N may be as large as 1,000. If your program runs in O(N^3), it will be too slow to pass all the tests.
Example:
Input:
5
1 1 3 4 5
2 2 3 1 5
4 3 5 5 1
12 4 23 2 3
5 5 3 3 2
Output:
2