#include <bits/stdc++.h>
using namespace std;
const int N = 1000 + 3;
int n, m, d[N][N], is[N][N];
vector<pair<int, pair<int, int> > > v;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  ;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      int x;
      cin >> x;
      v.push_back({x, {i, j}});
    }
  }
  sort(v.rbegin(), v.rend());
  for (auto u : v) {
    int x = u.second.first, y = u.second.second;
    for (int j = 0; j < m; j++) {
      if (is[x][j]) {
        if (d[y][j]) {
          cout << u.first;
          return 0;
        }
        d[y][j] = d[j][y] = 1;
      }
    }
    is[x][y] = 1;
  }
}
