#include <bits/stdc++.h>
using namespace std;
long long dis[600][600];
long long d[600][600];
int tmp[600], a[600];
long long ans[600];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) cin >> dis[i][j];
  for (int i = 1; i <= n; i++) cin >> tmp[i];
  for (int i = 1; i <= n; i++) a[n + 1 - i] = tmp[i];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < i; j++) {
      d[a[i]][a[j]] = dis[a[i]][a[j]];
      d[a[j]][a[i]] = dis[a[j]][a[i]];
    }
    for (int j = 1; j < i; j++)
      for (int k = 1; k < i; k++) {
        d[a[j]][a[k]] = min(d[a[j]][a[k]], d[a[j]][a[i]] + d[a[i]][a[k]]);
        d[a[k]][a[j]] = min(d[a[k]][a[j]], d[a[k]][a[i]] + d[a[i]][a[j]]);
      }
    for (int k = 1; k < i; k++)
      for (int j = 1; j < i; j++) {
        d[a[i]][a[j]] = min(d[a[i]][a[j]], d[a[i]][a[k]] + d[a[k]][a[j]]);
        d[a[j]][a[i]] = min(d[a[j]][a[i]], d[a[j]][a[k]] + d[a[k]][a[i]]);
      }
    for (int j = 1; j < i; j++)
      for (int k = 1; k < i; k++) {
        d[a[j]][a[k]] = min(d[a[j]][a[k]], d[a[j]][a[i]] + d[a[i]][a[k]]);
        d[a[k]][a[j]] = min(d[a[k]][a[j]], d[a[k]][a[i]] + d[a[i]][a[j]]);
      }
    for (int k = 1; k < i; k++)
      for (int j = 1; j < i; j++) {
        d[a[i]][a[j]] = min(d[a[i]][a[j]], d[a[i]][a[k]] + d[a[k]][a[j]]);
        d[a[j]][a[i]] = min(d[a[j]][a[i]], d[a[j]][a[k]] + d[a[k]][a[i]]);
      }
    long long sum = 0;
    for (int j = 1; j <= i; j++)
      for (int k = 1; k <= i; k++) sum += d[a[j]][a[k]];
    ans[i] = sum;
  }
  for (int i = n; i >= 1; i--) cout << ans[i] << " ";
}
