#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  int **dist;
  int *x;
  long long *summ;
  cin >> n;
  dist = new int *[n];
  x = new int[n];
  summ = new long long[n];
  int i, j, k, t, a, b, c;
  long long temp;
  i = (0);
  for (int _n = (int)(n), _di = (i > _n) ? -1 : 1; i != _n; i += _di) {
    dist[i] = new int[n];
    j = (0);
    for (int _n = (int)(n), _di = (j > _n) ? -1 : 1; j != _n; j += _di) {
      cin >> dist[i][j];
    }
  }
  i = (0);
  for (int _n = (int)(n), _di = (i > _n) ? -1 : 1; i != _n; i += _di) {
    cin >> t;
    x[i] = t - 1;
  }
  a = (n - 1);
  for (int _n = (int)(-1), _di = (a > _n) ? -1 : 1; a != _n; a += _di) {
    k = x[a];
    i = (0);
    for (int _n = (int)(n), _di = (i > _n) ? -1 : 1; i != _n; i += _di) {
      if (k != i) {
        j = (0);
        for (int _n = (int)(n), _di = (j > _n) ? -1 : 1; j != _n; j += _di) {
          if (k != j && i != j) {
            t = dist[i][k] + dist[k][j];
            if (t < dist[i][j]) {
              dist[i][j] = t;
            }
          }
        }
      }
    }
    temp = 0;
    b = (a);
    for (int _n = (int)(n), _di = (b > _n) ? -1 : 1; b != _n; b += _di) {
      i = x[b];
      c = (a);
      for (int _n = (int)(n), _di = (c > _n) ? -1 : 1; c != _n; c += _di) {
        j = x[c];
        temp += dist[i][j];
      }
    }
    summ[a] = temp;
  }
  i = (0);
  for (int _n = (int)(n), _di = (i > _n) ? -1 : 1; i != _n; i += _di) {
    cout << summ[i] << " ";
  }
  cout << endl;
  return 0;
}
