#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1010;
int a[MAXN][MAXN];
bool b[MAXN][MAXN];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) cin >> a[i][j];
  int lo = 0, hi = 1e9 + 10;
  while (lo + 1 < hi) {
    int mid = (lo + hi) / 2;
    for (int i = 0; i < m; i++)
      for (int j = 0; j < m; j++) b[i][j] = false;
    bool found = false;
    for (int i = 0; i < n; i++) {
      vector<int> v;
      for (int j = 0; j < m; j++)
        if (a[i][j] >= mid) v.push_back(j);
      for (int j1 : v) {
        for (int j2 : v) {
          if (j1 == j2) continue;
          if (b[j1][j2]) {
            found = true;
            break;
          }
          b[j1][j2] = true;
        }
        if (found) break;
      }
      if (found) break;
    }
    if (found)
      lo = mid;
    else
      hi = mid;
  }
  cout << lo;
  return 0;
}
