#include <bits/stdc++.h>
using namespace std;
const int N = 505;
int n;
int Node[N];
long long Arr[N][N];
vector<long long> Ans, Add;
void min_self(long long &x, long long y) { x = min(x, y); }
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) scanf("%lld", &Arr[i][j]);
  for (int i = n; i >= 1; i--) scanf("%d", &Node[i]);
  for (int t = 1; t <= n; t++) {
    long long Sum = 0;
    int k = Node[t];
    Add.push_back(k);
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++) min_self(Arr[i][j], Arr[i][k] + Arr[k][j]);
    for (auto i : Add)
      for (auto j : Add) Sum += Arr[i][j];
    Ans.push_back(Sum);
  }
  for (int i = n - 1; i >= 0; i--) printf("%lld ", Ans[i]);
  puts("");
  return 0;
}
