#include <bits/stdc++.h>
using namespace std;
const int inf = 1000000000;
const int MOD = 1000000007;
const double pi = acos(-1.0);
bool have[1001][1001];
int a[1001][1001];
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = (1); i <= (n); ++i)
    for (int j = (1); j <= (m); ++j) scanf("%d", &a[i][j]);
  int low = 0, high = inf;
  while (low < high) {
    memset(have, 0, sizeof(have));
    int mid = (low + high) / 2 + 1;
    bool ok = false;
    for (int i = (1); i <= (n); ++i) {
      vector<int> f;
      for (int j = (1); j <= (m); ++j)
        if (a[i][j] >= mid) f.push_back(j);
      for (int i1 = (0); i1 <= ((int)((f).size()) - 1); ++i1)
        for (int i2 = (i1 + 1); i2 <= ((int)((f).size()) - 1); ++i2) {
          if (have[f[i1]][f[i2]]) ok = true;
          have[f[i1]][f[i2]] = true;
        }
      if (ok) break;
    }
    if (ok)
      low = mid;
    else
      high = mid - 1;
  }
  cout << high << endl;
}
