spoj#GRIDFLIP. Flipping Slipping of grids

Flipping Slipping of grids

 

Given two grids of characters , consists of characters from 'a' to 'z' only. we name two grids 'A' and 'B'.
Now , we need to find the lexicographically largest triplet <i,j,k> 
Given that : 
f(A,i,j,k)=B , 0<=j<k<n and 1<=i<=n-2 (where 'n*n' is the size of grid)
(i.e. function 'f' operated on matrix 'A' with 'i' , 'j' and 'k' parametrs gives matrix 'B'.
Description of function 'f' : 
f(M,i,j,k) : function operated on matrix 'M' does following operations in the given order.
1) Take rows from index '0' to 'i' of the given grid M and flip it, i.e.
for( each column Ci ) reverse(A[0..i][Ci])
 
2) Take colums from index '0' to 'j' of the grid and flip it, i.e. 
for( each row Ri ) reverse(A[Ri][0..j])
3) Take colums from index 'k' to 'n-1' of the grid and flip it, i.e.
for( each row Rj ) reverse(A[Rj][k...n-1]
4) Remove columns indexed '0' to 'j' and concatenate on the right of the grid in the same order, making new grid.

Given two grids of characters , consists of characters from 'a' to 'z' only. we name two grids 'A' and 'B'.

Now , we need to find the lexicographically largest triplet <i,j,k>   ( Assuming that one such solution does always exists )

Given that : 

f(A,i,j,k)=B , 0<=j<k<n and 1<=i<=n-2 (where 'n*n' is the size of grids)

(i.e. function 'f' operated on matrix 'A' with 'i' , 'j' and 'k' parametrs gives matrix 'B'.

 

Description of function 'f' : 

f(M,i,j,k) : function operated on matrix 'M' does following operations in the given order.

 

1) Take rows from index '0' to 'i' of the given grid M and flip it, i.e.

for( each column Ci ) reverse(A[0..i][Ci])

 

2) Take colums from index '0' to 'j' of the grid and flip it, i.e. 

for( each row Ri ) reverse(A[Ri][0..j])

 

3) Take colums from index 'k' to 'n-1' of the grid and flip it, i.e.

for( each row Rj ) reverse(A[Rj][k...n-1]

 

4) Remove columns indexed '0' to 'j' and concatenate on the right of the grid in the same order, making new grid.

 

Input

First line contains one integer 'n' (n*n is size of grid)

Following n lines (i.e. line numbers 2 to n+1 ) containes strings each of size 'n' for grid 'A'.

Following n lines (i.e. line numbers n+2 to 2n+1) containes strings each of size 'n' for grid 'B'.

Constraints:

1) 5<=n<=1000 

2) String contains only lower case alphabets

 

Output

Three integers (space separated) in one line representing i , j and k respectively (lexicographically largest solution).

Example

Input:
5
ooscz
hkaea
nnzth
khdlf
rejtf
fldhk
htznn
aeakh
zcsoo
ftjer

Output: 3 3 4

</p>