#include <bits/stdc++.h>
using namespace std;
const int maxn = 1010;
int n, m;
pair<int, pair<int, int> > A[maxn * maxn];
vector<int> has[maxn];
int Ans[maxn][maxn];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      int x;
      scanf("%d", &x);
      A[(i - 1) * m + j] = make_pair(x, make_pair(i, j));
    }
  sort(A + 1, A + n * m + 1);
  for (int i = n * m; i; i--) {
    int x = A[i].second.first, y = A[i].second.second;
    for (auto j : has[y]) {
      Ans[x][j]++;
      Ans[j][x]++;
      if (Ans[x][j] == 2) {
        printf("%d\n", A[i].first);
        exit(0);
      }
    }
    has[y].push_back(x);
    if (n * m - i >= max(n, m) * 2 * sqrt(max(n, m))) break;
  }
  return 0;
}
