#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long int n;
  cin >> n;
  vector<vector<long long int>> weights(n, vector<long long int>(n));
  for (long long int i = 0; i < n; i++) {
    for (long long int j = 0; j < n; j++) {
      cin >> weights[i][j];
    }
  }
  vector<long long int> deleted(n);
  for (long long int i = 0; i < n; i++) {
    cin >> deleted[i];
    deleted[i]--;
  }
  vector<long long int> sums;
  vector<vector<long long int>> min_paths(
      n, vector<long long int>(n, LONG_LONG_MAX));
  for (long long int addedi = (long long int)deleted.size() - 1; addedi >= 0;
       addedi--) {
    long long int sum = 0;
    long long int added = deleted[addedi];
    for (long long int finali = (long long int)deleted.size() - 1;
         finali > addedi; finali--) {
      long long int final = deleted[finali];
      for (long long int middlei = (long long int)deleted.size() - 1;
           middlei > addedi; middlei--) {
        long long int middle = deleted[middlei];
        min_paths[added][final] =
            min(min_paths[added][final],
                weights[added][middle] + min_paths[middle][final]);
        min_paths[final][added] =
            min(min_paths[final][added],
                min_paths[final][middle] + weights[middle][added]);
      }
      sum += min_paths[added][final];
      sum += min_paths[final][added];
    }
    min_paths[added][added] = 0;
    for (long long int fromi = (long long int)deleted.size() - 1;
         fromi > addedi; fromi--) {
      long long int from = deleted[fromi];
      for (long long int toi = (long long int)deleted.size() - 1; toi > addedi;
           toi--) {
        long long int to = deleted[toi];
        min_paths[from][to] = min(
            min_paths[from][to], min_paths[from][added] + min_paths[added][to]);
        sum += min_paths[from][to];
      }
    }
    sums.push_back(sum);
  }
  for (long long int i = (long long int)sums.size() - 1; i >= 0; i--) {
    cout << sums[i] << " ";
  }
  cout << '\n';
  return 0;
}
