#include <bits/stdc++.h>
using namespace std;
int a[1007][1007];
bitset<1007> b[1007];
bool invalid[1007];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = (1); i <= (n); ++i) {
    for (int j = (1); j <= (m); ++j) scanf("%d", &a[i][j]);
  }
  int left = 0;
  int right = 1000000007;
  while (left < right - 1) {
    int mid = (left + right) / 2;
    for (int i = (1); i <= (n); ++i) {
      for (int j = (1); j <= (m); ++j) b[i].set(j, (a[i][j] >= mid));
    }
    for (int i = (1); i <= (n); ++i) {
      invalid[i] = 0;
      if (b[i].count() <= 1) invalid[i] = 1;
    }
    bool ans = 0;
    for (int i = (1); i <= (n); ++i) {
      if (invalid[i]) continue;
      for (int j = (i + 1); j <= (n); ++j) {
        if (invalid[j]) continue;
        if ((b[i] & b[j]).count() >= 2) {
          ans = 1;
          break;
        }
      }
      if (ans) break;
    }
    if (ans)
      left = mid;
    else {
      right = mid;
      for (int i = (1); i <= (n); ++i) b[i].reset();
    }
  }
  printf("%d\n", left);
  return 0;
}
