#include <bits/stdc++.h>
using namespace std;

using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using LL = long long;
using VL = vector<LL>;
using VVL = vector<VL>;
using PLL = pair<LL, LL>;
using VS = vector<string>;

#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

#define FF first
#define SS second
template<class S, class T>
istream& operator>>(istream& is, pair<S,T>& p){
  return is >> p.FF >> p.SS;
}
template<class S, class T>
ostream& operator<<(ostream& os, const pair<S,T>& p){
  return os << p.FF << " " << p.SS;
}
template<class T>
void maxi(T& x, T y){
  if(x < y) x = y;
}
template<class T>
void mini(T& x, T y){
  if(x > y) x = y;
}


const double EPS = 1e-10;
const double PI  = acos(-1.0);
const LL MOD = 1e9+7;
const int INF = 1e9+10;

int main(){
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int H, W; cin >> H >> W;

  int mn = INF;
  int mx, my;
  VVI xs(H, VI(W));
  VVI mnyt(H, VI(W));
  VVI mxyt(H, VI(W));
  VVI mxyb(H, VI(W));
  VVI mnyb(H, VI(W));
  REP(y,H){
	REP(x,W){
	  cin >> xs[y][x];
	  mxyt[y][x] = max((y-1>=0?mxyt[y-1][x]:-INF), xs[y][x]);
	  mnyt[y][x] = min((y-1>=0?mnyt[y-1][x]:INF), xs[y][x]);
	  if(mn > xs[y][x]){
		mn = xs[y][x];
		mx = x;
		my = y;
	  }
	}
  }
  REP(x,W) for(int y=H-1;y>=0;--y){
	mxyb[y][x] = max(y+1<H?mxyb[y+1][x]:-INF, xs[y][x]);
	mnyb[y][x] = min(y+1<H?mnyb[y+1][x]:INF, xs[y][x]);
  }

  int lb = -1, ub = INF;
  VI rows(W);
  while(ub-lb > 1){
	int m =(lb + ub) / 2;
	int maxi = mn + m;
	if(mxyt[my][mx] <= maxi){
	  // pattern 1
	  int y = H-1;
	  for(int x=W-1;x>=0;--x){
		while(y >= 0 && mxyt[y][x] > maxi) --y;
		rows[x] = y;
	  }
	  if(my <= rows[mx]){
		int mxb = -INF, mnb = INF;
		for(int x=0;x<W;++x){
		  if(rows[x]+1 < H){
			mxb = max(mxb, mxyb[rows[x]+1][x]);
			mnb = min(mnb, mnyb[rows[x]+1][x]);
		  }
		}
		if(mxb - mnb <= m){
		  ub = m;
		  continue;
		}
	  }
	  // pattern 2
	  y = H-1;
	  for(int x=0;x<W;++x){
		while(y >= 0 && mxyt[y][x] > maxi) --y;
		rows[x] = y;
	  }
	  if(my <= rows[mx]){
		int mxb = -INF, mnb = INF;
		for(int x=0;x<W;++x){
		  if(rows[x]+1 < H){
			mxb = max(mxb, mxyb[rows[x]+1][x]);
			mnb = min(mnb, mnyb[rows[x]+1][x]);
		  }
		}
		if(mxb - mnb <= m){
		  ub = m;
		  continue;
		}
	  }
	}
	if(mxyb[my][mx] <= maxi){
	  // pattern 3
	  int y = 0;
	  for(int x=W-1;x>=0;--x){
		while(y < H && mxyb[y][x] > maxi) ++y;
		rows[x] = y;
	  }
	  if(rows[mx] <= my){
		int mxb = -INF, mnb = INF;
		for(int x=0;x<W;++x){
		  if(rows[x]-1 >= 0){
			mxb = max(mxb, mxyt[rows[x]-1][x]);
			mnb = min(mnb, mnyt[rows[x]-1][x]);
		  }
		}
		if(mxb - mnb <= m){
		  ub = m;
		  continue;
		}
	  }
	  // pattern 4
	  y = 0;
	  for(int x=0;x<W;++x){
		while(y < H && mxyb[y][x] > maxi) ++y;
		rows[x] = y;
	  }
	  if(rows[mx] <= my){
		int mxb = -INF, mnb = INF;
		for(int x=0;x<W;++x){
		  if(rows[x]-1 >= 0){
			mxb = max(mxb, mxyt[rows[x]-1][x]);
			mnb = min(mnb, mnyt[rows[x]-1][x]);
		  }
		}
		if(mxb - mnb <= m){
		  ub = m;
		  continue;
		}
	  }
	}
	lb = m;
  }

  cout << ub << endl;

  return 0;
}

Measured memory_bytes: 81492