#include <bits/stdc++.h>
using namespace std;
const long long INFLL = 2 * (long long)1e18 + 100;
const int INFINT = 2 * (int)1e9 + 100;
void die() {
  cout << "-1";
  exit(0);
}
const int NMAX = 500 + 5;
const int MOD = 1e9 + 7;
const double PI = atan(1) * 4;
const double EPS = 1e-12;
int n;
int ad[NMAX][NMAX];
vector<int> erased;
vector<long long> sol;
bool valid[NMAX];
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      cin >> ad[i][j];
      if (ad[i][j] == 0 && i != j) ad[i][j] = INFINT;
    }
  for (int i = 1; i <= n; i++) {
    int a;
    cin >> a;
    erased.push_back(a);
  }
  reverse(erased.begin(), erased.end());
  for (auto k : erased) {
    valid[k] = 1;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        ad[i][j] = min(ad[i][j], ad[i][k] + ad[k][j]);
    long long sum = 0;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        if (valid[i] && valid[j]) sum += ad[i][j];
    sol.push_back(sum);
  }
  reverse(sol.begin(), sol.end());
  for (auto i : sol) cout << i << ' ';
  return 0;
}
