#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 5;
int arr[1005][1005];
map<int, int> mp;
int rev[1000005];
int n, m;
bitset<1005> bs[1005];
bool ok(int mid) {
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (arr[i][j] >= mid)
        bs[i][j] = 1;
      else
        bs[i][j] = 0;
    }
  }
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      if ((bs[i] & bs[j]).count() >= 2) return 1;
    }
  }
  return 0;
}
vector<int> vec;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      cin >> arr[i][j];
      vec.push_back(arr[i][j]);
    }
  }
  sort(vec.begin(), vec.end());
  int l = 0, r = vec.size() - 1;
  while (l < r) {
    int mid = l + (r - l + 1) / 2;
    if (ok(vec[mid]))
      l = mid;
    else
      r = mid - 1;
  }
  cout << vec[l] << endl;
  return 0;
}
