#include <bits/stdc++.h>
using namespace std;
const int Inf = 1e8;
const int N = 5e2 + 1;
int w[N][N], d[N][N], x[N];
long long ans[N];
bool is[N];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      cin >> w[i][j];
      d[i][j] = Inf;
    }
  for (int i = 0; i < n; i++) {
    d[i][i] = 0;
    cin >> x[i];
    --x[i];
  }
  for (int i = n - 1; i > -1; i--) {
    is[x[i]] = true;
    for (int j = 0; j < n; j++) {
      d[x[i]][j] = min(d[x[i]][j], w[x[i]][j]);
      d[j][x[i]] = min(d[j][x[i]], w[j][x[i]]);
    }
    for (int j = 0; j < n; j++)
      for (int k = 0; k < n; k++)
        d[j][k] = min(d[j][k], d[j][x[i]] + d[x[i]][k]);
    for (int j = 0; j < n; j++)
      for (int k = 0; k < n; k++)
        if (is[j] && is[k]) ans[i] += d[j][k];
  }
  for (int i = 0; i < n; i++) cout << ans[i] << ' ';
  return 0;
}
