Stacking Blocks

Given a 0/1 top view plus front and side height maxima, output the tallest cube stack arrangement matching all three views, or -1.

Medium4GreedyMatrixImplementationInterviewNo attempts yetTime limit1sMemory limit128 MB

Problem

Unit cubes are stacked on an N×MN \times M grid. A cell can hold zero or more cubes, and the number of cubes on a cell is the height of that cell.

Three views of the solid are given.

  • The top view is NN rows of MM values, each 0 or 1. A cell of height 1 or more is 1, and a cell of height 0 is 0.
  • The front view is MM numbers. The jj-th number is the largest height among the cells in column jj.
  • The right side view is NN numbers. A viewer standing on the right sees the last row of the top view on the far left, so the kk-th number is the largest height among the cells in row Nk+1N-k+1.

Several height assignments can match all three views. Find the one that uses the most cubes. If such an assignment exists, it is the only one.

Input

The first line has the vertical length NN and the horizontal length MM, separated by a space. (1N,M5001 \le N, M \le 500)

Each of the next NN lines has MM values, 0 or 1, giving the top view.

The next line has the front view as MM integers. Each number is between 0 and 100.

The last line has the right side view as NN integers. Each number is between 0 and 100.

Output

Print on NN lines the arrangement that matches all three views and uses the most cubes. Each line holds the heights of the MM cells in that row, separated by single spaces.

If no arrangement matches the three views, print -1.