#include <bits/stdc++.h>
using namespace std;
long long n, i, j, k, a, b;
long long F[505][505];
long long arr[505], top;
long long A[505][505];
long long H[505];
vector<long long> ans;
int main() {
  ios::sync_with_stdio(false);
  memset(F, 3, sizeof F);
  cin >> n;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++) cin >> A[i][j];
  for (i = 1; i <= n; i++) cin >> arr[i];
  for (i = n; i >= 1; i--) {
    top = 0;
    H[arr[i]] = 1;
    for (a = 1; a <= n; a++) {
      if (H[a]) {
        F[a][arr[i]] = A[a][arr[i]];
        F[arr[i]][a] = A[arr[i]][a];
      }
    }
    for (a = 1; a <= n; a++)
      for (b = 1; b <= n; b++) {
        if (H[a] and H[b])
          F[a][arr[i]] = min(F[a][arr[i]], F[a][b] + F[b][arr[i]]);
      }
    for (a = 1; a <= n; a++)
      for (b = 1; b <= n; b++) {
        if (H[a] and H[b])
          F[arr[i]][a] = min(F[arr[i]][a], F[arr[i]][b] + F[b][a]);
      }
    for (a = 1; a <= n; a++)
      for (b = 1; b <= n; b++)
        if (H[a] and H[b]) F[a][b] = min(F[a][b], F[a][arr[i]] + F[arr[i]][b]);
    for (a = 1; a <= n; a++)
      for (b = 1; b <= n; b++)
        if (H[a] and H[b]) top += F[a][b];
    ans.push_back(top);
  }
  reverse(ans.begin(), ans.end());
  for (i = 0; i < ans.size(); i++) cout << ans[i] << ' ';
}
