#include <bits/stdc++.h>
using namespace std;
const int MAXN = 505;
int n;
int mat[MAXN][MAXN];
int dist[MAXN][MAXN];
bool bio[MAXN];
vector<int> perm;
vector<long long> sol;
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      scanf("%d", &mat[i][j]);
    }
  }
  for (int i = 0; i < n; i++) {
    int x;
    scanf("%d", &x);
    x--;
    perm.push_back(x);
  }
  reverse(perm.begin(), perm.end());
  for (int a = 0; a < n; a++) {
    int sada = perm[a];
    bio[sada] = 1;
    long long sum = 0ll;
    for (int i = 0; i < n; i++)
      for (int k = 0; k < n; k++)
        if (bio[i] && bio[k])
          mat[sada][i] = min(mat[sada][i], mat[sada][k] + mat[k][i]);
    for (int i = 0; i < n; i++)
      for (int k = 0; k < n; k++)
        if (bio[i] && bio[k])
          mat[i][sada] = min(mat[i][sada], mat[i][k] + mat[k][sada]);
    for (int i = 0; i < n; i++)
      for (int k = 0; k < n; k++)
        if (bio[i] && bio[k])
          mat[i][k] = min(mat[i][k], mat[i][sada] + mat[sada][k]);
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        if (bio[i] && bio[j]) sum = sum + (long long)mat[i][j];
    sol.push_back(sum);
  }
  for (int i = n - 1; i >= 0; i--) printf("%I64d ", sol[i]);
  return 0;
}
