Our Chef is very happy that his son was selected for training in one of the finest culinary schools of the world.
So he and his wife decide to buy a gift for the kid as a token of appreciation.
Unfortunately, the Chef hasn't been doing good business lately, and is in no mood on splurging money.
On the other hand, the boy's mother wants to buy something big and expensive.
To settle the matter like reasonable parents, they play a game.

They spend the whole day thinking of various gifts and write them down in a huge matrix.
Each cell of the matrix contains the gift's cost.
Then they decide that the mother will choose a row number r while the father will choose a column number c,
the item from the corresponding cell will be gifted to the kid in a couple of days. 

The boy observes all of this secretly.
He is smart enough to understand that his parents will ultimately choose a gift whose cost is smallest in its row,
but largest in its column.
If no such gift exists, then our little chef has no option but to keep guessing.
As the matrix is huge, he turns to you for help.

He knows that sometimes the gift is not determined uniquely even if a gift exists whose cost is smallest in its row,
but largest in its column.
However, since the boy is so smart, he realizes that the gift's cost is determined uniquely.
Your task is to tell him the gift's cost which is smallest in its row,
but largest in its column, or to tell him no such gift exists.

-----Input-----
First line contains two integers R and C, the number of rows and columns in the matrix respectively. Then follow R lines, each containing C space separated integers - the costs of different gifts.

-----Output-----
Print a single integer - a value in the matrix that is smallest in its row but highest in its column. If no such value exists, then print "GUESS" (without quotes of course) 

-----Constraints-----
1 <= R, C <= 100 
All gift costs are positive and less than 100000000 (10^8) 

-----Example 1-----
Input:
2 3
9 8 8
2 6 11

Output:
8

-----Example 2-----
Input:
3 3
9 8 11
2 6 34
5 9 11

Output:
GUESS

-----Example 3-----
Input:
2 2
10 10
10 10

Output:
10

-----Explanation of Sample Cases-----
Example 1: The first row contains 9, 8, 8. Observe that both 8 are the minimum. Considering the first 8, look at the corresponding column (containing 8 and 6). Here, 8 is the largest element in that column. So it will be chosen.
Example 2: There is no value in the matrix that is smallest in its row but largest in its column.
Example 3: The required gift in matrix is not determined uniquely, but the required cost is determined uniquely.

Source: codechef | Difficulty: EASY
Tags: Implementation

Complete search


Solutions:
(r, c) = [int(x) for x in input('').split()]
l1 = []
l2 = []
l = []
for i in range(r):
	t = [int(x) for x in input('').split()]
	l1.append(t)
for i in range(c):
	for j in range(r):
		l.append(l1[j][i])
	l2.append(l)
	l = []
for i in range(r):
	for j in range(c):
		if l1[i][j] == min(l1[i]) and l1[i][j] == max(l2[j]):
			t = 1
			print(l1[i][j])
			break
	if t == 1:
		break
else:
	print('GUESS')


(r, c) = map(int, input().strip().split())
main = []
row = []
col = []
for j in range(r):
	a = list(map(int, input().strip().split()))
	main.append(a)
	row.append(min(a))
count = 0
for i in range(c):
	max = 0
	for k in range(r):
		if max < main[k][i]:
			max = main[k][i]
	col.append(max)
for i in range(r):
	n = row[i]
	for j in range(c):
		if main[i][j] == n and col[j] == n:
			count = 1
			break
	if count == 1:
		print(n)
		break
if count == 0:
	print('GUESS')


import numpy as np
(r, c) = map(int, input().split())
li = []
for _ in range(r):
	li.append(list(map(int, input().split())))
li = np.array(li)
fl = 0
for i in range(r):
	for j in range(c):
		if li[i][j] == min(li[i]) and li[i][j] == max(li[:, j]):
			print(li[i][j])
			fl = 1
			break
	if fl == 1:
		break
else:
	print('GUESS')


(r, c) = map(int, input().split())
k = []
for i in range(r):
	a = []
	a = list(map(int, input().split()))
	k.append(a)
rmin = []
cmax = []
for i in range(r):
	rmin.append(min(k[i]))
for j in range(c):
	b = k[0][j]
	for i in range(r):
		if b < k[i][j]:
			b = k[i][j]
	cmax.append(b)
