#include <bits/stdc++.h>
using namespace std;
int n, m, ret;
int a[1005][1005], b[1005];
char Pair[1005][1005];
int possible(int mn) {
  memset(Pair, 0, sizeof(Pair));
  for (int i = (0); i < (n); i++) {
    int t = 0;
    for (int j = (0); j < (m); j++)
      if (a[i][j] >= mn) b[t++] = j;
    for (int j = (0); j < (t); j++)
      for (int k = (j + 1); k < (t); k++) {
        if (Pair[b[j]][b[k]]) return 1;
        Pair[b[j]][b[k]] = 1;
      }
  }
  return 0;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = (0); i < (n); i++)
    for (int j = (0); j < (m); j++) scanf("%d", &a[i][j]);
  ret = 0;
  for (int i = 0, j = (1 << 30), k; j >= i;) {
    k = i + (j - i) / 2;
    if (possible(k))
      ret = k, i = k + 1;
    else
      j = k - 1;
  }
  printf("%d\n", ret);
  return 0;
}
