#include <bits/stdc++.h>
using namespace std;
int n, p[510];
long long ans[510], a[510][510];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j) scanf("%I64d", &a[i][j]);
  for (int i = 1; i <= n; ++i) scanf("%d", &p[i]);
  for (int i = n; i >= 1; --i) {
    for (int j = n; j >= 1; --j)
      for (int k = n; k >= 1; --k)
        if (a[p[j]][p[i]] + a[p[i]][p[k]] < a[p[j]][p[k]])
          a[p[j]][p[k]] = a[p[j]][p[i]] + a[p[i]][p[k]];
    for (int j = i; j <= n; ++j)
      for (int k = i; k <= n; ++k) ans[i] += a[p[j]][p[k]];
  }
  for (int i = 1; i <= n; ++i) printf("%I64d%c", ans[i], i < n ? ' ' : '\n');
  return 0;
}
