#include <bits/stdc++.h>
using namespace std;
int arr[1000][1000];
int values[1000000];
bool good[1000][1000];
int n, m;
bool check(int k) {
  memset(good, 0, sizeof good);
  for (int i = 0; i < n; i++) {
    vector<int> idx;
    for (int j = 0; j < m; j++)
      if (arr[i][j] >= k) idx.push_back(j);
    for (int j = 0; j < idx.size(); j++)
      for (int z = j + 1; z < idx.size(); z++)
        if (good[idx[j]][idx[z]])
          return 1;
        else
          good[idx[j]][idx[z]] = 1;
  }
  return 0;
}
int main() {
  scanf("%d %d", &n, &m);
  int z = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      scanf("%d", &arr[i][j]);
      values[z++] = arr[i][j];
    }
  }
  sort(values, values + m * n);
  int s = 0, e = m * n - 1;
  while (true) {
    if (s == e) break;
    if (e == s + 1) {
      if (!check(values[s + 1])) break;
      s++;
      break;
    }
    int mid = (s + e) / 2;
    if (check(values[mid]))
      s = mid;
    else
      e = mid - 1;
  }
  printf("%d\n", values[s]);
  return 0;
}
