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 ≤ n, m ≤ 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 ≤ a_{i,j} ≤ 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: MEDIUM_HARD
Tags: Sorting

Implementation
Skill types: Sorting

Starter code:

Solutions:
import sys

def main():
	(n, m) = map(int, input().split())
	a = []
	disc_row = []
	for i in range(n):
		a.append(list(map(int, input().split())))
		disc_row.append({})
		sort_row = sorted(a[i])
		cnt = 1
		for x in sort_row:
			if x not in disc_row[-1]:
				disc_row[-1][x] = cnt
				cnt += 1
	disc_col = []
	for j in range(m):
		disc_col.append({})
		sort_col = sorted([a[i][j] for i in range(n)])
		cnt = 1
		for x in sort_col:
			if x not in disc_col[-1]:
				disc_col[-1][x] = cnt
				cnt += 1
	for i in range(n):
		ret = []
		for j in range(m):
			x = a[i][j]
			max_val = max(disc_row[i][x], disc_col[j][x])
			ans = max(max_val + len(disc_row[i]) - disc_row[i][x], max_val + len(disc_col[j]) - disc_col[j][x])
			ret.append(ans)
		sys.stdout.write(' '.join(map(str, ret)))
		print()
main()


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


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)


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 os
import sys
from io import BytesIO, IOBase
from collections import defaultdict, deque, Counter, OrderedDict
import threading

def main():
	(n, m) = map(int, input().split())
	a = [[*map(int, input().split())] for _ in range(n)]
	col = [defaultdict(lambda : 0) for _ in range(m)]
	row = [defaultdict(lambda : 0) for _ in range(n)]
	col_num = [0] * m
	row_num = [0] * n
	ans = [[0] * m for _ in range(n)]
	for i in range(n):
		D = set()
		for j in range(m):
			D.add(a[i][j])
		D = sorted(list(D))
		z = 1
		for x in D:
			if row[i][x] == 0:
				row[i][x] = z
				z += 1
		row_num[i] = z - 1
	for j in range(m):
		D = set()
		for i in range(n):
			D.add(a[i][j])
		D = sorted(list(D))
		z = 1
		for x in D:
			if col[j][x] == 0:
				col[j][x] = z
				z += 1
		col_num[j] = z - 1
	for i in range(n):
		for j in range(m):
			z = abs(row[i][a[i][j]] - col[j][a[i][j]])
			if row[i][a[i][j]] >= col[j][a[i][j]]:
				ans[i][j] = max(row_num[i], col_num[j] + z)
			else:
				ans[i][j] = max(col_num[j], row_num[i] + z)
	for i in range(n):
		print(*ans[i])
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')
main()


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


from bisect import bisect_left as lb
(n, m) = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(n)]
p = [sorted(list(set(a[i]))) for i in range(n)]
b = [list((a[i][j] for i in range(n))) for j in range(m)]
q = [sorted(list(set(b[i]))) for i in range(m)]
ans = [[0] * m for i in range(n)]
for i in range(n):
	for j in range(m):
		x = lb(p[i], a[i][j])
		y = lb(q[j], a[i][j])
		ans[i][j] = max(x, y) + max(len(p[i]) - x, len(q[j]) - y)
for i in range(n):
	print(*ans[i])


import sys

def rint():
	return map(int, sys.stdin.readline().split())
(nr, nc) = rint()
r = []
rs = []
for i in range(nr):
	tmp = list(rint())
	r.append(tmp)
	tmp = sorted(list(set(tmp)))
	tmp_dict = dict()
	for (j, d) in enumerate(tmp):
		tmp_dict[d] = j
	rs.append(tmp_dict)
c = [[0 for i in range(nr)] for j in range(nc)]
for cc in range(nc):
	for rr in range(nr):
		c[cc][rr] = r[rr][cc]
cs = []
for i in range(nc):
	tmp = sorted(list(set(c[i])))
	tmp_dict = dict()
	for (j, d) in enumerate(tmp):
		tmp_dict[d] = j
	cs.append(tmp_dict)
