#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1005;
const int inf = 1e9 + 5;
int n, m;
int a[MAXN][MAXN];
bool found[MAXN][MAXN];
vector<int> v;
bool check(int x) {
  memset(found, false, sizeof(found));
  for (int i = 0; i < n; i++) {
    v.clear();
    for (int j = 0; j < m; j++) {
      if (a[i][j] >= x) {
        v.push_back(j);
      }
    }
    for (int j1 = 0; j1 < v.size(); j1++) {
      for (int j2 = j1 + 1; j2 < v.size(); j2++) {
        if (found[v[j1]][v[j2]] == true) {
          return true;
        }
        found[v[j1]][v[j2]] = true;
      }
    }
  }
  return false;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int l, r, mid;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
    }
  }
  l = 0;
  r = inf;
  while (l + 1 < r) {
    mid = (l + r) / 2;
    if (check(mid) == true)
      l = mid;
    else
      r = mid - 1;
  }
  if (check(r) == true)
    cout << r << '\n';
  else if (check(l) == true)
    cout << l << '\n';
}
