#include <bits/stdc++.h>
using namespace std;
const int N = 1000;
long long n, b[N], ans[N], a[N][N], f[N][N];
int main() {
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) cin >> f[i][j];
  for (int i = 1; i <= n; i++) f[i][i] = 0;
  for (int i = n; i >= 1; i--) cin >> b[i];
  for (int i = 1; i <= n; i++) {
    ans[i] = 0;
    for (int j = 1; j <= n; j++)
      for (int k = 1; k <= n; k++)
        f[j][k] = min(f[j][k], f[j][b[i]] + f[b[i]][k]);
    for (int j = 1; j <= i; j++)
      for (int k = 1; k <= i; k++) ans[i] = ans[i] + f[b[j]][b[k]];
  }
  for (int i = n; i > 0; i--) cout << ans[i] << " ";
  return 0;
}
