#include <bits/stdc++.h>
using namespace std;
double PI = acos(-1);
double EPS = 1e-7;
int INF = 1000000000;
int MAXINT = 2147483647;
long long INFLL = 1000000000000000000LL;
long long MAXLL = 9223372036854775807LL;
pair<int, int> M[8] = {make_pair(0, 1),  make_pair(1, 0),  make_pair(-1, 0),
                       make_pair(0, -1), make_pair(-1, 1), make_pair(-1, -1),
                       make_pair(1, -1), make_pair(1, 1)};
int ada[1005][1005];
int lol[1005][1005];
int ar[1005][1005];
set<int> st;
vector<int> exist[1005];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int(a) = (1); (a) <= (n); (a)++) {
    for (int(b) = (1); (b) <= (m); (b)++) {
      scanf("%d", &ar[a][b]);
      st.insert(ar[a][b]);
    }
  }
  vector<int> v;
  for (__typeof((st).begin()) it = (st).begin(); it != (st).end(); (it)++) {
    v.push_back((*it));
  }
  int ans = min(min(ar[1][1], ar[1][m]), min(ar[n][1], ar[n][m]));
  int l = 0;
  int r = (int)v.size() - 1;
  while (l <= r) {
    memset(lol, 0, sizeof(lol));
    bool bisa = 0;
    int mid = (l + r) / 2;
    int cur = v[mid];
    for (int(a) = (1); (a) <= (n); (a)++) exist[a].clear();
    for (int(a) = (1); (a) <= (n); (a)++) {
      for (int(b) = (1); (b) <= (m); (b)++) {
        if (ar[a][b] >= cur) exist[a].push_back(b);
      }
    }
    for (int(a) = (1); (a) <= (n); (a)++) {
      for (int(b) = (0); (b) <= ((int)exist[a].size() - 1); (b)++) {
        for (int(c) = (b + 1); (c) <= ((int)exist[a].size() - 1); (c)++) {
          if (lol[exist[a][b]][exist[a][c]]) {
            bisa = 1;
            break;
          }
          lol[exist[a][b]][exist[a][c]] = 1;
          lol[exist[a][c]][exist[a][b]] = 1;
        }
        if (bisa) break;
      }
      if (bisa) break;
    }
    if (bisa) {
      (ans) = max((ans), (v[mid]));
      l = mid + 1;
    } else
      r = mid - 1;
  }
  cout << ans << endl;
}
