#include <bits/stdc++.h>
using namespace std;
int a[1010][1010];
int mapa[1010][1010];
int n, m;
int TIME = 0;
bool ok(int val) {
  for (int i = 0; i < n; i++) {
    vector<int> row;
    for (int j = 0; j < m; j++) {
      if (a[i][j] >= val) row.push_back(j);
    }
    for (int first = 0; first < row.size(); first++)
      for (int second = first + 1; second < row.size(); second++) {
        if (mapa[row[first]][row[second]] == TIME) return true;
        mapa[row[first]][row[second]] = TIME;
      }
  }
  return false;
}
int solve(int lo, int hi) {
  if (lo == hi) return lo;
  int mid = (lo + hi + 1) / 2;
  TIME++;
  if (ok(mid)) return solve(mid, hi);
  return solve(lo, mid - 1);
}
int 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]);
  printf("%d", solve(0, 1000100100));
  return 0;
}
