#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, i, j, k, dis[501][501];
  long long sum;
  int x[501];
  vector<long long> v;
  cin >> n;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++) cin >> dis[i][j];
  for (i = 1; i <= n; i++) cin >> x[i];
  for (k = n; k >= 1; k--) {
    sum = 0;
    for (i = n; i >= 1; i--) {
      for (j = n; j >= 1; j--) {
        dis[x[i]][x[j]] =
            min(dis[x[i]][x[j]], dis[x[i]][x[k]] + dis[x[k]][x[j]]);
        if (i >= k && j >= k) sum += dis[x[i]][x[j]];
      }
    }
    v.push_back(sum);
  }
  for (i = v.size() - 1; i >= 0; i--) {
    cout << v[i] << ' ';
  }
  cout << endl;
  return 0;
}
