#include <bits/stdc++.h>
using namespace std;
const int N = 500 + 5, INF = 1e9;
int n, x[N];
long long d[N][N], ans[N];
void FloyedWarshall(int k) {
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) d[i][j] = min(d[i][k] + d[k][j], d[i][j]);
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) cin >> d[i][j];
  for (int i = n - 1; i >= 0; i--) {
    cin >> x[i];
    x[i]--;
  }
  for (int i = 0; i < n; i++) {
    FloyedWarshall(x[i]);
    for (int p = 0; p <= i; p++)
      for (int q = 0; q <= i; q++) ans[i] += d[x[p]][x[q]];
  }
  for (int i = n - 1; i >= 0; i--) cout << ans[i] << ' ';
  cout << '\n';
  return 0;
}
