#include <bits/stdc++.h>
using namespace std;
const int N = 500 + 5;
int dp[N][N], a[N][N], n, x[N];
long long que[N];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) scanf("%d", &a[i][j]);
  for (int t = 1; t <= n; t++) scanf("%d", x + t);
  int z;
  memset(dp, 127, sizeof dp);
  for (int i = 1; i <= n; i++) dp[i][i] = 0;
  long long ans;
  for (int t = n; t > 0; t--) {
    z = x[t];
    for (int i = 1; i <= n; i++) {
      dp[z][i] = min(dp[z][i], a[z][i]);
      dp[i][z] = min(dp[i][z], a[i][z]);
    }
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        dp[i][j] = min(dp[i][j], dp[i][z] + dp[z][j]);
    ans = 0;
    for (int i = t; i <= n; i++)
      for (int j = i + 1; j <= n; j++)
        ans += (long long)(dp[x[i]][x[j]] + dp[x[j]][x[i]]);
    que[t] = ans;
  }
  for (int i = 1; i <= n; i++) printf("%I64d ", que[i]);
  return 0;
}
