Matrix
InterviewTime limit2sMemory limit512 MB
Find row addition counts from 0 to 9 that turn matrix A into B with matching column subtraction counts and the smallest row digit string.
Problem
You are given two matrices and . Matrix comes from matrix by row-addition operations and column-subtraction operations. One row-addition operation adds 1 to every entry of a row, and one column-subtraction operation subtracts 1 from every entry of a column.
Find the numbers of row-addition operations applied to row 1 through row of so that all of the following hold.
- There exist numbers of column-subtraction operations applied to column 1 through column of such that these row and column operations turn into .
- Every count is between 0 and 9 inclusive, that is for and for .
- The value , read as one integer, is as small as possible.
The answer is through concatenated in order. If cannot be obtained from within these limits, the answer is .
Input
The first line contains two integers and separated by a space (, ).
The next lines give matrix , from row 1 to row . Each of these lines contains integers separated by single spaces. The next lines give matrix in the same format.
Every entry of both matrices is an integer between and inclusive.
Output
Print one line.
If the transformation is possible, print through concatenated in order as a digit string of length , keeping any leading zeros. Otherwise print .