#include <bits/stdc++.h>
using namespace std;
const int N = 1111;
int n, m, now;
int sta[N][N];
int a[N][N];
int q[N];
bool Can(int num) {
  int top;
  for (int j = 0; j != m; j++) {
    top = 0;
    for (int i = 0; i != n; i++) {
      if (a[i][j] >= num) q[top++] = i;
    }
    for (int k1 = 0; k1 != top; k1++)
      for (int k2 = k1 + 1; k2 != top; k2++)
        if (sta[q[k1]][q[k2]] == now)
          return true;
        else
          sta[q[k1]][q[k2]] = now;
  }
  return false;
}
int main() {
  int mi = 1111111111, ma = -1;
  scanf("%d%d", &n, &m);
  for (int i = 0; i != n; i++) {
    for (int j = 0; j != m; j++) {
      scanf("%d", &a[i][j]);
      mi = min(mi, a[i][j]);
      ma = max(ma, a[i][j]);
    }
  }
  for (int i = 0; i != n; i++)
    for (int j = 0; j != n; j++) sta[i][j] = 0;
  int l = mi, r = ma + 1, mid;
  now = 1;
  while (l + 1 < r) {
    mid = l + (r - l) / 2;
    if (Can(mid))
      l = mid;
    else
      r = mid;
    now++;
  }
  printf("%d\n", l);
  return 0;
}
