#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define sz size()
#define ff first
#define ss second
#define pb push_back
#define pii pair <int, int>
#define dur exit(0)
#define dur1 return(0)
const int N = 1e3 + 5;
ll n, m, a[N][N], jog;
int main () {
//	freopen ("input.txt", "r", stdin);
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin >> n >> m;
	for (ll i = 1; i <= n; ++i) {
		for (ll j = 1; j <= m; ++j) {
			cin >> a[i][j];
		}
	}
	for (ll i = 1; i <= n; ++i) {
		for (ll j = 1; j <= m; ++j) {
			for (ll l = 1; l <= n; ++l) {
				for (ll r = 1; r <= m; ++r) {
					ll cost = abs (a[l][r] - a[i][j]);
					ll cost1 = abs (i - l) + abs (r - j) + 1;
					jog = max (jog, cost - cost1);
//					if (jog == 3) {
//						cout << i << " " << j << " " << l << " " << r << "\n";
//						return 0;
//					}
				}
			}
		}
	}
	cout << jog;
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |