Problem2361--Number Box

2361: Number Box

[Creator : ]
Time Limit : 1.000 sec  Memory Limit : 128 MB

Description



Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 200200 points

Problem Statement

You are given a positive integer NN.
We have a grid with NN rows and NN columns, where the square at the ii-th row from the top and jj-th column from the left has a digit A_{i,j}Ai,j written on it.
Assume that the upper and lower edges of this grid are connected, as well as the left and right edges. In other words, all of the following holds.
  • (N,i)(N,i) is just above (1,i)(1,i), and (1,i)(1,i) is just below (N,i)(N,i)(1\le i\le N)(1iN).
  • (i,N)(i,N) is just to the left of (i,1)(i,1), and (i,1)(i,1) is just to the right of (i,N)(i,N)(1\le i\le N)(1iN).
Takahashi will first choose one of the following eight directions: up, down, left, right, and the four diagonal directions. Then, he will start on a square of his choice and repeat moving one square in the chosen direction N-1N1 times.
In this process, Takahashi visits NN squares. Find the greatest possible value of the integer that is obtained by arranging the digits written on the squares visited by Takahashi from left to right in the order visited by him.

Constraints

  • 1 \le N \le 101N10
  • 1 \le A_{i,j} \le 91Ai,j9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:
NNA_{1,1}A_{1,2}\dots A_{1,N}A1,1A1,2A1,NA_{2,1}A_{2,2}\dots A_{2,N}A2,1A2,2A2,N\vdotsA_{N,1}A_{N,2}\dots A_{N,N}AN,1AN,2AN,N

Output

Print the answer.

Sample Input Copy

4
1161
1119
7111
1811

Sample Output Copy

9786

HINT

If Takahashi starts on the square at the 22-nd row from the top and 44-th column from the left and goes down and to the right, the integer obtained by arranging the digits written on the visited squares will be 97869786. It is impossible to make a value greater than 97869786, so the answer is 97869786.

Source/Category