#include <bits/stdc++.h>
using namespace std;
// Define a large constant for initialization
const int INF = 1e9;
// Struct to represent each state in the priority queue
struct State {
int digits_count; // Number of distinct digits used
int cost; // Total cost accumulated
int i, j; // Current cell position
int mask; // Bitmask representing digits used
// Operator to make the priority queue a min-heap based on digits_count and cost
bool operator<(const State& other) const {
if (digits_count != other.digits_count)
return digits_count > other.digits_count; // Minimize digits_count
return cost > other.cost; // If digits_count is the same, minimize cost
}
};
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int N;
cin >> N;
// Read costs for digits 0 to 9
int v[10];
for(int d=0; d<10; d++) cin >> v[d];
// Read the grid
vector<string> grid(N);
for(int i=0; i<N; i++) cin >> grid[i];
// Initialize distance array as a 1D vector
// distance[(i*N + j)*1024 + mask] represents the minimum cost to reach (i,j) with mask
vector<int> distance(N * N * 1024, INF);
// Define directions: up, down, left, right
vector<pair<int, int>> dirs = { {-1,0},{1,0},{0,-1},{0,1} };
// Initialize priority queue
priority_queue<State> pq;
// Initialize starting states (all cells in the first column)
for(int i=0; i<N; i++){
int j = 0;
int digit = grid[i][j] - '0';
int mask = (1 << digit);
int digits_count = 1;
int cost = v[digit];
int idx = (i * N + j) * 1024 + mask;
if(distance[idx] > cost){
distance[idx] = cost;
pq.push(State{digits_count, cost, i, j, mask});
}
}
// Dijkstra's algorithm
while(!pq.empty()){
State current = pq.top();
pq.pop();
// Current distance index
int current_idx = (current.i * N + current.j) * 1024 + current.mask;
// If this state has already been processed with a better cost, skip
if(current.cost > distance[current_idx]) continue;
// Explore all four directions
for(auto &dir : dirs){
int ni = current.i + dir.first;
int nj = current.j + dir.second;
// Check boundaries
if(ni < 0 || ni >= N || nj < 0 || nj >= N) continue;
int digit = grid[ni][nj] - '0';
int mask_new = current.mask | (1 << digit);
int digits_new = __builtin_popcount(mask_new);
int cost_new = current.cost + v[digit];
int new_idx = (ni * N + nj) * 1024 + mask_new;
// If a better cost is found for this new state, update and push to queue
if(cost_new < distance[new_idx]){
distance[new_idx] = cost_new;
pq.push(State{digits_new, cost_new, ni, nj, mask_new});
}
}
}
// After processing, find the minimum digits_count and corresponding min_cost among all end cells
int min_digits = 11; // Initialize to a value greater than the maximum possible (10)
int min_cost = INF;
for(int i=0; i<N; i++){
int j = N-1;
for(int mask=0; mask<1024; mask++){
int idx = (i * N + j) * 1024 + mask;
if(distance[idx] < INF){
int digits = __builtin_popcount(mask);
if(digits < min_digits || (digits == min_digits && distance[idx] < min_cost)){
min_digits = digits;
min_cost = distance[idx];
}
}
}
}
// Output the results
cout << min_digits << "\n" << min_cost;
}