#include <bits/stdc++.h>
std::mt19937 rng(
    (int)std::chrono::steady_clock::now().time_since_epoch().count());
struct Event {
  int i, j, cost;
  bool operator<(Event o) const { return cost < o.cost; }
};
const int ms = 1010;
std::bitset<ms> to[ms];
Event a[ms * ms];
int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(NULL);
  int n, m;
  std::cin >> n >> m;
  int c = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      Event cur;
      cur.i = i;
      cur.j = j;
      std::cin >> cur.cost;
      a[c++] = cur;
    }
  }
  std::sort(a, a + c);
  auto check = [&](int x) {
    for (int i = 0; i < n; i++) {
      to[i].reset();
    }
    for (int i = x; i < c; i++) {
      to[a[i].i][a[i].j] = true;
    }
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        if ((to[i] & to[j]).count() >= 2) {
          return true;
        }
      }
    }
    return false;
  };
  int l = 0, r = c - 1;
  while (l != r) {
    int mid = (l + r + 1) / 2;
    if (check(mid)) {
      l = mid;
    } else {
      r = mid - 1;
    }
  }
  std::cout << a[l].cost << '\n';
}
