#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
const int inf = 1e9;
int n, m, a[N][N], l, r, c[N * N], t;
bool exist(int k) {
  vector<int> st;
  for (int i = 1; i <= n; i++) {
    st.clear();
    for (int j = 1; j <= m; j++)
      if (a[i][j] >= k) {
        for (auto it : st) {
          int hash = it * m + j;
          if (c[hash] == t) return 1;
          c[hash] = t;
        }
        st.push_back(j);
      }
  }
  return 0;
}
int main() {
  ios_base ::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) cin >> a[i][j];
  r = inf;
  while (r - l > 1) {
    ++t;
    int m = (l + r) / 2;
    if (exist(m))
      l = m;
    else
      r = m;
  }
  ++t;
  if (exist(r))
    cout << r;
  else
    cout << l;
  return 0;
}
