#include<bits/stdc++.h>
using namespace std;

int main(){
	int n;
	
	cin >> n;
	
	vector<vector<int>> mat(n, vector<int> (n));
	for(int i = 0; i < n; ++i){
		string s;
		cin >> s;
		for(auto j : s){
			mat[i][j] = s[j];
		}
	}
	
	vector<vector<int>> tok(n, vector<int> (n));
	for(int i = 0; i < n; ++i){
		for(int j = 0; j < n; ++j){
			cin >> tok[i][j];
		}
	}
	
	vector<vector<int>> dp(n, vector<int> (n));
	dp[0][0] = tok[0][0];
	for(int i = 1; i < n; ++i){
		dp[0][i] = dp[0][i-1]+tok[0][i];
		dp[i][0] = dp[i-1][0]+tok[i][0];
	}
	for(int i = 1; i < n; ++i){
		for(int j = 1; j < n; ++j){
			dp[i][j] = tok[i][j]+min(dp[i-1][j], dp[i][j-1]);
		}
	}
	cout << dp[n-1][n-1];
}