#include <bits/stdc++.h>
using namespace std;
long long Mat[510][510], Ans[510];
int N, X[510];
bool Active[510];
int main() {
  int i, j, k;
  cin >> N;
  for (i = 1; i <= N; ++i)
    for (j = 1; j <= N; ++j) cin >> Mat[i][j];
  for (i = 1; i <= N; ++i) cin >> X[i];
  for (i = N; i; --i) {
    Ans[i] = 0;
    Active[X[i]] = 1;
    for (j = 1; j <= N; ++j)
      for (k = 1; k <= N; ++k)
        if (Active[j] && Active[k])
          Mat[X[i]][j] = min(Mat[X[i]][j], Mat[X[i]][k] + Mat[k][j]);
    for (j = 1; j <= N; ++j)
      for (k = 1; k <= N; ++k)
        if (Active[j] && Active[k])
          Mat[j][X[i]] = min(Mat[j][X[i]], Mat[j][k] + Mat[k][X[i]]);
    for (j = 1; j <= N; ++j)
      for (k = 1; k <= N; ++k)
        if (Active[j] && Active[k])
          Mat[j][k] = min(Mat[j][k], Mat[j][X[i]] + Mat[X[i]][k]);
    for (j = 1; j <= N; ++j)
      for (k = 1; k <= N; ++k)
        if (Active[j] && Active[k]) Ans[i] += 1LL * Mat[j][k];
  }
  for (i = 1; i <= N; ++i) cout << Ans[i] << " ";
  return 0;
}
