#include <bits/stdc++.h>
const int N = 1010;
int q[N][N];
int vs[N][N];
int que[N];
int n, m;
bool fit(int mid) {
  for (int i = 1; i <= m; i++)
    for (int j = 1; j <= m; j++) vs[i][j] = 0;
  for (int i = 1; i <= n; i++) {
    int cnt = 0;
    for (int j = 1; j <= m; j++)
      if (q[i][j] >= mid) {
        for (int k = 1; k <= cnt; k++) {
          vs[que[k]][j]++;
          if (vs[que[k]][j] == 2) return 1;
        }
        que[++cnt] = j;
      }
  }
  return 0;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) scanf("%d", &q[i][j]);
  int l = 0, r = 1000000001;
  while (r - l > 1) {
    int mid = (l + r) / 2;
    if (fit(mid))
      l = mid;
    else
      r = mid;
  }
  if (fit(r))
    printf("%d\n", r);
  else
    printf("%d\n", l);
  return 0;
}
