#include <bits/stdc++.h>
using namespace std;
int n, m, a[1001][1001];
long long mask[1005][25];
bool ok(int x) {
  memset(mask, 0, sizeof(mask));
  for (int i = 0, _n = (int)n; i < _n; i++)
    for (int j = 0, _n = (int)m; j < _n; j++)
      if (a[i][j] >= x) mask[i][j / 62] |= 1LL << (j % 62);
  int l = (m + 61) / 62;
  for (int j = 0, _n = (int)n; j < _n; j++)
    for (int i = 0, _n = (int)j; i < _n; i++) {
      long long res = 0;
      for (int k = 0, _n = (int)l + 1; k < _n; k++) {
        long long ans = mask[i][k] & mask[j][k];
        if (ans == 0) continue;
        if ((ans & (ans - 1)) == 0)
          res++;
        else
          return true;
      }
      if (res >= 2) return true;
    }
  return false;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  for (int i = 0, _n = (int)n; i < _n; i++)
    for (int j = 0, _n = (int)m; j < _n; j++) cin >> a[i][j];
  int lo = 0, hi = 1000000000;
  while (lo < hi) {
    int mid = (lo + hi + 1) / 2;
    if (ok(mid))
      lo = mid;
    else
      hi = mid - 1;
  }
  cout << lo << endl;
}
