#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:200000000")
using namespace std;
template <typename T>
inline T Abs(T x) {
  return (x >= 0) ? x : -x;
}
template <typename T>
inline T sqr(T x) {
  return x * x;
}
template <typename T>
inline string toStr(T x) {
  stringstream st;
  st << x;
  string s;
  st >> s;
  return s;
}
template <typename T>
inline int bit(T mask, int b) {
  return (b >= 0 && (mask & (T(1) << b)) != 0) ? 1 : 0;
}
inline int nextInt() {
  int x;
  if (scanf("%d", &x) != 1) throw;
  return x;
}
inline long long nextInt64() {
  long long x;
  if (scanf("%lld", &x) != 1) throw;
  return x;
}
inline double nextDouble() {
  double x;
  if (scanf("%lf", &x) != 1) throw;
  return x;
}
const int INF = (int)1E9;
const long long INF64 = (long long)1E18;
const long double EPS = 1E-9;
const long double PI = 3.1415926535897932384626433832795;
const int MAXN = 1100;
const int X = 62;
long long d[MAXN][MAXN / X];
int n, m, a[MAXN][MAXN];
bool check(long long* d1, long long* d2) {
  int c = 0;
  for (int i = 0; i < (int)(m / X + 1); i++) {
    long long value = d1[i] & d2[i];
    if ((value & (value - 1)) != 0) return true;
    if (value != 0) c++;
  }
  return c >= 2;
}
bool solve(int x) {
  memset(d, 0, sizeof d);
  for (int i = 0; i < (int)(n); i++)
    for (int j = 0; j < (int)(m); j++)
      if (a[i][j] >= x) d[i][j / X] += 1LL << (j % X);
  for (int i = 0; i < (int)(n); i++)
    for (int j = 0; j < (int)(i); j++)
      if (check(d[i], d[j])) return true;
  return false;
}
int main() {
  n = nextInt();
  m = nextInt();
  for (int i = 0; i < (int)(n); i++)
    for (int j = 0; j < (int)(m); j++) a[i][j] = nextInt();
  vector<int> p;
  for (int i = 0; i < (int)(n); i++)
    for (int j = 0; j < (int)(m); j++) p.push_back(a[i][j]);
  sort((p).begin(), (p).end());
  p.erase(unique((p).begin(), (p).end()), p.end());
  int l = 0, r = int(p.size()) - 1;
  while (r - l > 1) {
    int mid = (l + r) >> 1;
    if (solve(p[mid]))
      l = mid;
    else
      r = mid;
  }
  int ans = 0;
  for (int i = l; i <= r; i++)
    if (solve(p[i])) ans = p[i];
  cout << ans << endl;
  return 0;
}
