Skyscrapers
Contest: Codeforces Round 545 (Div. 1)
Difficulty rating: 1600
Tags: implementation, sortings

Dora loves adventures quite a lot. During some journey she encountered an amazing city, which is formed by $$$n$$$ streets along the Eastern direction and $$$m$$$ streets across the Southern direction. Naturally, this city has $$$nm$$$ intersections. At any intersection of $$$i$$$-th Eastern street and $$$j$$$-th Southern street there is a monumental skyscraper. Dora instantly became curious and decided to explore the heights of the city buildings.

When Dora passes through the intersection of the $$$i$$$-th Eastern and $$$j$$$-th Southern street she examines those two streets. After Dora learns the heights of all the skyscrapers on those two streets she wonders: how one should reassign heights to the skyscrapers on those two streets, so that the maximum height would be as small as possible and the result of comparing the heights of any two skyscrapers on one street wouldn't change.

Formally, on every of $$$nm$$$ intersections Dora solves an independent problem. She sees $$$n + m - 1$$$ skyscrapers and for each of them she knows its real height. Moreover, any two heights can be compared to get a result "greater", "smaller" or "equal". Now Dora wants to select some integer $$$x$$$ and assign every skyscraper a height from $$$1$$$ to $$$x$$$. When assigning heights, Dora wants to preserve the relative order of the skyscrapers in both streets. That is, the result of any comparison of heights of two skyscrapers in the current Eastern street shouldn't change and the result of any comparison of heights of two skyscrapers in current Southern street shouldn't change as well. Note that skyscrapers located on the Southern street are not compared with skyscrapers located on the Eastern street only. However, the skyscraper located at the streets intersection can be compared with both Southern and Eastern skyscrapers. For every intersection Dora wants to independently calculate the minimum possible $$$x$$$.

For example, if the intersection and the two streets corresponding to it look as follows:

Then it is optimal to replace the heights of the skyscrapers as follows (note that all comparisons "less", "equal", "greater" inside the Eastern street and inside the Southern street are preserved)

The largest used number is $$$5$$$, hence the answer for this intersection would be $$$5$$$.

Help Dora to compute the answers for each intersection.

Input format:
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 1000$$$) — the number of streets going in the Eastern direction and the number of the streets going in Southern direction.

Each of the following $$$n$$$ lines contains $$$m$$$ integers $$$a_{i,1}$$$, $$$a_{i,2}$$$, ..., $$$a_{i,m}$$$ ($$$1 \le a_{i,j} \le 10^9$$$). The integer $$$a_{i,j}$$$, located on $$$j$$$-th position in the $$$i$$$-th line denotes the height of the skyscraper at the intersection of the $$$i$$$-th Eastern street and $$$j$$$-th Southern direction.

Output format:
Print $$$n$$$ lines containing $$$m$$$ integers each. The integer $$$x_{i,j}$$$, located on $$$j$$$-th position inside the $$$i$$$-th line is an answer for the problem at the intersection of $$$i$$$-th Eastern street and $$$j$$$-th Southern street.

Constraints:
Time limit: 2.0 s
Memory limit: 512.0 MB

Note:
In the first example, it's not possible to decrease the maximum used height for the problem at any intersection, hence we don't have to change any heights.

In the second example, the answers are as follows:

- For the intersection of the first line and the first column
- For the intersection of the first line and the second column
- For the intersection of the second line and the first column
- For the intersection of the second line and the second column

Example input:
2 3
1 2 1
2 1 2

Example output:
2 2 2 
2 2 2

Example input:
2 2
1 2
3 4

Example output:
2 3 
3 2

Editorial:
Let's examine the $$$i$$$-th row and $$$j$$$-th column, suppose the element on their intersection is $$$x$$$
Let's denote the number of different elements less than $$$x$$$ in the row as $$$L_{row}$$$, and in the column as $$$L_{col}$$$. Similarly, let's say that the number of different elements greater than $$$x$$$ in row is $$$G_{row}$$$ and in column is $$$G_{col}$$$ (L = Less, G = Greater).
Then the answer is $$$ans = \max(L_{row}, L_{col}) + 1 + \max(G_{row}, G_{col})$$$. First summand is necessary to fit all elements $$$<x$$$, second is for $$$x$$$ and the last one for $$$>x$$$.
Now let's find a way to compute all this $$$4$$$ values.
Let's for each line and each column write down all the elements in it, sort them, delete repeating elements and save the result in such form.
Now how to find $$$L_{row}$$$ and $$$G_{row}$$$? Simply do a binary search over this list to find the element equal to $$$x$$$. If the length of this list is $$$x$$$ and the position of the found element is $$$i$$$ ($$$0$$$-based), then $$$L_{row} = i$$$ and $$$G_{row} = k - 1 - i$$$.
Similarly we can find $$$L_{col}$$$, $$$G_{col}$$$ and solve the problem
Complexity is: $$$\mathcal{O}(nm \log)$$$.