#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
const ll INF = 1e16;
int n;
char board[13][13];
int weight[13][13];
ll dp[1 << 24];
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
if (!(cin >> n)) return 0;
for (int i = 0; i < n; i++) cin >> board[i];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) cin >> weight[i][j];
int max_mask = 1 << (2 * n);
fill(dp, dp + max_mask, INF);
int start_mask = ((1 << n) - 1) << n;
dp[start_mask] = 0;
for (int mask = start_mask; mask >= 0; mask--) {
if (dp[mask] >= INF) continue;
if (__builtin_popcount(mask) != n) continue;
int rs[13], cs[13], bits[13];
int cnt = 0;
int r = 0, c = 0;
for (int i = 2 * n - 1; i > 0; i--) {
if (((mask >> i) & 1) && !((mask >> (i - 1)) & 1)) {
rs[cnt] = r;
cs[cnt] = c;
bits[cnt] = i;
cnt++;
}
if ((mask >> i) & 1) c++; else r++;
}
for (int i = 0; i < cnt; i++) {
int r1 = rs[i], c1 = cs[i], b1 = bits[i];
int mask_after_i = mask ^ (3 << (b1 - 1));
if (dp[mask_after_i] > dp[mask] + weight[r1][c1]) {
dp[mask_after_i] = dp[mask] + weight[r1][c1];
}
for (int j = i + 1; j < cnt; j++) {
int r2 = rs[j], c2 = cs[j], b2 = bits[j];
if (board[r1][c1] != board[r2][c2]) {
int mask_after_pair = mask_after_i ^ (3 << (b2 - 1));
ll cost = abs(weight[r1][c1] - weight[r2][c2]);
if (dp[mask_after_pair] > dp[mask] + cost) {
dp[mask_after_pair] = dp[mask] + cost;
}
}
}
}
}
cout << dp[(1 << n) - 1] << endl;
return 0;
}