#include <bits/stdc++.h>
using namespace std;
struct node {
  int val, x;
};
int n, m, k;
int a[1111][1111];
node points[1111111];
int f[1111][1111];
vector<int> used[1111];
bool cmp(node a, node b) {
  return (a.val > b.val || (a.val == b.val && a.x > b.x));
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      int x;
      cin >> x;
      a[i][j] = x;
      points[k].val = x, points[k].x = i * m + j;
      k++;
    }
  sort(points, points + k, cmp);
  for (int ii = 0; ii < k; ii++) {
    int value = points[ii].val;
    int i = points[ii].x / m, j = points[ii].x % m;
    for (int jj = 0; jj < used[i].size(); jj++) {
      if (a[i][used[i][jj]] >= value) {
        f[j][used[i][jj]]++, f[used[i][jj]][j]++;
        if (f[j][used[i][jj]] >= 2) {
          cout << value << "\n";
          return 0;
        }
      }
    }
    used[i].push_back(j);
  }
  return 0;
}
