#include <bits/stdc++.h>
using namespace std;
int a[501][501], x[501];
long long int an[501][501], pn[501];
int main() {
  int n, i, j, k, vv, u;
  cin >> n;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++) cin >> a[i][j];
  memset(x, 0, sizeof(x));
  for (i = 1; i <= n; i++) {
    cin >> x[i];
  }
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++) an[i][j] = a[i][j];
  for (u = n; u; u--) {
    k = x[u];
    for (i = 1; i <= n; i++)
      for (j = 1; j <= n; j++) an[i][j] = min(an[i][j], an[i][k] + an[k][j]);
    for (i = u; i <= n; i++)
      for (j = u; j <= n; j++) pn[u] += an[x[i]][x[j]];
  }
  for (i = 1; i <= n; i++) {
    cout << pn[i] << " ";
  }
  cout << endl;
  return 0;
}
