#include <bits/stdc++.h>
using namespace std;
const int N = 1000;
int n, m, a[N][N];
bool used[N][N];
bool check(int val) {
  memset(used, 0, sizeof(used));
  for (int i = 0; i < (n); ++i) {
    vector<int> have;
    for (int j = 0; j < (m); ++j) {
      if (a[i][j] >= val) {
        for (int x : have) {
          if (used[x][j])
            return true;
          else
            used[x][j] = true;
        }
        have.push_back(j);
      }
    }
  }
  return false;
}
signed main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < (n); ++i)
    for (int j = 0; j < (m); ++j) scanf("%d", &a[i][j]);
  int l = 0, r = 1e9;
  while (l < r) {
    int mid = (l + r + 1) / 2;
    if (check(mid))
      l = mid;
    else
      r = mid - 1;
  }
  cout << l << endl;
  return 0;
}
