#include <bits/stdc++.h>
using namespace std;
int n;
long long floyd[502][502], c[502][502];
int del[600];
bool update[503][503][503];
long long res[600];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j) scanf("%lld", &floyd[i][j]);
  for (int i = 1; i <= n; ++i) scanf("%d", &del[i]);
  for (int idx = n; idx >= 1; --idx) {
    int k = del[idx];
    for (int i = 1; i <= n; ++i)
      for (int j = 1; j <= n; ++j)
        floyd[i][j] = min(floyd[i][j], floyd[i][k] + floyd[k][j]);
    for (int i = idx; i <= n; ++i)
      for (int j = idx; j <= n; ++j) res[idx] += floyd[del[i]][del[j]];
  }
  for (int i = 1; i <= n; ++i) printf("%lld ", res[i]);
  return 0;
}
