cswork / OAOpen practice workspace

CiscoOA #18

Find Elements Largest in Row Smallest in Column

Estimated EasyJudge-ready

Topics (AI-labelled)In-place Arrays

The matrix elements are non-negative. Find an element that equals both the maximum of its row and the minimum of its column, and output its value; if none exists, output −1. Multiple equal maximum or minimum positions are all valid; it does not need to be strictly greater or smaller.

Input

The first line contains N M, followed by N lines of M integers each. Original bounds: 1≤N,M≤1000; the elements are non-negative but have no upper bound, so this site adds ≤10^9. The next sentence of the original input swapped the dimensions as M rows and N columns; this site restores N rows and M columns based on the explicit dimension definition.

Output

Output a single integer, the answer.

Example 1

Input:

2 2
1 2
3 4

Output:

2

Example 2

Input:

2 2
1 1
0 1

Output:

1

Example 3

Input:

2 2
2 1
1 2

Output:

-1

Example notes

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

Editorial

Loading solution…