if set(rmin).intersection(set(cmax)):
	print(*set(rmin).intersection(set(cmax)))
else:
	print('GUESS')


(r, c) = map(int, input().strip().split())
matrix = []
minRow = []
for i in range(r):
	arr = list(map(int, input().strip().split()))
	minRow.append(min(arr))
	matrix.append(arr)
flag = 1
maxCol = []
for j in range(c):
	maxi = -1
	for i in range(r):
		maxi = max(maxi, matrix[i][j])
	maxCol.append(maxi)
for i in range(r):
	n = minRow[i]
	for j in range(c):
		if matrix[i][j] == n and maxCol[j] == n:
			flag = 0
			break
	if flag == 0:
		break
if flag == 0:
	print(n)
else:
	print('GUESS')


(r, c) = map(int, input().split())
m = []
rowmax = []
colmax = []
row = []
col = []
rowmin = []
colmax = []
gift = 0
for i in range(r):
	row = list(map(int, input().split()))
	m.append(row)
	rowmin.append(min(row))
for j in range(c):
	col = []
	for i in range(r):
		col.append(m[i][j])
	colmax.append(max(col))
for i in range(r):
	for j in range(c):
		if m[i][j] == rowmin[i] and m[i][j] == colmax[j]:
			gift = m[i][j]
if gift:
	print(gift)
else:
	print('GUESS')


(r, c) = list(map(int, input().split(' ')))
mat = []
small_mat = []
result = 'GUESS'
for i in range(r):
	data = list(map(int, input().split(' ')))
	small_mat.append(min(data))
	mat.append(data)
for i in range(r):
	for j in range(c):
		if mat[i][j] == small_mat[i]:
			temp_data = []
			for k in range(r):
				temp_data.append(mat[k][j])
			if max(temp_data) == small_mat[i]:
				result = small_mat[i]
				break
print(result)


(r, c) = map(int, input().split())
m1 = []
m2 = []
for i in range(r):
	s = list(map(int, input().split()))
	m1.append(min(s))
	m2.append(s)
n = []
for j in range(c):
	x = []
	for k in range(r):
		x.append(m2[k][j])
	n.append(max(x))
f = 0
for z in m1:
	if z in n:
		print(z)
		f = 1
		break
if f == 0:
	print('GUESS')


(n, m) = map(int, input().split())
min_lst = []
max_lst = [[]] * m
mx = [0] * m
for r in range(n):
	tmp = list(map(int, input().split()))
	mn = tmp[0]
	tmp_lst = []
	j = 0
	for i in tmp:
		if i > mx[j]:
			mx[j] = i
			max_lst[j] = [(r, j, i)]
		elif i == mx[j]:
			max_lst[j].append((r, j, i))
		if i < mn:
			mn = i
			tmp_lst = [(r, j, i)]
		elif i == mn:
			tmp_lst.append((r, j, i))
		j += 1
	min_lst.extend(tmp_lst)
flg = False
for i in max_lst:
	for j in i:
		if j in min_lst:
			print(j[2])
			flg = True
			exit(0)
if not flg:
	print('GUESS')


def f1(a, a1, r, c):
	for i in range(r):
		for j in range(c):
			if min(a[i]) == max(a1[j]):
				return (True, min(a[i]))
	else:
		return (False, 0)
(r, c) = map(int, input().split())
l = []
l2 = [[] for i in range(c)]
for i in range(r):
	l1 = list(map(int, input().split()))
	for i in range(c):
		l2[i].append(l1[i])
	l.append(l1)
(a, b) = f1(l, l2, r, c)
if a is True:
	print(b)
else:
	print('GUESS')


(r, c) = map(int, input().split())
g = []
l = set()
m = set()
f = []
for i in range(r):
	g.append(list(map(int, input().split())))
	for (index, item) in enumerate(g[i]):
		if item == min(g[i]):
			l.add((i, index))
for j in range(c):
	for (index, item) in enumerate([i[j] for i in g]):
		if item == max([i[j] for i in g]):
			m.add((index, j))
			if (index, j) in l:
				f.append((index, j))
if len(f) != 0:
	print(g[f[0][0]][f[0][1]])
else:
	print('GUESS')


