#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
using namespace std;
bitset<1002> b[1002];
int n, m;
int a[1001][1001];
bool judge(int x) {
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (a[i][j] >= x)
        b[i][j] = 1;
      else
        b[i][j] = 0;
  for (int i = 1; i <= n; i++)
    if (b[i].count() >= 2)
      for (int j = i + 1; j <= n; j++)
        if (b[j].count() >= 2)
          if ((b[i] & b[j]).count() >= 2) return true;
  return false;
}
signed 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 = 1000000001;
  while (l + 1 < r) {
    int mid = (l + r) / 2;
    if (judge(mid))
      l = mid;
    else
      r = mid;
  }
  printf("%d", l);
  return 0;
}
