#include <bits/stdc++.h>
using namespace std;
long long d[510][510], sum[510];
int x[510];
int main() {
  int n, i, j, a, b;
  scanf("%d", &n);
  for (i = 1; i <= n; ++i)
    for (j = 1; j <= n; ++j) cin >> d[i][j];
  for (i = 1; i <= n; ++i) scanf("%d", &x[i]);
  for (i = n; i; --i) {
    for (a = 1; a <= n; ++a) {
      for (b = 1; b <= n; ++b) {
        d[a][b] = min(d[a][b], d[a][x[i]] + d[x[i]][b]);
      }
    }
    for (a = i; a <= n; ++a) {
      for (b = i; b <= n; ++b) {
        sum[i] += d[x[a]][x[b]];
      }
    }
  }
  for (i = 1; i < n; ++i) cout << sum[i] << " ";
  cout << sum[i] << endl;
  return 0;
}