try:
	(r, c) = map(int, input().split())
	mat = []
	for i in range(r):
		arr = list(map(int, input().split()))
		mat.append(arr)
	flag = 0
	arr1 = []
	for x in range(c):
		arr = []
		for j in range(r):
			arr.append(mat[j][x])
		arr1.append(arr)
	i = 0
	for i in range(r):
		for j in range(c):
			if mat[i][j] == max(arr1[j]) and mat[i][j] == min(mat[i]):
				flag = 1
				print(mat[i][j])
				break
		if flag == 1:
			break
	if flag == 0:
		print('GUESS')
except:
	pass


(row, col) = map(int, input().split(' '))
matrix = list()
for _ in range(row):
	matrix.append(list(map(int, input().split(' '))))
maxcols = list()
for j in range(col):
	maxcols.append(max([matrix[i][j] for i in range(row)]))
for i in range(row):
	minrow = min(matrix[i])
	if minrow in maxcols:
		print(minrow)
		break
else:
	print('GUESS')


(r, c) = map(int, input().strip().split())
a = []
for i in range(r):
	b = list(map(int, input().strip().split()))
	a.append(b)
row = []
for i in a:
	row.append(min(i))
col = [0] * c
for i in range(c):
	for j in range(r):
		col[i] = max(col[i], a[j][i])
f = 1
for i in row:
	for j in col:
		if i == j:
			print(i)
			f = 0
			break
	if f == 0:
		break
if f == 1:
	print('GUESS')


(rows, cols) = map(int, input().split())
matrix = []
for i in range(rows):
	matrix.append(list(map(int, input().split())))
ans = 'GUESS'
for i in range(rows):
	minimum = min(matrix[i])
	for j in range(cols):
		true_minimum = True
		if matrix[i][j] == minimum:
			for k in range(rows):
				if matrix[k][j] > minimum:
					true_minimum = False
					break
			if true_minimum:
				ans = minimum
				break
	if ans != 'GUESS':
		break
print(ans)


(r, c) = map(int, input().split())
mat = []
for i in range(r):
	mat.append(list(map(int, input().split())))
flag = 0
for i in range(r):
	min = 10000000000000
	col = []
	for j in range(c):
		if mat[i][j] < min:
			min = mat[i][j]
	for j in range(c):
		if mat[i][j] == min:
			col.append(j)
	for m in col:
		max = 0
		for k in range(r):
			if mat[k][m] > max:
				max = mat[k][m]
		if max == min:
			flag = 1
			print(max)
			break
	if flag == 1:
		break
if flag == 0:
	print('GUESS')


try:
	(trs, ctc) = map(int, input().split())
	wat = []
	for i in range(trs):
		ujju = list(map(int, input().split()))
		wat.append(ujju)
	bye = 0
	ujju1 = []
	for v in range(ctc):
		ujju = []
		for s in range(trs):
			ujju.append(wat[s][v])
		ujju1.append(ujju)
	p = 0
	for p in range(trs):
		for s in range(ctc):
			if wat[p][s] == max(ujju1[s]) and wat[p][s] == min(wat[p]):
				bye = 1
				print(wat[p][s])
				break
		if bye == 1:
			break
	if bye == 0:
		print('GUESS')
except:
	pass


from collections import defaultdict
(n, m) = map(int, input().split())
matrix = []
row = defaultdict(list)
column = defaultdict(list)
for i in range(n):
	x = list(map(int, input().split()))
	matrix.append(x)
	row[i] = [i for (i, j) in enumerate(x) if min(x) == j]
x = list(zip(*matrix))
for i in range(len(x)):
	column[i] = [m for (m, j) in enumerate(x[i]) if max(x[i]) == j]
flag = 0
for i in range(n):
	for j in range(m):
		if j in row[i] and i in column[j]:
			flag = 1
			print(matrix[i][j])
			break
	if flag == 1:
		break
if flag != 1:
	print('GUESS')


(r, c) = map(int, input().split())
rows = []
for y in range(r):
	rows += [list(map(int, input().split()))]
found = -1
for x in range(c):
	cmaxi = 0
	for y in range(1, r):
		if rows[y][x] > rows[cmaxi][x]:
			cmaxi = y
	for y in range(0, r):
		if rows[y][x] == rows[cmaxi][x]:
			if rows[y][x] == min(rows[y]):
				found = rows[y][x]
				break
	if found >= 0:
		break
if found >= 0:
	print(found)
