#include <bits/stdc++.h>
using namespace std;
 
#define ll long long
#define pii pair<ll, ll>
#define fi first
#define sec second
#define ld long double
const int MAXN = 2e5;
const ll INF = 1e18;
const int MOD = 998244353;
const ld eps = 1e-6;
ll C[105][105];
ll dp[105][105][2];
int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);		
	int tc = 1;
	// cin >> tc;
	for(;tc--;){
		ll N, M, A, B, K; cin >> N >> M >> A >> B >> K;
		K /= 2;
		for(int i = 1; i <= N; i++){
			for(int j = 1; j <= M; j++){
				cin >> C[i][j];
				dp[i][j][0] = dp[i][j][1] = -INF;
			}
		}
		ll batas = min(K, 10000LL);
		dp[A][B][0] = 0;
		for(int k = 1; k <= batas; k++){
			for(int i = 1; i <= N; i++){
				for(int j = 1; j <= M; j++){
					dp[i][j][k % 2] = -INF;
					if(i > 1 && dp[i - 1][j][1 - k % 2] != -INF) dp[i][j][k % 2] = max(dp[i][j][k % 2], dp[i - 1][j][(1 - k % 2)] + C[i][j]);
					if(i < N && dp[i + 1][j][1 - k % 2] != -INF) dp[i][j][k % 2] = max(dp[i][j][k % 2], dp[i + 1][j][(1 - k % 2)] + C[i][j]);
					if(j > 1 && dp[i][j - 1][1 - k % 2] != -INF) dp[i][j][k % 2] = max(dp[i][j][k % 2], dp[i][j - 1][(1 - k % 2)] + C[i][j]);
					if(j < M && dp[i][j + 1][1 - k % 2] != -INF) dp[i][j][k % 2] = max(dp[i][j][k % 2], dp[i][j + 1][(1 - k % 2)] + C[i][j]);
				}
			}
		}
		
		ll ans = 0;
		for(int i = 1; i <= N; i++){
			for(int j = 1; j <= M; j++){
				ll res = K - batas;
				if(dp[i][j][batas % 2] == -INF) continue;
				if(i > 1) ans = max(ans, 2LL * (dp[i][j][batas % 2] + C[i][j] * (res / 2) + C[i - 1][j] * ((res + 1) / 2)) - (res % 2 ? C[i - 1][j] : C[i][j]));
				if(i < N) ans = max(ans, 2LL * (dp[i][j][batas % 2] + C[i][j] * (res / 2) + C[i + 1][j] * ((res + 1) / 2)) - (res % 2 ? C[i + 1][j] : C[i][j]));
				if(j > 1) ans = max(ans, 2LL * (dp[i][j][batas % 2] + C[i][j] * (res / 2) + C[i][j - 1] * ((res + 1) / 2)) - (res % 2 ? C[i][j - 1] : C[i][j]));
				if(j < M) ans = max(ans, 2LL * (dp[i][j][batas % 2] + C[i][j] * (res / 2) + C[i][j + 1] * ((res + 1) / 2)) - (res % 2 ? C[i][j + 1] : C[i][j]));
			}
		}
		
		cout << ans << "\n";
	}
}
| # | 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... | 
| # | 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... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |