#include <bits/stdc++.h>
using namespace std;
int a[1100][1100];
bool vis[1100][1100];
int n, m;
int s[1100];
bool can(int k) {
  memset(vis, 0, sizeof(vis));
  for (int i = 1; i <= n; i++) {
    int cnt = 0;
    for (int j = 1; j <= m; j++) {
      if (a[i][j] >= k) s[cnt++] = j;
    }
    for (int i = 0; i < cnt; i++)
      for (int j = i + 1; j < cnt; j++) {
        if (vis[s[i]][s[j]])
          return 1;
        else
          vis[s[i]][s[j]] = 1;
      }
  }
  return 0;
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) scanf("%d", &a[i][j]);
  int l = 0, r = 1000000000, ans;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (can(mid))
      ans = mid, l = mid + 1;
    else
      r = mid - 1;
  }
  printf("%d\n", ans);
  return 0;
}
