#include <bits/stdc++.h>
using namespace std;
const int MOD(1000000007);
const int INF((1 << 30) - 1);
const int MAXN(1005);
int n, m, a[MAXN][MAXN], b[MAXN][MAXN], c = 1, d[MAXN];
bool ok(int x) {
  for (int i = 0; i < n; i++) {
    int nd = 0;
    for (int j = 0; j < m; j++)
      if (a[i][j] >= x) d[nd++] = j;
    for (int j = 0; j <= nd - 2; j++)
      for (int k = j + 1; k <= nd - 1; k++) {
        if (b[d[j]][d[k]] == c)
          return true;
        else
          b[d[j]][d[k]] = c;
      }
  }
  return false;
}
int main() {
  scanf("%d%d", &n, &m);
  int low = INF, high = -1;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      scanf("%d", &a[i][j]);
      low = min(low, a[i][j]);
      high = max(high, a[i][j]);
    }
  int mid, ans;
  while (low <= high) {
    mid = (low + high) / 2;
    if (ok(mid))
      ans = mid, low = mid + 1;
    else
      high = mid - 1;
    c++;
  }
  printf("%d", ans);
}
