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: [Image] 

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) [Image] 

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-----

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-----

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.


-----Examples-----
Input
2 3
1 2 1
2 1 2

Output
2 2 2 
2 2 2 

Input
2 2
1 2
3 4

Output
2 3 
3 2 



-----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 [Image] For the intersection of the first line and the second column [Image] For the intersection of the second line and the first column [Image] For the intersection of the second line and the second column [Image]

Difficulty: competition

Starter code:

Solutions:
import heapq
import sys
from collections import defaultdict, Counter
from functools import reduce


n, m = list(map(int, input().split()))
arr = []
for _ in range(n):
    arr.append(list(map(int, input().split())))

rows = []
for i in range(n):
    row = set()
    for j in range(m):
        row.add(arr[i][j])
    rows.append({x: i for i, x in enumerate(sorted(row))})

columns = []

for j in range(m):
    column = set()
    for i in range(n):
        column.add(arr[i][j])
    columns.append({x: i for i, x in enumerate(sorted(column))})


def get_answer(i, j):
    el = arr[i][j]
    index1 = rows[i][el]
    index2 = columns[j][el]
    return max(index1, index2) + max(len(rows[i]) - index1,  len(columns[j]) - index2)


for i in range(n):
    answer = []
    for j in range(m):
        answer.append(str(get_answer(i, j)))
    print(' '.join(answer))


import sys
input=sys.stdin.readline
n, m = list(map(int, input().split()))
St = [list(map(int, input().split())) for _ in range(n)]
ppr = [sorted(list(set(s))) for s in St]
PR = []
for i in range(n):
    H = dict()
    for j, v in enumerate(ppr[i], 1):
        H[v] = j
    PR.append([H[p] for p in St[i]] + [j])

TSt = list(map(list, list(zip(*St))))
ppc = [sorted(list(set(s))) for s in TSt]
PC = []
for i in range(m):
    H = dict()
    for j, v in enumerate(ppc[i], 1):
        H[v] = j
    PC.append([H[p] for p in TSt[i]] + [j])

for i in range(n):
    pri = PR[i]
    prm = PR[i][-1]
    print(*[max(pri[j], PC[j][i]) + max(prm-pri[j], PC[j][-1]-PC[j][i]) for j in range(m)])



def solution(heights, n, m):
    rows = []
    cols = []

    for j in range(n):
        lst = set()
        for i in range(m):
            lst.add(heights[j][i])
        rows.append({x: i for i, x in enumerate(sorted(lst))})

    for i in range(m):
        lst = set()
        for j in range(n):
            lst.add(heights[j][i])
        cols.append({x: i for i, x in enumerate(sorted(lst))})

    for j in range(n):
        answer = []
        for i in range(m):
            val = heights[j][i]
            rank_row = rows[j][val]
            rank_col = cols[i][val]
            value = max(rank_row, rank_col) + max(len(rows[j]) - rank_row, len(cols[i]) - rank_col)
            answer.append(str(value))
        print(" ".join(answer))


def get():
    (n, m) = list(map(int, input().split()))
    data = []
    for j in range(n):
        data.append(list(map(int, input().split())))

    solution(data, n, m)


get()


n, m = (int(x) for x in input().split())

field = [[int(x) for x in input().split()] for _ in range(n)]

r_c___rord = []
r_c___cord = [[] for _ in range(n)]

r___uniques_nr = []
c___uniques_nr = []

for r in range(n):
    row = field[r]
    srted = sorted(list(set(row)))
    r___uniques_nr.append(len(srted))
    srted = {x: i for i, x in enumerate(srted)}
    ords = [srted[x] for x in row]
    r_c___rord.append(ords)

for c in range(m):
    col = [field[i][c] for i in range(n)]
    srted = sorted(list(set(col)))
    c___uniques_nr.append(len(srted))
    srted = {x: i for i, x in enumerate(srted)}
    ords = [srted[x] for x in col]
    for i in range(n):
        r_c___cord[i].append(ords[i])

for r in range(n):
    curr = []
    for c in range(m):
        less_nr = max(r_c___rord[r][c], r_c___cord[r][c])
        greq_nr = max(r___uniques_nr[r] - r_c___rord[r][c], c___uniques_nr[c] - r_c___cord[r][c])
        curr.append(less_nr + greq_nr)
    print(*curr)








n, m = list(map(int, input().split()))
gor = []
ver = []
o = []
for i in range(n):
    gor.append(list(map(int, input().split())))
    o.append([])
for i in range(m):
    ver.append([])
for i in range(m):
    for g in gor:
        ver[i].append(g[i])
ver = list([sorted(list(set(x))) for x in ver])


def f(vg, vr, o):
    vr1 = vr.index(o)
    vg1 = vg.index(o)
    if vr1 > vg1:
        return max([len(vr), len(vg) + vr1 - vg1])
    return max([len(vg), len(vr) + vg1 - vr1])


for i, vg in enumerate(gor):
    vg1 = sorted(list(set(vg)))
    for j, vr in enumerate(vg):
        o[i].append(f(vg1, ver[j], vr))
for i in o:
    print(' '.join(list(map(str, i))))


# # from collections import Counter
#
# n, m = list(map(int, input().split()))
# streets = []
# for i in range(n):
#     streets.append(list(map(int, input().split())))
#
# mem_rows = {}
# len_row = []
# for i in range(n):
#     mem_r = sorted(set(streets[i]))
#     mem_rows[i] = {x: i for i, x in enumerate(mem_r)}
#     len_row.append(len(mem_rows[i]))
#
# mem_cols = {}
# len_col = []
# for j in range(m):
#     mem_d = sorted(set([k[j] for k in streets]))
#     mem_cols[j] = {x: i for i, x in enumerate(mem_d)}
#     len_col.append(len(mem_cols[j]))
#
# for i in range(n):
#     for j in range(m):
#         elem = streets[i][j]
#         pos1, pos2 = mem_rows[i][elem], mem_cols[j][elem]
#         streets_ans = max(pos1, pos2) + max((len_row[i] - pos1), (len_col[j] - pos2))
#         print(streets_ans, end=' ')
#     print('\n', end='')


# from collections import Counter

n, m = list(map(int, input().split()))
streets = []
for i in range(n):
    streets.append(list(map(int, input().split())))

mem_rows = {}
len_row = []
for i in range(n):
    mem_r = sorted(set(streets[i]))
    srted = {x: i for i, x in enumerate(mem_r)}
    ords = [srted[i] for i in streets[i]]
    mem_rows[i] = ords
    len_row.append(len(mem_r))

mem_cols = {}
len_col = []
for j in range(m):
    col = [k[j] for k in streets]
    mem_d = sorted(set(col))
    srted = {x: i for i, x in enumerate(mem_d)}
    ords = [srted[i] for i in col]
    mem_cols[j] = ords
    len_col.append(len(mem_d))


for i in range(n):
    prt = []
    for j in range(m):
        elem = streets[i][j]
        pos1, pos2 = mem_rows[i][j], mem_cols[j][i]
        streets_ans = max(pos1, pos2) + max((len_row[i] - pos1), (len_col[j] - pos2))
        prt.append(streets_ans)
    print(*prt)