#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000 + 10;
int data[maxn][maxn];
int n, m;
long long st[maxn][20];
int cal(long long x) {
  int res = 0;
  for (long long i = x; i; i -= (i & -i)) {
    ++res;
  }
  return res;
}
bool check(int now) {
  int top = 0;
  for (int row = 0; row < n; ++row) {
    top = 0;
    for (int i = 0; i < m; i += 63) {
      st[row][top] = 0;
      for (int j = i; j < min(i + 63, m); ++j) {
        if (data[row][j] >= now) st[row][top] |= (((long long)1) << (j - i));
      }
      ++top;
    }
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < i; ++j) {
      int cnt = 0;
      for (int k = 0; k < top; ++k) {
        cnt += cal((st[i][k] & st[j][k]));
        if (cnt >= 2) return true;
      }
    }
  }
  return false;
}
int main() {
  while (cin >> n >> m) {
    int l = 0, r = 0;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < m; ++j) {
        scanf("%d", &data[i][j]);
        r = max(r, data[i][j]);
      }
    }
    while (l < r) {
      int mid = (l + r + 1) >> 1;
      if (check(mid)) {
        l = mid;
      } else
        r = mid - 1;
    }
    cout << l << endl;
  }
  return 0;
}
