#include <bits/stdc++.h>
using namespace std;
int read() {
  char ch;
  ch = getchar();
  while (ch < '0' || ch > '9') ch = getchar();
  int x = 0;
  while (ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
  return x;
}
int n;
int a[1001][1001];
long long int d[1001][1001];
int xs[10010];
long long int ans[10010];
int v[10010];
int main() {
  memset(v, 0, sizeof(v));
  n = read();
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) d[i][j] = read();
  for (int i = n; i >= 1; i--) xs[i] = read();
  for (int i = 1; i <= n; i++) {
    v[xs[i]] = 1;
    for (int l = 1; l <= n; l++)
      for (int ll = 1; ll <= n; ll++) {
        if (d[l][ll] > d[l][xs[i]] + d[xs[i]][ll])
          d[l][ll] = d[l][xs[i]] + d[xs[i]][ll];
        if (v[l] == 1 && v[ll] == 1) ans[i] += d[l][ll];
      }
  }
  for (int i = n; i >= 1; i--) cout << ans[i] << " ";
  return 0;
}
