#include <bits/stdc++.h>
using namespace std;
int n, m, v, cmt[1010][1010] = {0};
vector<vector<int> > g(1010);
set<pair<int, pair<int, int> > > s;
int main() {
  int rv = -1;
  scanf("%d%d", &n, &m);
  for (int i = (0); i < (n); i++)
    for (int j = (0); j < (m); j++) {
      scanf("%d", &v);
      s.insert({v, {i, j}});
    }
  for (set<pair<int, pair<int, int> > >::reverse_iterator it = s.rbegin();
       it != s.rend(); it++) {
    pair<int, pair<int, int> > p = *it;
    v = p.first;
    int x = p.second.first, y = p.second.second;
    for (int i = (0); i < (g[x].size()); i++) {
      cmt[y][g[x][i]]++;
      if ((++cmt[g[x][i]][y]) >= 2) {
        rv = v;
        break;
      }
    }
    if (rv != -1) {
      printf("%d\n", rv);
      break;
    }
    g[x].push_back(y);
  }
  return 0;
}
