#include <bits/stdc++.h>
using namespace std;
int const maxn = 1e3 + 5;
int n, m, a[maxn][maxn], sz, b[maxn];
bool used[maxn][maxn];
bool solve(int x) {
  memset(used, false, sizeof(used));
  for (int i = 1; i <= (int)n; i++) {
    sz = 0;
    for (int j = 1; j <= (int)m; j++)
      if (a[i][j] >= x) {
        for (int k = 1; k <= (int)sz; k++) {
          if (used[j][b[k]]) return true;
          used[j][b[k]] = true;
        }
        b[++sz] = j;
      }
  }
  return false;
}
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= (int)n; i++)
    for (int j = 1; j <= (int)m; j++) cin >> a[i][j];
  int ini = 0, fim = 1000000000;
  for (int i = 1; i <= (int)100; i++) {
    int mid = (ini + fim + 1) / 2;
    if (solve(mid))
      ini = mid;
    else
      fim = mid;
  }
  cout << ini;
}
