408261: GYM103074 B Игры с кольцами

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

time limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

Alice and Bob are playing the next game. Both have same matrix N × M filled with digits from 0 to 9.

Alice cuts the matrix vertically, choose order of the columns, then links all the columns to each other to have the cyclic sequence of N × M digits. Note that she cannot rotate the columns, i.e. end of some column must be linked to beginning of the other column. Then she cuts a sequence and reads the decimal representation of integer A upside down.

Bob cuts the matrix horizontally, choose order of the rows, then link all the rows to each other to have the cyclic sequence of N × M digits. Note that he cannot rotate the rows, i.e. end of some row must be linked to beginning of the other row. Then he cuts a sequence and reads the decimal representation of integer B from left to right.

Player who obtained the biggest integer wins. If both integers are are equal, then game is tied. You are given the matrix, find the number obtained by winner (or by both players in case of tie), if both Alice and Bob are playing optimally.

Input

First line contains integers N and M (1 ≤ M, N ≤ 100).

Each of next N lines contains string of M digits (without the spaces or other delimiters inside) — the given matrix. You may assuma that atleast one digit in the matrix is not equal to 0.

Output

Print answer to the problem. Note that number must be printed without leading zeroes.

ExampleB. time limit per test
2 2
28
27
Output
8722

加入题单

算法标签: