#include <iostream>
#include <queue>
#include <algorithm>
#include <cstring>
using namespace std;
#define MAXN 100
int map[MAXN][MAXN];
int dist[MAXN][MAXN][2];
struct node {
int x;
int y;
int flag;
int level;
bool operator < (const node& a) const {
return a.level < level;
}
};
priority_queue<node> pq;
int N, M;
int dx[] = { 0,0,1,-1 };
int dy[] = { 1,-1,0,0 };
bool is_range(int x, int y) {
if (x < 0 || y < 0 || x >= N || y >= M) return false;
return true;
}
int bfs() {
pq.push({ 0,0,0 ,map[0][0]});
dist[0][0][0] = map[0][0];
while (!pq.empty()) {
int x = pq.top().x;
int y = pq.top().y;
int flag = pq.top().flag;
int level = pq.top().level;
if (x == N - 1 && y == M - 1) return dist[x][y][flag];
pq.pop();
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (!is_range(nx, ny)) continue;
if (dist[nx][ny][flag] == -1) {
dist[nx][ny][flag] = max(dist[x][y][flag], map[nx][ny]);
pq.push({ nx,ny,flag,map[nx][ny] });
}
if (!flag) {
nx += dx[i];
ny += dy[i];
if (!is_range(nx, ny)) continue;
if (dist[nx][ny][flag + 1] != -1) continue;
dist[nx][ny][flag + 1] = max(dist[x][y][flag], map[nx][ny]);
pq.push({ nx,ny,flag + 1,map[nx][ny] });
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
memset(dist, -1, sizeof(dist));
cin >> N >> M;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
cin >> map[i][j];
}
}
cout << bfs() <<"\n";
return 0;
}