#include <bits/stdc++.h>
using namespace std;
signed main() {
  long long int n;
  cin >> n;
  long long int a[n][n];
  for (long long int i = 0; i < n; i++)
    for (long long int j = 0; j < n; j++) cin >> a[i][j];
  long long int x[n];
  for (long long int i = 0; i < n; i++) {
    cin >> x[i];
    x[i]--;
  }
  stack<long long int> s;
  vector<long long int> added;
  for (long long int k = n - 1; k >= 0; k--) {
    added.push_back(x[k]);
    for (long long int i = 0; i < n; i++)
      for (long long int j = 0; j < n; j++)
        a[i][j] = min(a[i][j], a[i][x[k]] + a[x[k]][j]);
    long long int sum = 0;
    for (long long int i : added) {
      for (long long int j : added) sum += a[i][j];
    }
    s.push(sum);
  }
  while (!s.empty()) {
    cout << s.top() << " ";
    s.pop();
  }
}
