Under construction

Still being built. Things may change

The Demon King's Castle

Interview

Time limit3sMemory limit1024 MB

Summary
For each plot, find the total tax of the connected region of plots no higher than it that contains that plot, with the plot as the castle.
Level

Medium5 of 10

Topics
Union-find, Sorting, Matrix
Solved
No attempts yet

Problem

A Demon King with ambitions of conquering the world has decided to found his own nation! But before he can form a nation, he has many things to do, such as choosing territory and building a castle.

The site that can become territory is an N×MN \times M rectangle divided into 1×11 \times 1 plots of land. The Demon King selects one or more plots and declares them his territory, then builds his castle on one of those plots. Each plot has two values: its "height" and the "tax it yields." The figure below shows the tax and height of each plot in Sample 2.

The Demon King values communication with his people, so every plot in the territory must be able to reach the castle by moving through adjacent territory plots up, down, left, or right. He also firmly believes he must be the highest person in the world, so the plot with the castle must be at least as high as every other plot in the territory.

The total tax the Demon King collects equals the sum of the taxes of all plots in the territory. He wants to know the maximum tax he can collect for each of the N×MN \times M plots as the castle location. He cannot program, so you, captured by the Demon King, must write a program to solve this.

Input

The first line contains NN and MM, separated by a space.

The next NN lines each contain MM heights hijh_{ij} of the plots, separated by spaces.

The following NN lines each contain MM taxes cijc_{ij} of the plots, separated by spaces.

Output

Print NN lines, each with MM integers separated by spaces. The jj-th number on the ii-th line is the maximum total tax when the castle is built on plot (i,j)(i, j).

Constraints

1≤N,M≤1,0001 \le N, M \le 1,000

1≤hij,cij≤1091 \le h_{ij}, c_{ij} \le 10^9

Examples2

  1. Example 1

    Input
    3 4
    1 1 7 4
    3 1 2 2
    4 5 3 6
    1 1 1 1
    1 1 1 1
    1 1 1 1
    
    Expected output
    3 3 12 9
    7 3 5 5
    9 10 7 11
    
  2. Example 2

    Input
    3 3
    1 2 1
    3 10 4
    1 5 1
    2 9 4
    6 16 9
    14 11 6
    
    Expected output
    2 15 4
    35 77 50
    14 61 6