#include <bits/stdc++.h>
using namespace std;
const int kMaxN = 1010;
struct Cell {
  int x, y, val;
  bool operator<(const Cell &p) const { return val > p.val; }
} cell[kMaxN * kMaxN];
vector<int> vec[kMaxN];
int g[kMaxN][kMaxN];
int n, m;
void init() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      scanf("%d", &cell[i * m + j].val);
      cell[i * m + j].x = i;
      cell[i * m + j].y = j;
    }
  }
  sort(cell, cell + n * m);
}
void solve() {
  for (int i = 0; i < n * m; ++i) {
    int j = cell[i].y;
    int q = cell[i].x;
    for (int l = 0; l < ((int)(vec[j]).size()); ++l) {
      int p = vec[j][l];
      if (g[p][q]) {
        printf("%d\n", cell[i].val);
        return;
      }
      g[p][q] = g[q][p] = 1;
    }
    vec[j].push_back(q);
  }
}
int main() {
  init();
  solve();
  return 0;
}
