#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7, mod = 1e9 + 7, inf = 1e9 + 7;
const long long linf = (long long)1e18 + 7;
const long double eps = 1e-15, pi = 3.141592;
const int dx[] = {-1, 0, 1, 0, 1, -1, -1, 1},
          dy[] = {0, 1, 0, -1, 1, -1, 1, -1};
int n, m;
int pos[N], a[1001][1001];
bool was[1001][1001];
inline bool Check(int x) {
  memset(was, 0, sizeof(was));
  for (int i = 1; i <= n; i++) {
    int sz = 0;
    for (int j = 1; j <= m; j++)
      if (a[i][j] >= x) pos[++sz] = j;
    for (int l = 1; l <= sz; l++)
      for (int r = l + 1; r <= sz; r++) {
        if (was[pos[l]][pos[r]]) return 1;
        was[pos[l]][pos[r]] = 1;
      }
  }
  return 0;
}
int main() {
  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 l = 0, r = inf, mid, ans;
  while (l <= r) {
    mid = l + r >> 1;
    if (Check(mid))
      ans = mid, l = mid + 1;
    else
      r = mid - 1;
  }
  cout << ans;
  exit(0);
}
