#include <bits/stdc++.h>
using namespace std;
const long long int INF = 1e9 + 7;
int main() {
  long long int n;
  scanf("%lld", &n);
  long long int i, j;
  long long int dp[505][505];
  for (i = 0; i <= n; i++)
    for (j = 0; j <= n; j++) dp[i][j] = INF;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++) scanf("%lld", &dp[i][j]);
  long long int k;
  long long int x[505];
  for (i = 1; i <= n; i++) scanf("%lld", &x[i]);
  long long int res[505];
  for (k = n; k >= 1; k--) {
    long long int sum = 0;
    for (i = 1; i <= n; i++) {
      for (j = 1; j <= n; j++) {
        dp[i][j] = min(dp[i][j], dp[i][x[k]] + dp[x[k]][j]);
      }
    }
    for (i = k; i <= n; i++) {
      for (j = k; j <= n; j++) {
        sum += dp[x[i]][x[j]];
      }
    }
    res[k] = sum;
  }
  for (i = 1; i <= n; i++) cout << res[i] << " ";
  return 0;
}
