#include <bits/stdc++.h>
using namespace std;
const long double PI = 3.141592653589793;
const long long INF = 9223372036854775807ll;
const long long mod = 1e9 + 7;
void solve() {
  int n, m;
  cin >> n >> m;
  int ar[n][m];
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      cin >> ar[i][j];
    }
  }
  int hi = 1e9 + 1;
  int lo = 0;
  int done[n][n];
  while (hi - lo > 1) {
    int mid = (hi + lo) / 2;
    memset(done, 0, sizeof(done));
    int f = 0;
    for (int j = 0; j < m; ++j) {
      if (f) break;
      vector<int> curi;
      for (int i2 = 0; i2 < n; ++i2) {
        if (f) break;
        if (ar[i2][j] >= mid) {
          for (int i1 : curi) {
            if (done[i1][i2]) {
              f = 1;
              break;
            }
            done[i1][i2]++;
          }
          curi.push_back(i2);
        }
      }
    }
    if (f) {
      lo = mid;
    } else {
      hi = mid;
    }
  }
  cout << lo << '\n';
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  srand(chrono::high_resolution_clock::now().time_since_epoch().count());
  solve();
  return 0;
}