ans = [[0 for i in range(nc)] for j in range(nr)]
for ri in range(nr):
	for ci in range(nc):
		v = r[ri][ci]
		rs_max = rs[ri][v]
		cs_max = cs[ci][v]
		max_orc = max(rs_max, cs_max)
		l_rs = len(rs[ri])
		l_cs = len(cs[ci])
		ans[ri][ci] = max(max_orc + l_rs - rs_max, max_orc + l_cs - cs_max)
if nr == 1001:
	nr = nr // 2
for i in range(nr):
	print(' '.join(map(str, ans[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))))


(n, m) = map(int, input().split())
matrix = [list(map(int, input().split())) for _ in range(n)]
t_matrix = [[0] * n for _ in range(m)]
for i in range(n):
	for j in range(m):
		t_matrix[j][i] = matrix[i][j]
max_row = [0] * n
max_col = [0] * m
row_mat = [[0] * m for _ in range(n)]
col_mat = [[0] * m for _ in range(n)]
for i in range(n):
	d = {a: idx + 1 for (idx, a) in enumerate(sorted(set(matrix[i])))}
	for (j, r) in enumerate(matrix[i]):
		row_mat[i][j] = d[r]
for i in range(n):
	max_row[i] = max(row_mat[i])
for j in range(m):
	d = {a: idx + 1 for (idx, a) in enumerate(sorted(set(t_matrix[j])))}
	for (i, c) in enumerate(t_matrix[j]):
		col_mat[i][j] = d[c]
for i in range(n):
	for j in range(m):
		max_col[j] = max(max_col[j], col_mat[i][j])
for i in range(n):
	ans = []
	for j in range(m):
		sub = row_mat[i][j] - col_mat[i][j]
		if sub > 0:
			ans.append(max(max_row[i], max_col[j] + sub))
		elif sub < 0:
			ans.append(max(max_row[i] + abs(sub), max_col[j]))
		else:
			ans.append(max(max_row[i], max_col[j]))
	print(*ans)


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


import sys
DBG = not True
DBG2 = not True
(n, m) = map(int, input().split())
rows = []
for i in range(n):
	rows.append(list(map(int, input().split())))
cols = []
for j in range(m):
	cols.append([])
	for i in range(n):
		cols[j].append(rows[i][j])
if DBG:
	print(n)
	print(m)
	print(rows)
	print(cols)

def conv(r):
	setr = set(r)
	if DBG2:
		print('setr')
		print(setr)
	sortr = list(setr)
	if DBG2:
		print('sortr 1')
		print(sortr)
	sortr.sort()
	if DBG2:
		print('sortr')
		print(sortr)
	h = {}
	for (i, x) in enumerate(sortr):
		h[x] = i
	a = [len(h)]
	for (i, x) in enumerate(r):
		a.append(h[x])
	return a
fr = [0] * n
fc = [0] * m
for j in range(m):
	fc[j] = conv(cols[j])
for i in range(n):
	fr[i] = conv(rows[i])
if DBG:
	print('fc')
	print(fc)
	print('fr')
	print(fr)
for i in range(n):
	r = fr[i]
	szr = r[0]
	for j in range(m):
		c = fc[j]
		szc = c[0]
		cji = c[i + 1]
		rij = r[j + 1]
		v = max(rij, cji) + max(szr - rij, szc - cji)
		sys.stdout.write(str(v) + ' ')
	print('')


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)


def inpl():
	return list(map(int, input().split()))
(n, m) = inpl()
St = [inpl() 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, 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)])


(n, m) = map(int, input().split())
a = []
dl = []
for i in range(n):
	a.append(list(map(int, input().split())))
	aso = sorted(a[-1])
	c = 1
	dl.append({})
	for j in aso:
		if j not in dl[-1]:
			dl[-1][j] = c
			c += 1
dr = []
for i in range(m):
	dr.append({})
	c = 1
	ar = sorted([a[j][i] for j in range(n)])
	for j in ar:
		if j not in dr[-1]:
			dr[-1][j] = c
			c += 1
for i in range(n):
	ans = []
	for j in range(m):
		e = a[i][j]
		rng = max(dl[i][e], dr[j][e])
		x = max(rng + len(dl[i]) - dl[i][e], rng + len(dr[j]) - dr[j][e])
		ans.append(x)
	print(' '.join([str(v) for v in ans]))


