#include <bits/stdc++.h>
#define ll long long
#define MAXN 1001
#define INF ((ll)(1e9))
using namespace std;

int N;
int p[MAXN][MAXN];
ll a[MAXN][MAXN];
queue<tuple<int, int, int>> q;

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

inline bool pustinja(int i, int j, int t) {
    return t >= p[i][j];
}

unordered_map<int, unordered_map<int, unordered_map<int, int>>> memo;

ll f(int i, int j, int t, bool cekamo = false)
{
    ll trenutno = !cekamo && pustinja(i, j, t) ? a[i][j] : 0;

    if (i == N && j == N)
    {
        return trenutno;
    }

    if (memo[i][j][t] != 0) {
        return memo[i][j][t];
    }

    ll ans = INF;
    if (i < N)
    {
        ans = min(ans, f(i + 1, j, t + 1) + trenutno);
    }

    if (j < N)
    {
        ans = min(ans, f(i, j + 1, t + 1) + trenutno);
    }


    bool wait = false;

    if (i < N) {
        if (a[i+1][j] < 0) {
            if (!pustinja(i+1, j, t+1) && p[i+1][j] != 0) {
                wait = true;
            }
        }
    }
    if (j < N) {
        if (a[i][j+1] < 0) {
            if (!pustinja(i, j+1, t+1) && p[i][j+1] != 0) {
                wait = true;
            }
        }
    }

    if (wait) {
        ans = min(ans, f(i, j, t+1, true));
    }

    return memo[i][j][t] = ans;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> N;

    for (int i = 1; i <= N; i++)
    {
        string s;
        cin >> s;
        for (int j = 0; j < N; j++)
        {
            p[i][j + 1] = !(s[j] - '0');
            if (p[i][j + 1])
            {
                q.emplace(i, j + 1, 1);
            }
        }
    }

    for (int i = 1; i <= N; i++)
    {
        for (int j = 1; j <= N; j++)
        {
            cin >> a[i][j];
        }
    }

    while (!q.empty())
    {
        int x, y, d;
        tie(x, y, d) = q.front();
        q.pop();

        for (int i = 0; i < 4; i++)
        {
            int nx = x + dx[i], ny = y + dy[i];
            if (nx > 0 && nx <= N && ny > 0 && ny <= N)
            {
                if (p[nx][ny] == 0)
                {
                    p[nx][ny] = d + 1;
                    q.emplace(nx, ny, d + 1);
                }
            }
        }
    }

    for (int i = 1; i <= N; i++)
    {
        for (int j = 1; j <= N; j++)
        {
            p[i][j]--;
        }
    }

    cout << f(1, 1, 0) << "\n";
}

/*

5
11111
11101
10111
11111
11101
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1


*/