#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int M = 1e3 + 5, INF = 0x3f3f3f3f, mod = 1e9 + 7;
int A[M][M];
bool mark[M][M];
int n, m;
bool check(int mid) {
  memset(mark, 0, sizeof(mark));
  for (int j = 1; j <= n; j++) {
    vector<int> B;
    for (int k = 1; k <= m; k++) {
      if (A[j][k] >= mid) B.push_back(k);
    }
    for (int k = 0; k < B.size(); k++) {
      for (int i = k + 1; i < B.size(); i++) {
        if (mark[B[k]][B[i]]) return 1;
        mark[B[k]][B[i]] = 1;
      }
    }
  }
  return 0;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int j = 1; j <= n; j++) {
    for (int k = 1; k <= m; k++) {
      scanf("%d", &A[j][k]);
    }
  }
  int l = 0, r = INF;
  int res = 0;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (check(mid)) {
      l = mid + 1;
      res = mid;
    } else
      r = mid - 1;
  }
  printf("%d\n", res);
  return 0;
}