else:
	print('GUESS')


(r, c) = map(int, input().split())
l1 = []
for val in range(0, r):
	k = list(map(int, input().split()))
	l1.append(k)
l2 = []
for val in range(0, c):
	l3 = []
	for gh in range(0, r):
		l3.append(l1[gh][val])
	l2.append(max(l3))
p = 0
for val in range(0, r):
	kp = min(l1[val])
	for vbc in range(0, len(l1[val])):
		if kp == l1[val][vbc] and kp == l2[vbc]:
			p = 1
			break
	if p == 1:
		break
if p == 0:
	print('GUESS')
else:
	print(kp)


def abc(l, k, n, m):
	d = []
	for i in range(n):
		x = min(l[i])
		for j in range(m):
			if l[i][j] == x:
				if k[j] == x:
					d.append(x)
	if d:
		return max(d)
	return 'GUESS'
(n, m) = map(int, input().split())
l = []
for i in range(n):
	l.append(list(map(int, input().split())))
k = []
for i in range(m):
	c = l[0][i]
	for j in range(n):
		if l[j][i] > c:
			c = l[j][i]
	k.append(c)
print(abc(l, k, n, m))


(r, c) = map(int, input().strip().split())
matrix = []
for i in range(r):
	matrix.append(list(map(int, input().split())))
maxcol = []
for j in range(c):
	maxcol.append(max((matrix[i][j] for i in range(r))))
for i in range(r):
	minrow = min(matrix[i])
	if minrow in maxcol:
		print(minrow)
		break
else:
	print('GUESS')


(r, c) = map(int, input().split())
matrix = []
l = []
col = []
m = 1
for i in range(r):
	ele = list(map(int, input().split()))
	matrix.append(ele)
for i in range(c):
	for j in range(r):
		l.append(matrix[j][i])
	col.append(l)
	l = []
for i in range(r):
	for j in range(c):
		if matrix[i][j] == min(matrix[i]) and matrix[i][j] == max(col[j]):
			m = 0
			print(matrix[i][j])
			break
	if m == 0:
		break
else:
	print('GUESS')


from numpy import *
(r, c) = map(int, input().split())
x = []
for i in range(r):
	y = list(map(int, input().split()))
	x.append(y)
x = array(x)
b = x.transpose()
for i in x:
	c = []
	i = list(i)
	a = min(i)
	ab = 0
	for k in range(len(i)):
		if i[k] == a:
			c.append(k)
	for k in c:
		d = max(list(b[k]))
		if a == d:
			print(a)
			ab += 1
			break
	if ab > 0:
		break
else:
	print('GUESS')


def solution(mat, R, C):
	for i in range(R):
		for j in range(C):
			flag = 1
			for k in range(C):
				if mat[i][k] < mat[i][j]:
					flag = 0
					break
			if flag != 0:
				for k in range(R):
					if mat[k][j] > mat[i][j]:
						flag = 0
						break
			if flag == 1:
				return mat[i][j]
	return 'GUESS'
(R, C) = map(int, input().split())
mat = []
for i in range(R):
	mat.append(list(map(int, input().split())))
print(solution(mat, R, C))


(row, col) = map(int, input().split())
m = [[int(x) for x in input().split()] for y in range(row)]
minL = []
maxL = []
ans = []
for i in range(row):
	minVal = 10 ** 12
	for j in range(col):
		if minVal >= m[i][j]:
			minVal = m[i][j]
	minL.append(minVal)
for i in range(col):
	maxVal = 0
	for j in range(row):
		if maxVal <= m[j][i]:
			maxVal = m[j][i]
	maxL.append(maxVal)
for i in minL:
	for j in maxL:
		if i == j:
			ans.append(i)
if ans:
	print(max(ans))
else:
	print('GUESS')


(r, c) = map(int, input().split())
l = list()
sm = list()
for ni in range(r):
	a = list(map(int, input().split()))
	sm.append(min(a))
	l.append(a)
col = list()
maxc = 0
ma = list()
for ni in range(c):
	for nj in range(r):
		maxc = max(maxc, l[nj][ni])
	ma.append(maxc)
	maxc = 0
(exists, value) = (0, 0)
for i in range(c):
	for j in range(r):
		if ma[i] == sm[j]:
			exists = 1
			value = ma[i]
			break
