#include <bits/stdc++.h>
using namespace std;
int n, m;
int a[1000 + 10][1000 + 10];
bitset<1024> bs[1000 + 10], temp;
bool testAns(int ans) {
  for (int(i) = (0), _n = (n - 1); (i) <= _n; (i)++) {
    for (int(j) = (0), _n = (m - 1); (j) <= _n; (j)++) {
      if (a[i][j] >= ans)
        bs[i].set(j, true);
      else
        bs[i].set(j, false);
    }
  }
  for (int(i) = (0), _n = (n - 1); (i) <= _n; (i)++) {
    if (bs[i].count() < 2) continue;
    for (int(j) = (i + 1), _n = (n - 1); (j) <= _n; (j)++) {
      temp = bs[i] & bs[j];
      if (temp.count() >= 2) return true;
    }
  }
  return false;
}
int solve(int right) {
  int left = 0;
  int mid, ret = 0;
  while (left <= right) {
    mid = (left + right) / 2;
    if (testAns(mid)) {
      ret = mid;
      left = mid + 1;
    } else {
      right = mid - 1;
    }
  }
  return ret;
}
int main() {
  int maxs = 0;
  scanf("%d %d", &n, &m);
  for (int(i) = (0), _n = (n - 1); (i) <= _n; (i)++) {
    for (int(j) = (0), _n = (m - 1); (j) <= _n; (j)++) {
      scanf("%d", &a[i][j]);
      maxs = max(maxs, a[i][j]);
    }
  }
  printf("%d\n", solve(maxs));
  return 0;
}
