#include <bits/stdc++.h>
using namespace std;
int a[1153][1153];
int n, m;
unsigned long long int b[1153][1153];
bool pos(int minval) {
  memset(b, 0, sizeof(b));
  int i, j, k;
  for (i = 0; i < n; i++) {
    int ccnt = 0;
    for (j = 0; j < 1024; j += 63) {
      unsigned long long int blk = 0;
      for (k = j; k < j + 63; k++) {
        blk <<= 1;
        if (a[i][k] >= minval) {
          blk |= 1;
        }
      }
      b[i][j] = blk;
    }
    for (j = 0; j < i; j++) {
      ccnt = 0;
      for (k = 0; k < 1024; k += 63) {
        unsigned long long int ret = (b[i][k] & b[j][k]);
        if (ret != 0) {
          if ((ret & (ret - 1)) == 0)
            ccnt++;
          else
            return true;
        }
      }
      if (ccnt >= 2) return true;
    }
  }
  return false;
}
int main() {
  int i, j;
  scanf("%d %d", &n, &m);
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      scanf("%d", &a[i][j]);
    }
  }
  int lb = 0, ub = 1000000004, mid, ans;
  while (lb <= ub) {
    mid = (lb + ub) / 2;
    if (pos(mid)) {
      lb = mid + 1;
      ans = mid;
    } else
      ub = mid - 1;
  }
  cout << ans << endl;
  return 0;
}