if exists == 0:
	print('GUESS')
else:
	print(value)


(r, c) = input().strip().split()
r = int(r)
c = int(c)
a = []
low = []
high = []
for i in range(0, r):
	b = list(map(int, input().strip().split()))
	a.append(b)
	low.append(min(b))
for j in range(0, c):
	ma = 0
	for k in range(0, r):
		if a[k][j] > ma:
			ma = a[k][j]
	high.append(ma)
u = 0
for h in high:
	if h in low:
		u += 1
		print(h)
		break
if u == 0:
	print('GUESS')


from sys import stdin, stdout
(r, c) = map(int, stdin.readline().strip().split())
gift_list = [list(map(int, stdin.readline().strip().split())) for _ in range(r)]
max_list = [max(i) for i in zip(*gift_list)]
found = False
for i in range(r):
	min_row = min(gift_list[i])
	for j in range(c):
		if gift_list[i][j] == min_row and gift_list[i][j] == max_list[j]:
			print(gift_list[i][j])
			found = True
			break
	if found:
		break
else:
	print('GUESS')


(r, c) = map(int, input().split())
mi = []
m = []
for i in range(r):
	s = list(map(int, input().split()))
	mi.append(min(s))
	m.append(s)
n = []
for i in range(c):
	x = []
	for j in range(r):
		x.append(m[j][i])
	n.append(max(x))
flag = 0
for i in mi:
	if i in n:
		print(i)
		flag = 1
		break
if flag == 0:
	print('GUESS')


(a, b) = map(int, input().split())
lh = []
lv = []
bye = 0
l = []
for i in range(a):
	t = list(map(int, input().split()))
	lh.append(t)
for i in range(b):
	for m in range(a):
		l.append(lh[m][i])
	lv.append(l)
	l = []
for i in range(a):
	for j in range(b):
		if lh[i][j] == min(lh[i]) and lh[i][j] == max(lv[j]):
			t = 1
			print(lh[i][j])
			break
	if t == 1:
		break
else:
	print('GUESS')


(R, C) = map(int, input().strip().split())
smallInRow = [1000000000] * R
largeInCol = [0] * C
A = []
for i in range(R):
	A.append([int(x) for x in input().strip().split()])
for i in range(R):
	for j in range(C):
		smallInRow[i] = min(smallInRow[i], A[i][j])
		largeInCol[j] = max(largeInCol[j], A[i][j])
there = False
for i in range(R):
	for j in range(C):
		if A[i][j] == smallInRow[i] and A[i][j] == largeInCol[j]:
			there = True
			val = A[i][j]
if there:
	print(val)
else:
	print('GUESS')


(R, C) = map(int, input().split())
MAT = []
for r in range(R):
	MAT.append(list(map(int, input().split())))
colMax = []
for c in range(C):
	maxi = -1
	for r in range(R):
		if MAT[r][c] > maxi:
			maxi = MAT[r][c]
	colMax.append(maxi)
rowMin = []
for r in range(R):
	mini = MAT[r][0]
	for c in range(1, C):
		if MAT[r][c] < mini:
			mini = MAT[r][c]
	rowMin.append(mini)
noGuess = False
element = -1
for i in rowMin:
	if i in colMax:
		element = i
		noGuess = True
		break
if noGuess:
	print(element)
else:
	print('GUESS')


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

def rmin(mat, r):
	row = mat[r]
	rm = min(row)
	return rm

def cmax(mat, c):
	col = list((_[c] for _ in mat))
	cm = max(col)
	return cm
rm = list((rmin(mat, _) for _ in range(n)))
cm = list((cmax(mat, _) for _ in range(m)))
num = 0
for _ in range(n):
	for __ in range(m):
		if mat[_][__] == rm[_]:
			if mat[_][__] == cm[__]:
				num = mat[_][__]
				break
	if num != 0:
		break
if num != 0:
	print(num)
else:
	print('GUESS')


(r, c) = [int(x) for x in input().split()]
matrix = []
for i in range(r):
	matrix.append([int(x) for x in input().split()])
score = [[[0, 0] for _ in range(c)] for _ in range(r)]
for i in range(r):
	curr = min(matrix[i])
	for j in range(c):
		score[i][j][0] = curr
cols = list(zip(*matrix))
for i in range(c):
	curr = max(cols[i])
	for j in range(r):
		score[j][i][1] = curr
