#include <bits/stdc++.h>
using namespace std;
const int SIZE = 505;
const int INF = 0x3f3f3f3f;
const double eps = 1e-5;
const double PI = atan(1.0) * 4.0;
const long long mod = 1e9 + 7;
int n;
int a[SIZE][SIZE], x[SIZE], in[SIZE];
long long ans[SIZE];
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 i = 1; i <= n; i++) scanf("%d", &x[i]);
  for (int i = n; i; i--) {
    int c = x[i];
    in[c] = 1;
    for (int j = 1; j <= n; j++)
      for (int k = 1; k <= n; k++) {
        if (a[j][c] + a[c][k] < a[j][k]) a[j][k] = a[j][c] + a[c][k];
        if (in[j] && in[k]) ans[i] += a[j][k];
      }
  }
  for (int i = 1; i <= n; i++) printf("%I64d ", ans[i]);
  return 0;
}
