#include <bits/stdc++.h>
using namespace std;
int fastMax(int x, int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ y; }
int fastMin(int x, int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ x; }
const int N = 50010;
const int M = 1e6 + 5;
int res[1010][1010];
bool vis[1010][1010];
int f[1010];
int n, m;
bool ok(int x) {
  memset(vis, 0, sizeof vis);
  for (int i = 1; i <= n; ++i) {
    int tot = 0;
    for (int j = 1; j <= m; ++j) {
      if (res[i][j] > x) f[++tot] = j;
    }
    for (int j = 1; j <= tot - 1; ++j) {
      for (int k = j + 1; k <= tot; ++k) {
        if (vis[f[j]][f[k]]) return 1;
        vis[f[j]][f[k]] = 1;
      }
    }
  }
  return 0;
}
int main() {
  scanf("%d%d", &n, &m);
  int MAX = -0x3f3f3f3f;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      scanf("%d", &res[i][j]);
      MAX = max(MAX, res[i][j]);
    }
  }
  int l = 0, r = MAX;
  while (l < r) {
    int mid = l + r >> 1;
    if (ok(mid))
      l = mid + 1;
    else
      r = mid;
  }
  cout << l;
}
