#include <bits/stdc++.h>
using namespace std;
const int maxn = 2000;
bool good[maxn][maxn];
int arr[maxn][maxn];
bool ok(int val, int n, int m) {
  memset(good, 0, sizeof(good));
  for (int i = 1; i <= n; i++) {
    vector<int> x;
    for (int j = 1; j <= m; j++) {
      if (arr[i][j] >= val) x.push_back(j);
    }
    for (int a = 0; a < x.size(); a++) {
      for (int b = a + 1; b < x.size(); b++) {
        int u = x[a];
        int v = x[b];
        if (good[u][v]) return 1;
        good[u][v] = 1;
      }
    }
  }
  return false;
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) scanf("%d", &arr[i][j]);
  int lo = 0, hi = 1E9;
  while (lo < hi) {
    int mid = (lo + hi + 1) >> 1;
    if (ok(mid, n, m))
      lo = mid;
    else
      hi = mid - 1;
  }
  printf("%d\n", lo);
  return 0;
}
