#include <bits/stdc++.h>
using namespace std;
int const bound = 2e6 + 2, N = 5e5 + 2;
long long OO = 1e18;
double eps = 1e-6;
int oo = 1e9, mod = oo + 7;
int dist[501][501], org[501][501], aa[501];
long long res[501];
bool rem[501];
int main() {
  int n;
  scanf("%d", &n);
  for (int(i) = 0; (i) < (n); (i)++)
    for (int(j) = 0; (j) < (n); (j)++)
      scanf("%d", &dist[i][j]), org[i][j] = dist[i][j];
  for (int i = n - 1; i > -1; i--) scanf("%d", &aa[i]);
  for (int(p) = 0; (p) < (n); (p)++) {
    aa[p]--;
    rem[aa[p]] = 1;
    for (int(i) = 0; (i) < (n); (i)++)
      for (int(j) = 0; (j) < (n); (j)++)
        dist[i][j] = min(dist[i][j], dist[i][aa[p]] + dist[aa[p]][j]);
    long long an = 0;
    for (int(i) = 0; (i) < (n); (i)++)
      for (int(j) = 0; (j) < (n); (j)++)
        if (rem[i] && rem[j]) an += dist[i][j];
    res[p] = an;
  }
  for (int i = n - 1; i > -1; i--) printf("%lld ", res[i]);
  return 0;
}
