#include <bits/stdc++.h>
using namespace std;
const int N = 1111;
int a[N][N], c, q[N], used[N][N], it, n, m;
bool check(int val) {
  ++it;
  for (int i = 0; i < n; ++i) {
    c = 0;
    for (int j = 0; j < m; ++j)
      if (a[i][j] >= val) q[c++] = j;
    for (int j = 0; j < c; ++j)
      for (int k = 0; k < j; ++k) {
        if (used[q[k]][q[j]] == it) return 1;
        used[q[k]][q[j]] = it;
      }
  }
  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]);
  int l = 1, r = 1e9 + 1;
  while (l < r) {
    int m = (l + r) >> 1;
    if (check(m))
      l = m + 1;
    else
      r = m;
  }
  printf("%d\n", l - 1);
  return 0;
}
