#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1024;
int a[MAXN][MAXN];
bitset<MAXN * MAXN> bs;
int n, m;
bool vali(int _) {
  bs.reset();
  int now[MAXN], nn;
  for (int i = 0; i < n; i++) {
    nn = 0;
    for (int j = 0; j < m; j++) {
      if (a[i][j] >= _) now[nn++] = j;
    }
    for (int j = 0; j < nn; j++) {
      for (int k = j + 1; k < nn; k++) {
        int t = now[j] * MAXN + now[k];
        if (bs.test(t)) return true;
        bs.set(t);
      }
    }
  }
  return false;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
    }
  }
  int L = a[0][0], R = a[0][0];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      L = min(L, a[i][j]);
      R = max(R, a[i][j]);
    }
  }
  R++;
  while (L < R) {
    int m = (L + R) / 2;
    if (vali(m)) {
      L = m + 1;
    } else {
      R = m;
    }
  }
  cout << L - 1 << '\n';
}
