#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:256000000")
using namespace std;
const int maxN = 510;
long long d[maxN][maxN];
long long order[maxN];
int n;
int main() {
  ios_base::sync_with_stdio(false);
  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];
  }
  reverse(order, order + n);
  vector<long long> results;
  for (int k = 0; k < n; ++k) {
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        d[i][j] = min(d[i][j], d[i][order[k]] + d[order[k]][j]);
      }
    }
    long long sum = 0;
    for (int i = 0; i <= k; ++i) {
      for (int j = 0; j <= k; ++j) {
        if (order[i] != order[j]) {
          sum += d[order[i]][order[j]];
        }
      }
    }
    results.push_back(sum);
  }
  reverse(results.begin(), results.end());
  for (int i = 0; i < results.size(); ++i) {
    cout << results[i] << " ";
  }
  cout << endl;
  return 0;
}
