Alice and Bob are playing the next game. Both have same matrix $N \times 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 \times 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 \times 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.
