#include <bits/stdc++.h>
using namespace std;
const int maxn = (int)1e3 + 10;
const int inf = (int)1e9;
const int mod = (int)1e9 + 7;
const double eps = 1e-9;
int n, m, a[maxn][maxn];
int cnt[maxn], head[maxn], sz, c[maxn][maxn];
pair<int, int> res[maxn * maxn], s[maxn * maxn];
void Sort() {
  for (int i = 1; i <= m; ++i) cnt[i] = 0;
  for (int i = 0; i < sz; ++i) cnt[s[i].first]++;
  int curHead = 0;
  for (int i = 1; i <= m; ++i) {
    head[i] = curHead;
    curHead += cnt[i];
  }
  for (int i = 0; i < sz; ++i) res[head[s[i].first]++] = s[i];
  for (int i = 0; i < sz; ++i) s[i] = res[i];
  return;
}
bool check(int x) {
  sz = 0;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      if (a[i][j] >= x) s[sz++] = make_pair(j, i);
  Sort();
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j) c[i][j] = 0;
  for (int i = 0; i < sz;) {
    int j = i;
    while (j < sz && s[j].first == s[i].first) {
      for (int k = i; k < j; ++k) {
        c[s[k].second][s[j].second]++;
        c[s[j].second][s[k].second]++;
        if (c[s[k].second][s[j].second] == 2) return true;
      }
      ++j;
    }
    i = j;
  }
  return false;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) cin >> a[i][j];
  int l = 0, r = inf + 1, mid;
  while (r - l > 1) {
    mid = (l + r) / 2;
    if (check(mid))
      l = mid;
    else
      r = mid;
  }
  cout << l;
  return 0;
}
