cswork / OAOpen practice workspace

CiscoOA #20

Find Last Cell

Estimated MediumJudge-ready

Topics (AI-labelled)SimulationIn-place Arrays

Starting from the top-left corner, traverse the matrix one cell at a time in a counterclockwise spiral going down, right, up, then left. You land on the starting cell, and after that you skip one path cell before each landing; the count continues through turns, and the parity is never reset. Output the value of the last cell you land on.

Input

The input is N M followed by N lines of M integers each. The original wording of N/M as rows and columns contains a typo; this site specifies N rows and M columns. The original had no numeric bounds; this site adds 1≤N,M≤1000 and elements in −10^9..10^9.

Output

Output a single integer, the answer.

Example 1

Input:

3 3
29 8 37
15 41 3
1 10 14

Output:

41

Example 2

Input:

2 2
1 2
3 4

Output:

4

Example 3

Input:

1 4
9 8 7 6

Output:

7

Example notes

The outputs of the three public examples are, in order: 41; 4; 7. Verified by independent enumeration or direct simulation.

Editorial

Loading solution…