#include <bits/stdc++.h>
using namespace std;
int n, m, a[1005][1005];
bitset<1005> aktif[1005];
bool check(int val) {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      aktif[i][j] = a[i][j] >= val;
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      bitset<1005> cur = aktif[i] & aktif[j];
      if (cur.count() >= 2) return true;
    }
  }
  return false;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
    }
  }
  int lo = 0, hi = 1e9 + 7, mid;
  while (lo < hi) {
    mid = ((lo + hi) >> 1) + ((lo + hi) & 1);
    if (check(mid))
      lo = mid;
    else
      hi = mid - 1;
  }
  cout << lo << '\n';
  return 0;
}
