Roman Corridor

No attempts yetTime limit1sMemory limit128 MB

Problem

Roman numerals represent the natural numbers from 11 to 39993999. They use the capital Latin letters I, V, X, L, C, D, M, whose "atomic" values are shown below.

ValueRoman
1I
4IV
5V
9IX
10X
40XL
50L
90XC
100C
400CD
500D
900CM
1000M

To write a number NN, repeatedly take the largest atomic value KK that does not exceed NN, append its Roman form, and continue with NKN - K. The symbols are written left to right with no spaces. For example, 999999 is written as CMXCIX (not IM, as one might guess).

You must walk through a rectangular corridor that is nn meters wide and mm meters long (1n,m151 \le n, m \le 15, n×m100n \times m \le 100). It is paved with square tiles, one meter on a side, and every tile shows one Roman symbol: I, V, X, L, C, D, or M. You move from tile to tile; from the current tile you may step only to a tile that shares an edge with it (up, down, left, or right — never diagonally). You enter from the leftmost column and must leave from the rightmost column.

Reading the symbols along your route from start to finish yields a string. Find a route whose string is a valid Roman numeral, and among all such routes report the one with the smallest value. If no route spells a valid Roman numeral, report that it is impossible.

Input

The first line contains two integers nn and mm, separated by one or more spaces. Each of the next nn lines contains mm characters describing one row of tiles.

Output

Print the smallest valid Roman numeral that can be spelled by a route from the leftmost column to the rightmost column. If no such route exists, print NO.