#include <bits/stdc++.h>
using namespace std;
int arr[1005][1005];
bool grid[1005][1005];
bool vis[1005][1005];
vector<int> v[1005];
set<pair<int, int> > ss;
int main() {
  int n, i, j, m, c, mc, rc, k, pen;
  scanf("%d", &n);
  scanf("%d", &m);
  for (i = 0; i < n; i++)
    for (j = 0; j < m; j++) scanf("%d", &arr[i][j]);
  int low = 0, up = 1000000000, mid;
  while (low <= up) {
    ss.clear();
    mid = (low + up) >> 1;
    for (i = 0; i < n; i++) v[i].clear();
    memset(grid, 0, sizeof grid);
    memset(vis, 0, sizeof vis);
    for (i = 0; i < n; i++) {
      for (j = 0; j < m; j++) {
        if (arr[i][j] >= mid) {
          grid[i][j] = 1;
          v[i].push_back(j);
          c++;
        }
      }
    }
    bool f = 0;
    for (i = 0; i < n; i++) {
      for (j = 0; j < v[i].size(); j++) {
        for (k = j + 1; k < v[i].size(); k++) {
          if (vis[v[i][j]][v[i][k]]) {
            f = 1;
            break;
          }
          vis[v[i][j]][v[i][k]] = 1;
        }
        if (f) break;
      }
      if (f) break;
    }
    if (f)
      low = mid + 1;
    else
      up = mid - 1;
  }
  cout << up << endl;
  return 0;
}
