#include <bits/stdc++.h>
using namespace std;
const int maxn = 505;
int n, g[maxn][maxn], x[maxn], f[maxn][maxn];
long long ans[maxn];
void test(int i) {
  printf("[test] i = %d\n", i);
  for (int j = i; j <= n; j++) {
    for (int k = i; k <= n; k++) {
      printf("%4d ", g[x[j]][x[k]]);
    }
    puts("");
  }
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      cin >> g[i][j];
    }
  for (int i = 1; i <= n; i++) cin >> x[i];
  for (int i = n; i >= 1; i--) {
    int p = x[i];
    for (int j = i; j <= n; j++) {
      for (int k = i; k <= n; k++) {
        g[p][x[j]] = min(g[p][x[j]], g[p][x[k]] + g[x[k]][x[j]]);
        g[x[j]][p] = min(g[x[j]][p], g[x[j]][x[k]] + g[x[k]][p]);
      }
    }
    for (int j = i; j <= n; j++)
      for (int k = i; k <= n; k++)
        g[x[j]][x[k]] = min(g[x[j]][x[k]], g[x[j]][p] + g[p][x[k]]);
    for (int j = i; j <= n; j++) {
      for (int k = i; k <= n; k++) {
        ans[i] += g[x[j]][x[k]];
      }
    }
  }
  for (int i = 1; i <= n; i++) cout << ans[i] << " ";
  return 0;
}
