#include <bits/stdc++.h>
using namespace std;
int n, m;
int a[1010][1010];
int vis[1010][1010];
int sett[1010];
bool check(int S) {
  memset(vis, 0, sizeof(vis));
  memset(sett, 0, sizeof(sett));
  for (int i = 1; i <= n; i++) {
    int id = 1;
    for (int j = 1; j <= m; j++) {
      if (a[i][j] >= S) {
        sett[id] = j;
        id++;
      }
    }
    for (int j = 1; j < id; j++) {
      for (int k = j + 1; k < id; k++) {
        if (vis[sett[j]][sett[k]] == 1 && sett[j] != sett[k]) {
          return 1;
        }
        vis[sett[j]][sett[k]] = 1;
      }
    }
  }
  return 0;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  int maxx = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> a[i][j];
      maxx = max(maxx, a[i][j]);
    }
  }
  int l = 0;
  int r = maxx;
  int mid;
  int Ans = 0;
  while (l <= r) {
    mid = (l + r) / 2;
    if (check(mid) == 1) {
      Ans = max(Ans, mid);
      l = mid + 1;
    } else {
      r = mid - 1;
    }
  }
  cout << Ans;
  return 0;
}
