#include <bits/stdc++.h>
using namespace std;
int const N = 1000 + 20;
int n, m, t[N][N];
bool mark[N][N];
pair<int, pair<int, int>> a[N * N];
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      scanf("%d", &a[i * m + j].first);
      a[i * m + j].second.first = i, a[i * m + j].second.second = j;
    }
  }
  sort(a, a + n * m, greater<pair<int, pair<int, int>>>());
  for (int i = 0; i < n * m; i++) {
    int num = a[i].first, x = a[i].second.first, y = a[i].second.second;
    for (int j = 0; j < m; j++) {
      if (mark[x][j]) t[y][j]++, t[j][y]++;
      if (t[y][j] == 2) return printf("%d ", num), 0;
    }
    mark[x][y] = 1;
  }
  return 0;
}
