#include <bits/stdc++.h>
using namespace std;
int m, n, a[1001][1001], F, L, mi, rr = 0, sv[1001][1001], sx[1001], n_s;
bool chk(int x) {
  rr++;
  for (int i = 0; i < n; i++) {
    n_s = 0;
    for (int j = 0; j < m; j++) {
      if (a[i][j] >= x) sx[n_s++] = j;
    }
    for (int j = 0; j < n_s; j++)
      for (int k = j + 1; k < n_s; k++)
        if (sv[sx[j]][sx[k]] == rr)
          return true;
        else
          sv[sx[j]][sx[k]] = rr;
  }
  return false;
}
int main() {
  scanf("%d%d", &n, &m);
  L = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) scanf("%d", &a[i][j]), L = max(L, a[i][j]);
  while (F <= L) {
    mi = (F + L) / 2;
    if (chk(mi))
      F = mi + 1;
    else
      L = mi - 1;
  }
  printf("%d\n", L);
  return 0;
}
