#include <bits/stdc++.h>
using namespace std;
int a[1005][1005];
vector<pair<int, int> > v;
int b[1005][1005], nb[1005];
int c[1005][1005];
int main(void) {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      scanf("%d", &a[i][j]);
      v.push_back(make_pair(a[i][j], (i << 10) + j));
    }
  sort(v.begin(), v.end());
  reverse(v.begin(), v.end());
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++) {
    int u = it->first, x = it->second;
    int y = x % 1024;
    x >>= 10;
    for (int i = 0; i < nb[x]; i++) {
      if (c[y][b[x][i]]) {
        printf("%d\n", u);
        return 0;
      }
      c[y][b[x][i]] = c[b[x][i]][y] = 1;
    }
    b[x][nb[x]++] = y;
  }
  return 0;
}