answer = 'GUESS'
for i in range(r):
	for j in range(c):
		if score[i][j][0] == score[i][j][1]:
			answer = score[i][j][0]
			break
	if answer != 'GUESS':
		break
print(answer)


(r, c) = map(int, input().split())
l = []
flag = 0
for i in range(r):
	k = list(map(int, input().split()))
	l.append(k)
ans = []
for i in range(r):
	min = 10 ** 12
	col = []
	for j in range(c):
		if l[i][j] < min:
			min = l[i][j]
	for j in range(c):
		if l[i][j] == min:
			col.append(j)
	for m in col:
		max = 0
		for k in range(r):
			if l[k][m] > max:
				max = l[k][m]
		if max == min:
			flag = 1
			print(max)
			break
	if flag == 1:
		break
if flag == 0:
	print('GUESS')


(r, c) = map(int, input().split())
mat = []
rmin = []
cmax = []
d = {}
f = 0
for i in range(r):
	l = list(map(int, input().split()))
	mat.append(l)
for i in range(r):
	x = min(mat[i])
	rmin.append(x)
for i in range(c):
	x = []
	for j in range(r):
		x.append(mat[j][i])
	cmax.append(max(x))
for i in rmin:
	if i in cmax:
		print(i)
		f = 1
		break
if f == 0:
	print('GUESS')


def main():
	(row, column) = map(int, input().strip().split())
	(minRow, maxCol) = (list(), [-1 for i in range(column)])
	matrix = list()
	for r in range(row):
		matrix.append(list(map(int, input().strip().split())))
	for r in range(row):
		minRow.append(min(matrix[r]))
	for c in range(column):
		for r in range(row):
			maxCol[c] = max(maxCol[c], matrix[r][c])
	rmax = max(minRow)
	cmin = min(maxCol)
	if rmax == cmin:
		print(rmax)
	else:
		print('GUESS')
main()


(r, c) = map(int, input().split())
l = []
(low, high) = ([], [])
for i in range(r):
	m = list(map(int, input().split()))
	l.append(m)
	low.append(min(m))
for j in range(c):
	n = 0
	li = []
	for k in range(r):
		li.append(l[k][j])
	high.append(max(li))
flag = 0
for i in low:
	if i in high:
		flag = 1
		print(i)
		break
if flag == 0:
	print('GUESS')


(r, c) = map(int, input().split())
mat = []
l = []
flag = 0
for i in range(r):
	t_1 = list(map(int, input().split()))
	l.append(min(t_1))
	mat.append(t_1)
r_1 = []
for i in range(c):
	x = []
	for j in range(r):
		x.append(mat[j][i])
	r_1.append(max(x))
for i in l:
	if i in r_1:
		print(i)
		flag = 1
		break
if flag == 0:
	print('GUESS')


(a, b) = map(int, input().split())
l1 = []
for i in range(0, a):
	l = list(map(int, input().split()))
	l1.append(l)
l2 = []
for j in range(0, b):
	s = []
	for k in range(0, a):
		s.append(l1[k][j])
	l2.append(s)
t = 0
for p in range(0, a):
	for q in range(0, b):
		if l1[p][q] == min(l1[p]) and l1[p][q] == max(l2[q]):
			print(l1[p][q])
			t = 1
			break
	if t == 1:
		break
if t == 0:
	print('GUESS')


(r, c) = map(int, input().split())
rows = []
max_column = []
for _ in range(r):
	rows.append(list(map(int, input().split())))
for i in range(c):
	max_local = 0
	for j in range(r):
		if rows[j][i] > max_local:
			max_local = rows[j][i]
	max_column.append(max_local)
for row in rows:
	for i in range(c):
		if row[i] == min(row) and row[i] == max_column[i]:
			print(row[i])
			exit()
print('GUESS')


(r, c) = (int(x) for x in input().split())
matrix = []
for _ in range(r):
	matrix.append([int(x) for x in input().split()])
transpose_matrix = [x for x in zip(*matrix)]
row_min = [min(matrix[i]) for i in range(r)]
col_max = [max(transpose_matrix[i]) for i in range(c)]
found = False
for r_min in row_min:
	for c_max in col_max:
		if r_min == c_max:
			print(r_min)
			found = True
			break
	if found:
		break
