#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
template <class T>
void checkMax(T &a, T b) {
  a = max(a, b);
}
template <class T>
void checkMin(T &a, T b) {
  a = min(a, b);
}
const int N = 1005;
int n, m, org[N][N];
unsigned int row[N][32];
int bitCount(unsigned int x) {
  int ret = 0;
  while (x) {
    ret++;
    x &= (x - 1);
  }
  return ret;
}
int check(int v) {
  int i, j, k;
  memset(row, 0, sizeof(row));
  for (i = 0; i < n; i++)
    for (j = 0; j < m; j++)
      if (org[i][j] >= v) row[i][j / 32] |= 1u << (j % 32);
  for (i = 0; i < n; i++)
    for (j = i + 1; j < n; j++) {
      int cnt = 0;
      for (k = 0; k < 32; k++) cnt += bitCount(row[i][k] & row[j][k]);
      if (cnt >= 2) return 1;
    }
  return 0;
}
int main() {
  int i, j;
  while (scanf("%d%d", &n, &m) != EOF) {
    for (i = 0; i < n; i++)
      for (j = 0; j < m; j++) scanf("%d", &org[i][j]);
    int l = 0, r = 0x3f3f3f3f;
    while (l + 1 < r) {
      int mid = (l + r) / 2;
      if (check(mid))
        l = mid;
      else
        r = mid;
    }
    printf("%d\n", l);
  }
  return 0;
}
