#include <bits/stdc++.h>
using namespace std;
long long gcd(long long n1, long long n2) {
  if (!n1) return n2;
  if (!n2) return n1;
  if (n1 % n2 == 0) return n2;
  return gcd(n2, n1 % n2);
}
long long powmod(long long base, long long exponent) {
  base %= 1000000007;
  long long ans = 1;
  while (exponent) {
    if (exponent & 1) ans = (ans * base) % 1000000007;
    base = (base * base) % 1000000007;
    exponent /= 2;
  }
  ans %= 1000000007;
  return ans;
}
int arr[502][502];
long long dp[502][502];
long long answer[502];
int x[502];
int main() {
  int i, j, k, n;
  scanf("%d", &n);
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++) {
      scanf("%d", &arr[i][j]);
      if (i != j) dp[i][j] = 1000000000000000100;
    }
  for (i = n; i >= 1; i--) scanf("%d", &x[i]);
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= i; j++) {
      for (k = 1; k <= i; k++) {
        dp[x[i]][x[j]] = min(dp[x[i]][x[j]], arr[x[i]][x[k]] + dp[x[k]][x[j]]);
        dp[x[j]][x[i]] = min(dp[x[j]][x[i]], dp[x[j]][x[k]] + arr[x[k]][x[i]]);
      }
      answer[i] += (dp[x[i]][x[j]] + dp[x[j]][x[i]]);
    }
    for (j = 1; j < i; j++)
      for (k = 1; k < i; k++) {
        dp[x[j]][x[k]] = min(dp[x[j]][x[k]], dp[x[j]][x[i]] + dp[x[i]][x[k]]);
        answer[i] += dp[x[j]][x[k]];
      }
  }
  for (i = n; i >= 1; i--) printf("%lld ", answer[i]);
  return 0;
}
