#include <bits/stdc++.h>
using namespace std;
long long n, mas1[505][505], ielikts[505];
vector<long long> indexes;
stack<long long> rinda;
int main() {
  cin >> n;
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < n; j++) {
      cin >> mas1[i][j];
    }
  }
  long long k, sum = 0;
  for (long long i = 0; i < n; i++) {
    cin >> k;
    indexes.push_back(k - 1);
  }
  for (long long q = 0; q < n; q++) {
    sum = 0;
    k = indexes[indexes.size() - 1 - q];
    ielikts[k] = 1;
    for (long long i = 0; i < n; i++) {
      for (long long j = 0; j < n; j++) {
        if (i != j && i != k && j != k) {
          mas1[i][j] = min(mas1[i][j], mas1[i][k] + mas1[k][j]);
        }
        if (ielikts[i] == 1 && ielikts[j] == 1) {
          sum += mas1[i][j];
        }
      }
    }
    rinda.push(sum);
  }
  while (!rinda.empty()) {
    cout << rinda.top() << " ";
    rinda.pop();
  }
  return 0;
}