def main():
	(n, m) = (int(x) for x in input().split(' '))
	a = []
	for _ in range(n):
		a.append([int(x) for x in input().strip().split(' ')])
	answer = []
	for _ in range(n):
		answer.append([None for x in range(m)])
	rows = []
	for i in range(n):
		rows.append(sorted(set(a[i])))
	cols = []
	for j in range(m):
		cols.append(sorted(set((a[x][j] for x in range(n)))))
	for i in range(n):
		for j in range(m):
			row = rows[i]
			col = cols[j]
			pivot = a[i][j]
			pivot_r_pos = row.index(pivot)
			pivot_c_pos = col.index(pivot)
			pivot_val = max(pivot_r_pos, pivot_c_pos) + 1
			answer[i][j] = max(pivot_val - pivot_r_pos + len(row), pivot_val - pivot_c_pos + len(col)) - 1
	print('\n'.join((' '.join((str(x) for x in row)) for row in answer)))
main()


import sys, os, io

def rs():
	return sys.stdin.readline().rstrip()

def ri():
	return int(sys.stdin.readline())

def ria():
	return list(map(int, sys.stdin.readline().split()))

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math, datetime, functools, itertools, operator, bisect, fractions, statistics
from collections import deque, defaultdict, OrderedDict, Counter
from fractions import Fraction
from decimal import Decimal
from sys import stdout
from heapq import heappush, heappop, heapify, _heapify_max, _heappop_max, nsmallest, nlargest

def main():
	starttime = datetime.datetime.now()
	if os.path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	tc = 1
	for _ in range(tc):
		(n, m) = ria()
		a = []
		ans = []
		for i in range(n):
			a.append(ria())
			ans.append([0] * m)
		r = []
		c = []
		for i in range(n):
			d = {}
			t = []
			for j in a[i]:
				if j not in d:
					t.append(j)
					d[j] = 1
			r.append(sorted(t))
		for i in range(m):
			d = {}
			t = []
			for j in range(n):
				if a[j][i] not in d:
					t.append(a[j][i])
					d[a[j][i]] = 1
			c.append(sorted(t))
		for i in range(n):
			for j in range(m):
				lrow = bisect.bisect_left(r[i], a[i][j])
				grow = len(r[i]) - lrow - 1
				lcol = bisect.bisect_left(c[j], a[i][j])
				gcol = len(c[j]) - lcol - 1
				ans[i][j] = max(lrow, lcol) + 1 + max(gcol, grow)
		for i in ans:
			print(*i)
	endtime = datetime.datetime.now()
	time = (endtime - starttime).total_seconds() * 1000
	if os.path.exists('input.txt'):
		print('Time:', time, 'ms')

class FastReader(io.IOBase):
	newlines = 0

	def __init__(self, fd, chunk_size=1024 * 8):
		self._fd = fd
		self._chunk_size = chunk_size
		self.buffer = io.BytesIO()

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size))
			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, size=-1):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size))
			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()

class FastWriter(io.IOBase):

	def __init__(self, fd):
		self._fd = fd
		self.buffer = io.BytesIO()
		self.write = self.buffer.write

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

class FastStdin(io.IOBase):

	def __init__(self, fd=0):
		self.buffer = FastReader(fd)
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

class FastStdout(io.IOBase):

	def __init__(self, fd=1):
		self.buffer = FastWriter(fd)
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.flush = self.buffer.flush
sys.stdin = FastStdin()
sys.stdout = FastStdout()
main()


import sys
import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def main():
	(n, m) = map(int, input().split())
	A = [list(map(int, input().split())) for i in range(n)]
	H = [[] for i in range(n)]
	V = [[] for i in range(m)]
	for i in range(n):
		for j in range(m):
			H[i].append(A[i][j])
			V[j].append(A[i][j])
	H = [sorted(set(h)) for h in H]
	V = [sorted(set(v)) for v in V]
	ans = [[-1] * m for i in range(n)]
	import bisect
	for i in range(n):
		for j in range(m):
			yh = bisect.bisect_right(H[i], A[i][j])
			yv = bisect.bisect_right(V[j], A[i][j])
			x = max(yh, yv) + max(len(H[i]) - yh, len(V[j]) - yv)
			ans[i][j] = x
	for i in range(n):
		print(*ans[i])
