#include <bits/stdc++.h>
using namespace std;
long long weight[502][502], sum[502], dlt[502];
int n;
void floyd_warshall(int p);
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) cin >> weight[i][j];
  for (int i = 0; i < n; i++) cin >> dlt[i];
  for (int i = n - 1; i >= 0; i--) {
    int x = dlt[i] - 1;
    floyd_warshall(i);
    sum[i] = 0;
    for (int k = i; k < n; k++)
      for (int j = i; j < n; j++) {
        int a = dlt[k] - 1, c = dlt[j] - 1;
        sum[i] += weight[a][c];
      }
  }
  for (int i = 0; i < n; i++) cout << sum[i] << ' ';
  return 0;
}
void floyd_warshall(int p) {
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      int k = dlt[p] - 1;
      weight[i][j] = min(weight[i][j], weight[i][k] + weight[k][j]);
    }
}
