#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
int n, m, a[N][N];
bitset<N> f[N];
inline bool check(int x) {
  memset(f, 0, sizeof f);
  for (int i = 1; i <= m; ++i) {
    vector<int> g;
    for (int j = 1; j <= n; ++j) {
      if (a[j][i] >= x) {
        for (int x : g) {
          if (f[x][j]) return true;
          f[x][j] = 1;
        }
        g.push_back(j);
      }
    }
  }
  return false;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      cin >> a[i][j];
    }
  }
  int l = 0, r = 1e9, ans = 0;
  while (r >= l) {
    int m = l + r >> 1;
    if (check(m)) {
      ans = m;
      l = m + 1;
    } else {
      r = m - 1;
    }
  }
  cout << ans << '\n';
}
