#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
struct Node {
int r, c, dist;
bool operator > (const Node& b) const {
return dist > b.dist;
}
};
const int N = 1010;
const int INF = 0x3f3f3f3f;
int n, m;
int Ra, Rb, Rc;
int grid[N][N];
int distA[N][N];
int distB[N][N];
int distO[N][N];
bool st[N][N];
int dr[] = {-1, 1, 0, 0};
int dc[] = {0, 0, -1, 1};
void dijkstra(int x, int y, int dist[N][N]) {
memset(dist, 0x3f, sizeof(distA));
memset(st, false, sizeof(st));
priority_queue<Node, vector<Node>, greater<Node>> heap;
dist[x][y] = grid[x][y];
heap.push({x, y, dist[x][y]});
while (!heap.empty()) {
Node cur = heap.top();
heap.pop();
int r = cur.r;
int c = cur.c;
if (st[r][c]) {
continue;
}
st[r][c] = true;
for (int i = 0; i < 4; ++i) {
int r1 = r + dr[i];
int c1 = c + dc[i];
if (r1 >= 1 && r1 <= n && c1 >= 1 && c1 <= m) {
int new_dist = dist[r][c] + grid[r1][c1];
if (dist[r1][c1] > new_dist) {
dist[r1][c1] = new_dist;
heap.push({r1, c1, new_dist});
}
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int a, b, c;
cin >> n >> m >> a >> b >> c;
int x_O = 1, y_O = a;
int x_A = n, y_A = b;
int x_B = n, y_B = c;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
cin >> grid[i][j];
}
}
dijkstra(x_A, y_A, distA);
dijkstra(x_B, y_B, distB);
dijkstra(x_O, y_O, distO);
long long res = INF;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
long long l = (long long)distA[i][j] + distB[i][j] + distO[i][j] - 2 * grid[i][j];
if (res > l) {
res = l;
}
}
}
cout << res << endl;
return 0;
}