#include <bits/stdc++.h>
using namespace std;
int n, m;
int a[1111][1111];
bool b[1111][1111];
vector<int> v;
vector<int> z;
bool done[1111][1111];
bool ok(int k) {
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) b[i][j] = a[i][j] >= k;
  memset(done, 0, sizeof done);
  for (int j = 0; j < m; j++) {
    z.clear();
    for (int i = 0; i < n; i++)
      if (b[i][j]) z.push_back(i);
    for (int i1 = 0; i1 < z.size(); i1++)
      for (int i2 = i1 + 1; i2 < z.size(); i2++) {
        int r1 = z[i1];
        int r2 = z[i2];
        if (done[r1][r2]) return 1;
        done[r1][r2] = 1;
      }
  }
  return 0;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) cin >> a[i][j];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) v.push_back(a[i][j]);
  sort(v.begin(), v.end());
  int lo = 0, hi = n * m - 1;
  while (lo < hi) {
    int m = hi - (hi - lo) / 2;
    if (ok(v[m]))
      lo = m;
    else
      hi = m - 1;
  }
  cout << v[lo] << endl;
  return 0;
}
