cswork / OAOpen practice workspace

CiscoOA #25

Find Number in Matrix

Estimated EasyJudge-ready

Topics (AI-labelled)In-place Arrays

Find an element that is greater than or equal to every element in its row (ties allowed) and less than or equal to every element in its column; if one exists, output its value, otherwise −1. All valid positions necessarily have the same value.

Input

The input is N M, followed by N lines of M integers each. The original bounds 1≤N,M≤1000 and non-negative integers are kept; the original upper bound on elements is missing, and this site adds 0..10^9.

Output

Output a single integer, the answer.

Example 1

Input:

2 3
3 4 5
1 2 6

Output:

5

Example 2

Input:

2 2
1 1
0 1

Output:

1

Example 3

Input:

2 2
0 1
1 0

Output:

-1

Example notes

The outputs of the three public examples are, in order: 5; 1; -1. Verified by independent enumeration or direct simulation.

Editorial

Loading solution…