#include <bits/stdc++.h>
using namespace std;
int v[1005][1005];
int n, m;
int i, j, k, t;
int main() {
  scanf("%d%d", &n, &m);
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      scanf("%d", &v[i][j]);
    }
  }
  int ans = 0;
  int first, second;
  for (i = 0; i < n - 1; i++) {
    for (k = i + 1; k < n; k++) {
      first = 0;
      second = 0;
      for (j = 0; j < m; j++)
        if (v[i][j] > ans && v[k][j] > ans) {
          t = (v[i][j] < v[k][j] ? v[i][j] : v[k][j]);
          if (t > first) {
            second = first;
            first = t;
          } else if (t > second) {
            second = t;
          }
        }
      if (second > ans) ans = second;
    }
  }
  printf("%d", ans);
  return 0;
}
