#include <bits/stdc++.h>
#define ll long long
#define MAXN 1001
using namespace std;

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

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

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++) {
            dp[i][j] = min(dp[i-1][j], dp[i][j-1]);

            if (i+j-2 >= p[i][j]-1) {
                dp[i][j] += a[i][j];
            }
        }
    }

    cout << dp[N][N] << "\n";
}