#include <bits/stdc++.h>
using namespace std;
const long long INF = (long long)1e18;
ifstream Cin("input.txt");
ofstream Cout("output.txt");
long long n, m, r[1001][1001];
bool check(int M) {
  char b[1001][1001] = {};
  int i, j, k;
  for (i = 1; i <= n; ++i) {
    vector<int> x;
    for (j = 1; j <= m; ++j)
      if (r[i][j] >= M) x.push_back(j);
    for (j = 0; j < (int)x.size(); ++j)
      for (k = j + 1; k < (int)x.size(); k++)
        if (b[x[j]][x[k]])
          return true;
        else
          b[x[j]][x[k]] = true;
  }
  return false;
}
int main() {
  int i, j, k, ans, Min = 0, Max = (int)1e9, M;
  cin >> n >> m;
  for (i = 1; i <= n; ++i)
    for (j = 1; j <= m; ++j) scanf("%d", &r[i][j]);
  while (Min < Max) {
    M = (Min + Max) / 2;
    if (M == Min) M = Max;
    if (check(M))
      Min = M;
    else
      Max = M - 1;
  }
  cout << Min;
}
