#include <bits/stdc++.h>
using namespace std;
int arr[1001][1001];
bool mark[1001][1001];
bool f(int x, int n, int m) {
  memset(mark, 0, sizeof(mark));
  bool flag = false;
  for (int i = 1; i <= m; i++) {
    vector<int> v;
    for (int j = 1; j <= n; j++) {
      if (arr[j][i] >= x) {
        v.push_back(j);
      }
    }
    for (int j = 0; j < (int)v.size(); j++) {
      for (int k = j + 1; k < (int)v.size(); k++) {
        if (mark[v[j]][v[k]]) {
          flag = true;
          break;
        }
        mark[v[j]][v[k]] = true;
        if (flag) {
          break;
        }
      }
      if (flag) {
        break;
      }
    }
    if (flag) {
      break;
    }
  }
  return flag;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  int maxi = 0;
  int mini = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> arr[i][j];
      maxi = max(maxi, arr[i][j]);
    }
  }
  int l = mini, r = maxi, ans = 0;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (f(mid, n, m)) {
      ans = mid;
      l = mid + 1;
    } else {
      r = mid - 1;
    }
  }
  cout << ans << endl;
}
