#include <bits/stdc++.h>
using namespace std;
const int N = 505;
long long f[N][N], ans[N];
int n, del[N];
inline void floyd() {
  for (int k = 1; k <= n; k++) {
    int K = del[k];
    for (int i = 1; i <= n; i++) {
      int I = del[i];
      for (int j = 1; j <= n; j++) {
        int J = del[j];
        f[I][J] = min(f[I][J], f[I][K] + f[K][J]);
        if (i <= k && j <= k) ans[k] += f[I][J];
      }
    }
  }
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      scanf("%lld", &f[i][j]);
    }
  for (int i = n; i >= 1; i--) scanf("%d", &del[i]);
  floyd();
  for (int i = n; i >= 1; i--) cout << ans[i] << " ";
}
