#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1000000007;
const long long BIG = 1446803456761533460LL;
const int Big = 336860180;
const long long int INF = LONG_LONG_MAX;
const vector<vector<long long int> > adj4({{0, 1}, {0, -1}, {1, 0}, {-1, 0}});
const vector<vector<long long int> > adj8(
    {{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {-1, -1}, {1, -1}, {-1, 1}, {1, 1}});
long long int gcd(long long int a, long long int b) {
  return b == 0 ? a : gcd(b, a % b);
}
bool isprime(long long int n) {
  if (n == 2 || n == 3) return true;
  if (n < 2 || n % 2 == 0 || n % 3 == 0) return false;
  for (long long int i = 6; (i - 1) * (i - 1) <= n; i += 6) {
    if (n % (i - 1) == 0 || n % (i + 1) == 0) {
      return false;
    }
  }
  return true;
}
vector<bool> sprime;
void genPrime(long long int sz) {
  sprime = vector<bool>(sz, true);
  sprime[0] = false;
  sprime[1] = false;
  for (long long int i = (2); i < (sz); ++i) {
    if (sprime[i]) {
      for (long long int j = i * i; j < sz; j += i) {
        sprime[j] = false;
      }
    }
  }
}
long long int powMod(long long int a, long long int b, long long int mod) {
  long long int n = 1;
  long long int p = a;
  while (b > 0) {
    if (b % 2 == 1) {
      n *= p;
      n %= mod;
    }
    p *= p;
    p %= mod;
    b /= 2;
  }
  return n;
}
long long int modularInverse(long long int a, long long int mod) {
  return powMod(a, mod - 2, mod);
}
long long int binarysearch(long long int l, long long int r,
                           bool (*bsfunction)(long long int)) {
  while (r - l > 1) {
    long long int mid = (l + r) / 2;
    bool val = bsfunction(mid);
    if (val) {
      r = mid;
    } else {
      l = mid;
    }
  }
  return l;
}
stringstream sss;
const long long int maxn = 1000;
long long int n, m;
long long int ar[maxn][maxn];
long long int cnt[maxn][maxn];
bool func(long long int mx) {
  memset(cnt, 0, sizeof cnt);
  long long int second = 0;
  for (long long int i = 0; i < (n); ++i) {
    long long int s = 0;
    for (long long int j = 0; j < (m); ++j) {
      if (ar[i][j] >= mx) {
        ++s;
      }
    }
    second += s * (s - 1);
  }
  if (second > m * m) {
    return false;
  }
  for (long long int i = 0; i < (n); ++i) {
    for (long long int j = 0; j < (m); ++j) {
      if (ar[i][j] >= mx) {
        for (long long int k = (j + 1); k < (m); ++k) {
          if (ar[i][k] >= mx && (++cnt[j][k] >= 2)) {
            return false;
          }
        }
      }
    }
  }
  return true;
}
void MAIN() {
  cin >> n >> m;
  for (long long int i = 0; i < (n); ++i) {
    for (long long int j = 0; j < (m); ++j) {
      cin >> ar[i][j];
    }
  }
  cout << (binarysearch(0, 1000000001, func)) << endl;
}
const long long int TESTCASEN = 1;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(10);
  sss << R"(
2 2
1 2
3 4
    )";
  for (long long int i = 0; i < (TESTCASEN); ++i) {
    MAIN();
  }
  return 0;
}