if not found:
	print('GUESS')


def __starting_point():
	(ROWS, COLS) = list(map(int, input().strip().split()))
	giftCost = set()
	matrix = []
	minRow = []
	for i in range(ROWS):
		arr = list(map(int, input().strip().split()))
		minRow.append(min(arr))
		matrix.append(arr)
	maxCol = []
	for j in range(COLS):
		maxi = -1
		for i in range(ROWS):
			maxi = max(maxi, matrix[i][j])
		maxCol.append(maxi)
	for i in range(ROWS):
		n = minRow[i]
		if n in giftCost:
			continue
		for j in range(COLS):
			if matrix[i][j] == n and maxCol[j] == n:
				giftCost.add(n)
			if len(giftCost) > 1:
				break
		if len(giftCost) > 1:
			break
	if len(giftCost) == 1:
		for n in giftCost:
			print(n)
	else:
		print('GUESS')
__starting_point()


x = input().split()
r = int(x[0])
c = int(x[1])
a = []
b = []
for i in range(r):
	a.append([])
	x = input().split()
	for j in range(c):
		a[i].append(int(x[j]))
minrow = []
maxcol = []
for i in range(r):
	m = a[i][0]
	for j in range(1, c):
		if a[i][j] < m:
			m = a[i][j]
	minrow.append(m)
for i in range(c):
	m = a[0][i]
	for j in range(1, r):
		if a[j][i] > m:
			m = a[j][i]
	maxcol.append(m)
flag = 0
for i in range(r):
	for j in range(c):
		if minrow[i] == maxcol[j]:
			b.append(minrow[i])
			flag = 1
if flag == 0:
	print('GUESS')
else:
	print(max(b))


def check_in_column(arr, value, index, X):
	for i in range(X):
		if arr[i][index] > value:
			return False
	return True

def result(arr, X, Y):
	for i in range(X):
		s = min(arr[i])
		for j in range(Y):
			if arr[i][j] == s:
				if check_in_column(arr, s, j, X):
					return s
	return 'GUESS'
(X, Y) = [int(d) for d in input().split()]
arr = []
for i in range(X):
	arr.append([int(d) for d in input().split()])
print(result(arr, X, Y))


(r, c) = map(int, input().split())
a = []
d = []
flag = 0
for i in range(0, r):
	b = list(map(int, input().split()))
	a.append(b)
	l = min(b)
	for k in range(0, c):
		if b[k] == l:
			d.append((i, k))
for i in range(0, c):
	k = a[0][i]
	for y in range(1, r):
		if a[y][i] > k:
			k = a[y][i]
	for y in range(0, r):
		if a[y][i] == k and d.count((y, i)) > 0:
			print(a[y][i])
			flag = 1
			break
	if flag == 1:
		break
if flag == 0:
	print('GUESS')


(r, c) = map(int, input().split())
arr = []
for i in range(0, r):
	arr.append([int(j) for j in input().split()])
cost = -1
for i in range(r):
	min_r = min(arr[i])
	for j in range(c):
		if arr[i][j] == min_r and max(list((row[j] for row in arr))) == min_r:
			cost = arr[i][j]
			break
if cost >= 0:
	print(cost)
else:
	print('GUESS')


(r, c) = map(int, input().split())
gifts = []
for i in range(0, r):
	gifts.append(list(map(int, input().split())))
minRow = []
maxCol = [-1] * c
for i in range(0, r):
	minRow.append(min(gifts[i]))
for j in range(0, c):
	for i in range(0, r):
		maxCol[j] = max(maxCol[j], gifts[i][j])
rMax = max(minRow)
cMin = min(maxCol)
if rMax == cMin:
	print(rMax)
else:
	print('GUESS')


l = []
h = []
co = 0
c = 0
s = 0
r = 0
(a, b) = map(int, input().split())
for i in range(a):
	m = list(map(int, input().split()))
	l.append(m)
for j in l:
	x = min(j)
	c += 1
	for k in range(b):
		if l[c - 1][k] == x:
			h.append(int(k))
	for q in h:
		co = 0
		for t in range(a):
			if l[t][q] <= x:
				co += 1
				if co == a:
					r = x
	if r != 0:
		print(r)
		s = 1
		break
if s == 0:
	print('GUESS')


