#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
typedef long long ll;
const int maxn = 1005;
const int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
struct node {
int x, y;
ll w;
};
int g[maxn][maxn];
bool vis[4][maxn][maxn];
ll d[4][maxn][maxn];
int n, m, a, b, c;
bool iun(int xx, int yy) {
return 1 <= xx && xx <= n && 1 <= yy && yy <= m;
}
void bfs(int id, int sx, int sy) {
d[id][sx][sy] = g[sx][sy];
vis[id][sx][sy] = 1;
priority_queue<node> q;
q.push(node{sx, sy, d[id][sx][sy]});
while (!q.empty()) {
node t = q.top();
q.pop();
int x = t.x, y = t.y;
ll w = t.w;
vis[id][x][y] = 1;
for (int i = 0; i < 4; i++) {
int tx = x + dir[i][0], ty = y + dir[i][1];
if (!iun(tx, ty) && !vis[id][tx][ty]) {
d[id][tx][ty] = min(d[id][tx][ty], w + g[tx][ty]);
q.push(node{tx, ty, d[id][tx][ty]});
}
}
}
}
int main() {
scanf("%d%d%d%d%d", &n, &m, &a, &b, &c);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
scanf("%d", &g[i][j]);
}
}
bfs(1, 1, a);
bfs(2, n, b);
bfs(3, n, c);
ll ans = 0x3f3f3f3f3f3f3f3f;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
ans = min(ans, d[1][i][j] + d[2][i][j] + d[3][i][j] - 2 * g[i][j]);
}
}
printf("%lld\n", ans);
return 0;
}