MaxSum (Happy Sum - 2)
You have a rectangular table with N rows and M columns, where each cell contains an integer. You can traverse this table from top to bottom, starting from any cell in the top row. From each cell at position (i, j), you can move to one of the "lower neighboring" cells: (i+1, j-1), (i+1, j), or (i+1, j+1). Note that if j=M, you cannot move to (i+1, j+1), and if j=1, you cannot move to (i+1, j-1). You can finish your path in any cell of the bottom row.
Your task is to write a program that finds the maximum possible lucky sum of the values of the cells traversed along any valid path. A lucky number is a natural number whose decimal representation consists only of the digits 4 and 7. For instance, 47, 744, and 4 are lucky numbers, whereas 0, 5, 17, and 467 are not. It is important that the sum itself is a lucky number, not the individual numbers being added.
Input
The first line contains two integers, N and M, representing the number of rows and columns respectively (1 ≤ N, M ≤ 12). Each of the following N lines contains M space-separated non-negative integers, each with no more than 12 digits, representing the values in the table cells.
Output
Output a single integer, which is the maximum lucky sum found among all possible paths, or the word "impossible" (in lowercase, without quotes) if no such path results in a lucky sum.