#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 41;
const int mod = 1e9 + 7;
const int inf = 1e9 + 90;
const long long INF = 1e18;
const int curN = 1e3 + 3;
int a[curN][curN];
bitset<curN> b[curN];
bool was[curN];
int n, m;
bool ok(int x) {
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      b[i].set(j, a[i][j] >= x);
    }
    for (int j = 1; j < i; ++j) {
      if ((b[i] & b[j]).count() > 1) {
        return true;
      }
    }
  }
  return false;
}
vector<int> w;
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  ;
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      cin >> a[i][j];
      w.push_back(a[i][j]);
    }
  }
  sort(w.begin(), w.end());
  w.resize(unique(w.begin(), w.end()) - w.begin());
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      a[i][j] = lower_bound(w.begin(), w.end(), a[i][j]) - w.begin();
    }
  }
  int l{}, ans{}, r = n * m;
  while (l <= r) {
    int m = (l + r) / 2;
    if (ok(m)) {
      ans = m, l = m + 1;
    } else
      r = m - 1;
  }
  cout << w[ans];
}
