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]

Source: codeforces | Difficulty: MEDIUM_HARD
Tags: Sorting

Implementation


Solutions:
import sys
input = sys.stdin.buffer.readline
from bisect import bisect_left
(n, m) = map(int, input().split())
arr = []
for i in range(n):
	arr.append(list(map(int, input().split())))
arr_maxi = [[0 for i in range(m)] for j in range(n)]
arr_mini = [[0 for i in range(m)] for j in range(n)]
for i in range(n):
	s = sorted(set(arr[i]))
	for j in range(m):
		indx = bisect_left(s, arr[i][j])
		arr_maxi[i][j] = len(s) - indx - 1
		arr_mini[i][j] = indx
for j in range(m):
	s = sorted(set([arr[i][j] for i in range(n)]))
	for i in range(n):
		indx = bisect_left(s, arr[i][j])
		arr_maxi[i][j] = max(arr_maxi[i][j], len(s) - indx - 1)
		arr_mini[i][j] = max(arr_mini[i][j], indx)
for i in range(n):
	for j in range(m):
		print(arr_maxi[i][j] + arr_mini[i][j] + 1, end=' ')
	print()


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from bisect import *
from collections import *
(a, b) = map(int, input().split())
ans = []
rp = [[0 for i in range(b)] for i in range(a)]
for i in range(a):
	z = list(map(int, input().split()))
	ans.append(z)
	t = z.copy()
	t.sort()
	al = defaultdict(int)
	ke = list(set(t))
	for t in range(len(ke)):
		al[ke[t]] = t + 1
	for j in range(b):
		rp[i][j] = (al[z[j]], len(ke))
col = [[0 for i in range(a)] for j in range(b)]
for i in range(b):
	for j in range(a):
		col[i][j] = ans[j][i]
cp = [[0 for i in range(a)] for j in range(b)]
for i in range(b):
	t = col[i].copy()
	t.sort()
	al = defaultdict(int)
	ke = list(set(t))
	for t in range(len(ke)):
		al[ke[t]] = t + 1
	for j in range(a):
		cp[i][j] = (al[col[i][j]], len(ke))
fin = [[0 for i in range(b)] for j in range(a)]
for i in range(a):
	for j in range(b):
		m = rp[i][j]
		u = cp[j][i]
		if m[0] == u[0]:
			fin[i][j] = max(u[1], m[1])
		if m[0] > u[0]:
			fin[i][j] = max(m[1], m[0] + u[1] - u[0])
		if m[0] < u[0]:
			fin[i][j] = max(u[1], u[0] + m[1] - m[0])
for i in range(a):
	print(*fin[i])


(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(map(lambda x: sorted(list(set(x))), 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))))


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
import collections
from itertools import permutations
from collections import defaultdict
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
(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)


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


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


from bisect import bisect_left
import sys
input = sys.stdin.buffer.readline
(n, m) = map(int, input().split())
a = [list(map(int, input().split())) for i in range(n)]
yoko_less = [[0] * m for i in range(n)]
yoko_more = [[0] * m for i in range(n)]
for i in range(n):
	yoko = sorted(set(a[i]))
	for j in range(m):
		idx = bisect_left(yoko, a[i][j])
		yoko_less[i][j] = idx
		yoko_more[i][j] = len(yoko) - idx - 1
tate_less = [[0] * m for i in range(n)]
tate_more = [[0] * m for i in range(n)]
for j in range(m):
	tate = sorted(set([a[i][j] for i in range(n)]))
	for i in range(n):
		idx = bisect_left(tate, a[i][j])
		tate_less[i][j] = idx
		tate_more[i][j] = len(tate) - idx - 1
ans = [[0] * m for i in range(n)]
for i in range(n):
	for j in range(m):
		less = max(yoko_less[i][j], tate_less[i][j])
		more = max(yoko_more[i][j], tate_more[i][j])
		ans[i][j] = 1 + more + less
for res in ans:
	print(*res)


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


import sys
from bisect import bisect_left
input = sys.stdin.buffer.readline
(n, m) = map(int, input().split())
a = [list(map(int, input().split())) for i in range(n)]
lr_less = [[0] * m for i in range(n)]
lr_more = [[0] * m for i in range(n)]
for i in range(n):
	lr = sorted(set(a[i]))
	for j in range(m):
		idx = bisect_left(lr, a[i][j])
		lr_less[i][j] = idx
		lr_more[i][j] = len(lr) - 1 - idx
du_less = [[0] * m for i in range(n)]
du_more = [[0] * m for i in range(n)]
for j in range(m):
	du = sorted(set([a[i][j] for i in range(n)]))
	for i in range(n):
		idx = bisect_left(du, a[i][j])
		du_less[i][j] = idx
		du_more[i][j] = len(du) - 1 - idx
ans = [[0] * m for i in range(n)]
for i in range(n):
	for j in range(m):
		less = max(lr_less[i][j], du_less[i][j])
		more = max(lr_more[i][j], du_more[i][j])
		ans[i][j] = less + more + 1
for z in ans:
	print(*z)


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