Under construction

Still being built. Things may change

Cylon Code

Interview

Time limit2sMemory limit1024 MB

Summary
Given an n by m matrix A encoded with a 2x2 matrix C (entries at most 1 in absolute value, C22 nonzero), recover the original matrix B.
Level

Medium5 of 10

Topics
Dynamic programming, Implementation
Solved
No attempts yet

Problem

The battlestar Galactica intercepted a message from the Cylons. The message is an integer matrix AA of size n×mn \times m, and it is encoded. After interrogating one of the Cylon sixes, the crew learned how the matrix is encoded. A 2×22 \times 2 matrix CC is used. Each element of CC is an integer with absolute value at most 1, and C22≠0C_{22} \neq 0. When matrix BB is encoded, the result is a matrix AA with the same size as BB, where Aij=Bi−1,j−1⋅C11+Bi−1,j⋅C12+Bi,j−1⋅C21+Bij⋅C22A_{ij} = B_{i-1,j-1} \cdot C_{11} + B_{i-1,j} \cdot C_{12} + B_{i,j-1} \cdot C_{21} + B_{ij} \cdot C_{22} If an index falls outside the matrix, that element is taken to be zero. Admiral Adama asks you to decode the Cylon message, since the fate of what remains of humanity depends on it.

Input

The first line contains two integers nn and mm (1≤n,m≤201 \le n,m \le 20). The next nn lines each contain mm integers, which form the encoded matrix AA. The number in the jj-th position of the (i+1)(i+1)-th line is AijA_{ij}. The last line contains four integers C11C_{11}, C12C_{12}, C21C_{21}, and C22C_{22}, the elements of matrix CC. All numbers in the input are integers with absolute value at most 1000. For all ii and jj, ∣Cij∣≤1|C_{ij}| \le 1, and C22≠0C_{22} \neq 0.

Output

Print nn lines with mm numbers each. These are the elements of the matrix AA decoded with matrix CC.

Examples2

  1. Example 1

    Input
    3 3
    1 2 3
    4 4 4
    7 4 4
    -1 0 0 1
    
    Expected output
    1 2 3
    4 5 6
    7 8 9
    
  2. Example 2

    Input
    1 6
    -4 -12 -23 -31 -39 -65
    1 1 -1 -1
    
    Expected output
    4 8 15 16 23 42