#include <bits/stdc++.h>
using namespace std;
int n, m;
const int N = 1002;
int a[N][N], b[N][N];
bool check(int c) {
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      b[i][j] = 0;
    }
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (a[i][j] < c) {
        continue;
      }
      for (int t = i + 1; t < n; ++t) {
        if (a[t][j] >= c) {
          if (b[i][t]) {
            return true;
          } else {
            b[i][t] = 1;
          }
        }
      }
    }
  }
  return false;
}
void solve() {
  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 = 1000 * 1000 * 1000 + 7, c;
  while (l < r) {
    c = l + (r - l + 1) / 2;
    if (check(c)) {
      l = c;
    } else {
      r = c - 1;
    }
  }
  cout << l;
}
int main() {
  solve();
  return 0;
}