(r, c) = [int(x) for x in input().split()]
(ans, v, ind) = (-1, 0, 0)
(row, col1) = ([], [[0] * r] * c)
col = []
val = [[0] * c]
for i in range(r):
	row.append([int(x) for x in input().split()])
for i in range(c):
	for j in range(r):
		col1[i][j] = row[j][i]
	col.append(col1[i][:])
for i in range(r):
	v = min(row[i])
	for j in range(c):
		if row[i][j] == v:
			ind = j
			if max(col[ind]) == v:
				ans = v
if ans != -1:
	print(ans)
else:
	print('GUESS')


def maxcol(m, r, k):
	ma = 0
	for i in range(r):
		ma = max(m[i][k], ma)
	return ma
(r, c) = map(int, input().split())
m = []
for i in range(r):
	l = list(map(int, input().split()))
	m.append(l)
f = False
for j in range(r):
	for k in range(c):
		if m[j][k] == min(m[j]) and m[j][k] == maxcol(m, r, k):
			print(m[j][k])
			f = True
			break
	if f == True:
		break
else:
	print('GUESS')


from sys import stdin, stdout

def sin():
	return stdin.readline().rstrip()

def listInput():
	return list(map(int, sin().split()))

def printBS(li):
	if not li:
		return
	for i in range(len(li) - 1):
		stdout.write('%d ' % li[i])
	stdout.write('%d\n' % li[-1])
(r, c) = listInput()
row = [0] * r
col = [0] * c
mat = [[i for i in listInput()] for _ in range(r)]
for i in range(r):
	row[i] = min(mat[i])
for i in range(c):
	col[i] = mat[0][i]
	for j in range(1, r):
		col[i] = max(col[i], mat[j][i])
for i in range(r):
	for j in range(c):
		if row[i] == col[j]:
			print(row[i])
			exit(0)
else:
	print('GUESS')


(r, c) = map(int, input().split())
matrix_row = []
for _ in range(r):
	matrix_row.append([int(x) for x in input().split()])
matrix_column = []
for i in range(c):
	arr = []
	for j in range(r):
		arr.append(matrix_row[j][i])
	matrix_column.append(max(arr))
possible = set()
for i in range(r):
	current_row = matrix_row[i]
	min_row = min(current_row)
	for j in range(c):
		max_column = matrix_column[j]
		if current_row[j] == min_row and current_row[j] == max_column:
			possible.add(current_row[j])
if possible:
	print(min(possible))
else:
	print('GUESS')


import numpy as np
(r, c) = map(int, input().split())
l = []
for i in range(r):
	a = [int(x) for x in input().split()]
	l.append(a)
x = np.array(l)
m = np.asmatrix(x)
a = m.min(1)
b = m.max(0)
q = []
for item in a:
	for i in item:
		q.append(int(i))
z = []
b = b.T
for item in b:
	for i in item:
		z.append(int(i))
g = list(set(q).intersection(set(z)))
if len(g) == 0:
	print('GUESS')
else:
	print(g[0])


(r, c) = [int(i) for i in input().strip().split(' ')]
li = []
for i in range(r):
	li.append([int(j) for j in input().strip().split(' ')])
f = 0
for i in range(r):
	mn = min(li[i])
	pos = [j for (j, x) in enumerate(li[i]) if x == mn]
	for p in pos:
		tmp = []
		for j in range(r):
			tmp.append(li[j][p])
		mx = max(tmp)
		if mx == mn:
			f = 1
			print(mx)
			break
	if f == 1:
		break
if f == 0:
	print('GUESS')


(r, c) = map(int, input().split())
matrix = []
for i in range(r):
	row = list(map(int, input().split()))
	matrix.append(row)
maximumArr = []
minimumArr = []
for i in range(r):
	minimum = 1000000000000
	for j in range(c):
		minimum = min(minimum, matrix[i][j])
	minimumArr.append(minimum)
for i in range(c):
	maximum = -1
	for j in range(r):
		maximum = max(maximum, matrix[j][i])
	maximumArr.append(maximum)
done = False
for i in range(r):
	for j in range(c):
		if matrix[i][j] == minimumArr[i] and matrix[i][j] == maximumArr[j]:
			print(matrix[i][j])
			done = True
			break
	if done:
		break
if not done:
	print('GUESS')