#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T abs(T n) {
  return (n < 0 ? -n : n);
}
template <typename T>
inline T sqr(T n) {
  return (n * n);
}
int a[1010][1010], n, m;
bool big[1010][1010];
vector<int> pos;
bool check(int val) {
  memset(big, 0, sizeof big);
  for (int i = 0; i < n; i++) {
    pos.clear();
    for (int j = 0; j < m; j++)
      if (a[i][j] >= val) pos.push_back(j);
    for (vector<int>::iterator it = pos.begin(); it != pos.end(); it++) {
      for (vector<int>::iterator jt = pos.begin(); jt != pos.end(); jt++) {
        if (it == jt) continue;
        if (big[*it][*jt])
          return true;
        else
          big[*it][*jt] = true;
      }
    }
  }
  return false;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) scanf("%d", &a[i][j]);
  int l = -1, r = (int)1e9 + 1;
  while (r - l > 1) {
    int mid = (r + l) >> 1;
    if (check(mid))
      l = mid;
    else
      r = mid;
  }
  cout << l;
  return 0;
}
