Ada Matrix SOLUTIONS ADAMAT
Culinary expert Ada has a lattice with N lines (numbered 1 through N start to finish) and N sections (numbered 1 through N from left to right) containing all numbers somewhere in the range of 1 and N2 comprehensive. For each substantial I and j, we should indicate the cell in the I-th line and j-th segment by (i,j).
Ada needs to sort the grid in column significant request ― for each legitimate I and j, she needs the cell (i,j) to contain the worth (i−1)⋅N+j.
In one activity, Ada ought to pick a number L and translate the submatrix between lines 1 and L and sections 1 and L (comprehensive). Officially, for every I and j (1≤i,j≤L), the incentive in the cell (i,j) after this activity is equivalent to the incentive in (j,i) before it.
The underlying condition of the network is picked so that it is conceivable to sort it utilizing a limited number of activities (perhaps zero). Locate the most modest number of activities needed to sort the grid.
The primary line of the info contains a solitary whole number T signifying the quantity of experiments. The portrayal of T experiments follows.
The main line of each experiment contains a solitary whole number N.
The following N lines portray the grid. For each substantial I, the I-th of these lines contains N space-isolated whole numbers ― the underlying qualities in cells (i,1),(i,2),… ,(i,N).
For each experiment, print a solitary line containing one number ― the most modest number of tasks needed to sort the lattice.
the aggregate of N2 over all test records doesn’t surpass 3⋅105
Subtask #1 (10 focuses):
Subtask #2 (90 focuses): unique limitations
1 2 9 13
5 6 10 14
3 7 11 15
4 8 12 16
Model case 1: After the main activity, with L=2, the subsequent lattice is
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
After the subsequent activity, with L=3, the network gets arranged
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
LOGIC VIDEO IS REMOVED