#include <bits/stdc++.h>
using namespace std;
struct timer {
  time_t start;
  timer() { start = clock(); }
  ~timer() {
    cerr << 1. * (clock() - start) / CLOCKS_PER_SEC << " secs" << endl;
  }
};
int n, m;
int on[1010][1010];
vector<int> row[1010], col[1010];
pair<int, pair<int, int> > cel[1000010];
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cin >> n >> m;
  for (int i = 0; i < (n); i++)
    for (int j = 0; j < (m); j++) {
      int a;
      cin >> a;
      cel[i * m + j] = make_pair(-a, pair<int, int>(i, j));
    }
  sort(cel, cel + n * m);
  for (int c = 0; c < (n * m); c++) {
    int y = cel[c].second.first, x = cel[c].second.second;
    on[y][x] = 1;
    for (int i = 0; i < (((int)(row[y]).size())); i++)
      for (int j = 0; j < (((int)(col[x]).size())); j++) {
        int y2 = col[x][j], x2 = row[y][i];
        if (on[y][x2] && on[y2][x] && on[y2][x2]) {
          cout << -cel[c].first;
          return 0;
        }
      }
    row[y].push_back(x);
    col[x].push_back(y);
  }
}
