#include <bits/stdc++.h>
using namespace std;
int n, m, l, r, mid, a[1001][1001], ans = 0;
bool c[1001][1001];
vector<int> v;
bool valid(int x) {
  memset(c, 0, sizeof c);
  for (int i = 1; i <= n; i++) {
    v.clear();
    for (int j = 1; j <= m; j++) {
      if (a[i][j] >= x) {
        for (int u : v) {
          if (c[u][j]) {
            return true;
          } else {
            c[u][j] = true;
          }
        }
        v.push_back(j);
      }
    }
  }
  return false;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      scanf("%d", &a[i][j]);
    }
  }
  l = 0;
  r = 1000000000;
  while (l <= r) {
    mid = (l + r) / 2;
    if (valid(mid)) {
      ans = max(ans, mid);
      l = mid + 1;
    } else {
      r = mid - 1;
    }
  }
  printf("%d", ans);
  return 0;
}
