#include <bits/stdc++.h>
using namespace std;
const long long lmax = 999999999;
long long fix[506], n, m[503][503], d[503][503], x[503], i, j, k, p, ans;
vector<long long> ANS;
int main() {
  cin >> n;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++) cin >> m[i][j], d[i][j] = m[i][j];
  ;
  for (i = 1; i <= n; i++) cin >> x[i];
  reverse(x + 1, x + n + 1);
  for (k = 1; k <= n; k++) {
    p = x[k];
    fix[p] = 1;
    for (i = 1; i <= n; i++)
      for (j = 1; j <= n; j++)
        if (i != j)
          if (d[i][p] + d[p][j] < d[i][j]) {
            d[i][j] = d[i][p] + d[p][j];
          }
    for (i = 1; i <= n; i++)
      for (j = 1; j <= n; j++) {
        if (fix[i] && fix[j]) ans += d[i][j];
        ;
      }
    ANS.push_back(ans);
    ans = 0;
  }
  for (i = ANS.size() - 1; i >= 0; i--) cout << ANS[i] << " ";
}
