#include <bits/stdc++.h>
using namespace std;
const int MAX = 504;
int removed[MAX];
long long graph[MAX][MAX];
long long ANS[MAX];
int n;
void floyd() {
  for (int v = n - 1; v >= 0; --v) {
    int k = removed[v];
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < n; ++j)
        graph[i][j] = min(graph[i][j], graph[i][k] + graph[k][j]);
    for (int i = v; i < n; ++i)
      for (int j = v; j < n; ++j) ANS[v] += graph[removed[i]][removed[j]];
  }
}
int main() {
  cin >> n;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j) cin >> graph[i][j];
  for (int i = 0; i < n; ++i) cin >> removed[i], removed[i]--;
  floyd();
  for (int i = 0; i < n; ++i) cout << ANS[i] << " ";
  cout << endl;
}
