#include <bits/stdc++.h>
using namespace std;
int a[1010][1010];
int p[1010][1010];
int w[1010];
int n, m;
int ok(int d) {
  int cur, i, j, k;
  for (i = 0; i < m; i++)
    for (j = 0; j < m; j++) p[i][j] = 0;
  for (i = 0; i < n; i++) {
    cur = 0;
    for (j = 0; j < m; j++)
      if (a[i][j] >= d) w[cur++] = j;
    for (j = 0; j < cur; j++) {
      for (k = 0; k < j; k++) {
        if (1 == p[w[k]][w[j]]) return 1;
        p[w[k]][w[j]] = 1;
      }
    }
  }
  return 0;
}
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 l, r, res, mid;
  res = 0;
  l = 0;
  r = 1000000000;
  ok(11);
  while (l < r) {
    mid = (l + r + 1) / 2;
    if (1 == ok(mid)) {
      res = mid;
      l = mid;
    } else
      r = mid - 1;
  }
  printf("%d\n", res);
  return 0;
}
