#include <bits/stdc++.h>
using namespace std;
long long int d[550][550], x[550], ans[550];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) cin >> d[i][j];
  for (int i = 1; i <= n; i++) cin >> x[i];
  for (int k = n; k > 0; k--) {
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        d[i][j] = min(d[i][j], d[i][x[k]] + d[x[k]][j]);
    for (int i = k; i <= n; i++)
      for (int j = k; j <= n; j++) ans[k] += d[x[i]][x[j]];
  }
  for (int i = 1; i <= n; i++) cout << ans[i] << " ";
}
