#include <iostream>
#include <vector>
#include <queue>

using namespace std;

int main() {
    while (true) {
        int r;
        cin >> r;
        if (r == 0)break;
        vector<vector<int>> v(2, vector<int>(r + 1));
        for (int _ = 0; _ < 2; _++) {
            int w, h, x, y;
            cin >> w >> h >> x >> y;
            vector<vector<int>> ban(h, vector<int>(w));
            for (int i = 0; i < h; i++) {
                for (int j = 0; j < w; j++)cin >> ban[i][j];
            }
            priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> q;
            q.push({1, y, x});
            vector<vector<bool>> visited(h + 2, vector<bool>(w + 2));
            visited[y][x] = true;
            v[_][1] = 1;
            for (int i = 0; i <= h + 1; i++) {
                visited[i][0] = true;
                visited[i][w + 1] = true;
            }
            for (int i = 0; i <= w + 1; i++) {
                visited[0][i] = true;
                visited[h + 1][i] = true;
            }

            vector<vector<int>> move = {{1,  0},
                                        {-1, 0},
                                        {0,  1},
                                        {0,  -1}};
            for (int i = 2; i <= r; i++) {
                vector<int> a = q.top();
                q.pop();
                for (int j = 0; j < 4; j++) {
                    int c = a[1] + move[j][0];
                    int d = a[2] + move[j][1];
                    if (!visited[c][d]) {
                        visited[c][d] = true;
                        vector<int> aaa(3);
                        aaa[0] = ban[c - 1][d - 1];
                        aaa[1] = c;
                        aaa[2] = d;
                        q.push(aaa);
                    }
                }
                if (q.empty())break;
                v[_][i] = max(v[_][i - 1], q.top()[0]);
            }
        }
        int ret = 1145141919;
        for (int i = 0; i <= r; i++) {
            if ((i == 0 || v[0][i] != 0) && (r - i == 0 || v[1][r - i] != 0))
                ret = min(ret, v[0][i] + v[1][r - i]);
        }
        cout << ret << endl;
    }
    return 0;
}
