#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  long long d[n + 1][n + 1];
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j) cin >> d[i][j];
  vector<int> v;
  vector<long long> sol;
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    v.push_back(x);
  }
  reverse(v.begin(), v.end());
  long long sum;
  for (int r = 0; r < n; ++r) {
    int k = v[r];
    for (int i = 1; i <= n; ++i)
      for (int j = 1; j <= n; ++j) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
    sum = 0;
    for (int i = 0; i <= r; ++i)
      for (int j = 0; j <= r; ++j) sum += d[v[i]][v[j]];
    sol.push_back(sum);
  }
  for (int i = sol.size() - 1; i >= 0; --i) cout << sol[i] << " ";
  return 0;
}
