#include<bits/stdc++.h>
using namespace std;
int v[1050][1050],n,m,dx[4]={1,-1,0,0},dy[4]={0,0,1,-1},ans=0x3f3f3f3f;
bool t[1050][1050];
bool check(int x,int y) {
if(x<1||x>n||y>m||y<1||t[x][y]) {
return false;
}
return true;
}
int bfs(int x,int y) {
for(int i=1;i<=n;i++) {
for(int j=1;j<=m;j++) {
t[i][j]=false;
}
}
queue<pair<int,int> >q;
int s,tmp=0x3f3f3f3f;
q.push({x,y});
while(!q.empty()) {
pair<int,int>head;
head=q.front();
q.pop();
for(int i=0;i<4;i++) {
pair<int,int>next;
int xx=head.first;
int yy=head.second;
if(check(xx,yy)) {
if(v[xx][yy]<tmp) {
tmp=v[xx][yy];
next={xx,yy};
}
q.push(next);
t[xx][yy]=true;
s=max(s,tmp);
if(yy==n) {
while(!q.empty()) {
q.pop();
}
}
}
tmp=0x3f3f3f3f;
}
}
return s;
}
int main() {
cin>>n>>m;
for(int i=1;i<=n;i++) {
for(int j=1;j<=m;j++) {
cin>>v[i][j];
}
}
for(int i=1;i<=m;i++) {
ans=min(bfs(2,i),ans);
}
return 0;
}