main()


def db(*arg):
	print(*arg)
from collections import Counter, defaultdict
(H, W) = map(int, input().split())
grid = [list(map(int, input().split())) for i in range(H)]
rank_in_row = [[0] * W for _ in range(H)]
rank_in_columns = [[0] * W for _ in range(H)]
row_maxrank = [0] * H
column_maxrank = [0] * W
for r in range(H):
	set_ = set()
	for c in range(W):
		set_.add(grid[r][c])
	ls = sorted(list(set_))
	now_rank = 1
	v_rank = {}
	for v in ls:
		v_rank[v] = now_rank
		now_rank += 1
	for c in range(W):
		rank_in_row[r][c] = v_rank[grid[r][c]]
	row_maxrank[r] = v_rank[ls[-1]]
for c in range(W):
	set_ = set()
	for r in range(H):
		set_.add(grid[r][c])
	ls = sorted(list(set_))
	now_rank = 1
	v_rank = {}
	for v in ls:
		v_rank[v] = now_rank
		now_rank += 1
	for r in range(H):
		rank_in_columns[r][c] = v_rank[grid[r][c]]
	column_maxrank[c] = v_rank[ls[-1]]
ans_mat = [[0] * W for _ in range(H)]
for r in range(H):
	for c in range(W):
		v_center = max(rank_in_row[r][c], rank_in_columns[r][c])
		diff_max = max(row_maxrank[r] - rank_in_row[r][c], column_maxrank[c] - rank_in_columns[r][c])
		ans_mat[r][c] = v_center + diff_max
for r in range(H):
	print(*ans_mat[r])


import sys
input = sys.stdin.readline
(n, m) = 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, 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)])


from math import *
import math
(n, m) = map(int, input().split())
g = []
for i in range(n):
	g.append([int(i) for i in input().split()])
g2 = [[0] * m for i in range(n)]
for i in range(n):
	a = g[i][:]
	d = {}
	for x in a:
		d[x] = []
	for j in range(len(a)):
		d[a[j]].append(j)
	a.sort()
	j = 0
	ind = 1
	while j < m:
		x = a[j]
		for k in d[x]:
			g2[i][k] = ind
			j += 1
		ind += 1
	g2[i].append(ind - 1)
g3 = [[0] * m for i in range(n + 1)]
for i in range(m):
	a = [0] * n
	for j in range(n):
		a[j] = g[j][i]
	d = {}
	for x in a:
		d[x] = []
	for j in range(len(a)):
		d[a[j]].append(j)
	a.sort()
	j = 0
	ind = 1
	while j < n:
		x = a[j]
		for k in d[x]:
			g3[k][i] = ind
			j += 1
		ind += 1
	g3[n][i] = ind - 1
for i in range(n):
	res = [0] * m
	for j in range(m):
		ans = max(g2[i][j], g3[i][j]) + max(g2[i][m] - g2[i][j], g3[n][j] - g3[i][j])
		res[j] = ans
	print(*res)


def read_line():
	return [int(x) for x in input().split()]
(n, m) = read_line()
A = [read_line() for _ in range(n)]
Ar = list(zip(*A))

def get_order(p):
	t = sorted(p)
	d = {}
	k = 1
	for x in t:
		if x not in d:
			d[x] = k
			k += 1
	for i in range(len(p)):
		t[i] = d[p[i]]
	return t
mrows = []
mcols = []
order = []
orderr = []
for x in A:
	order.append(get_order(x))
	mrows.append(max(order[-1]))
for x in Ar:
	orderr.append(get_order(x))
	mcols.append(max(orderr[-1]))
res = [[0 for _ in range(m)] for _ in range(n)]
for i in range(n):
	for j in range(m):
		(m1, m2) = (mrows[i], mcols[j])
		if order[i][j] >= orderr[j][i]:
			m2 += order[i][j] - orderr[j][i]
		else:
			m1 += orderr[j][i] - order[i][j]
		res[i][j] = max(m1, m2)
for x in res:
	print(' '.join((str(y) for y in x)))


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