#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
int n, m, a[N][N], l, r, mid;
unordered_set<int> e;
vector<int> b;
inline bool check(int x) {
  bitset<N> q[N];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (a[i][j] >= x) {
        q[i][j] = 1;
      }
    }
  }
  for (int i = 1; i < n; i++) {
    for (int j = i + 1; j <= n; j++) {
      if ((q[i] & q[j]).count() >= 2) {
        return true;
      }
    }
  }
  return false;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> a[i][j];
      e.insert(a[i][j]);
    }
  }
  for (auto it : e) {
    b.push_back(it);
  }
  sort(b.begin(), b.end());
  r = b.size() - 1;
  while (r - l > 1) {
    mid = (r + l) >> 1;
    if (check(b[mid])) {
      l = mid;
    } else {
      r = mid;
    }
  }
  if (check(b[r])) {
    cout << b[r];
  } else {
    cout << b[l];
  }
}
