#include <bits/stdc++.h>
using namespace std;
const int N = 501;
long long d[N][N];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> d[i][j];
    }
  }
  vector<int> vertex(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> vertex[i];
  }
  reverse(vertex.begin() + 1, vertex.end());
  vector<long long> res;
  for (int l = 1; l <= n; l++) {
    int k = vertex[l];
    for (int i = 1; i <= n; i++) {
      int u = vertex[i];
      for (int j = 1; j <= n; j++) {
        int v = vertex[j];
        d[u][v] = min(d[u][v], d[u][k] + d[k][v]);
      }
    }
    long long sum = 0;
    for (int i = 1; i <= l; i++) {
      int u = vertex[i];
      for (int j = 1; j <= l; j++) {
        int v = vertex[j];
        sum += d[u][v];
      }
    }
    res.push_back(sum);
  }
  reverse(res.begin(), res.end());
  for (auto nx : res) {
    cout << nx << ' ';
  }
  return 0;
}
