#include <bits/stdc++.h>
using namespace std;
const int n = 500;
const long long INF = 1e8;
long long D[n][n];
int order[n];
vector<long long> V;
int N;
int main() {
  cin >> N;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      cin >> D[i][j];
    }
  }
  for (int i = 0; i < N; ++i) {
    cin >> order[i];
    order[i]--;
  }
  for (int ii = 0; ii < N; ++ii) {
    int i = order[N - ii - 1];
    long long S = 0;
    for (int jj = 0; jj < N; ++jj) {
      for (int kk = 0; kk < N; ++kk) {
        int j = order[N - jj - 1];
        int k = order[N - kk - 1];
        D[j][k] = min(D[j][k], D[j][i] + D[i][k]);
        if (jj <= ii && kk <= ii) S += D[j][k];
      }
    }
    V.push_back(S);
  }
  for (int i = N - 1; i >= 0; --i) {
    cout << V[i] << " ";
  }
  cout << endl;
  return 0;
}
