#include <bits/stdc++.h>
using namespace std;
const int maxn = 505;
int n, g[maxn][maxn], a[maxn];
long long ans[maxn];
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) cin >> g[i][j];
  for (int i = n; i >= 1; i--) cin >> a[i];
  ans[1] = 0;
  for (int i = 2; i <= n; i++) {
    int aa = a[i];
    ans[i] += ans[i - 1];
    for (int j = 1; j < i; j++) ans[i] += g[a[i]][a[j]] + g[a[j]][a[i]];
    for (int j = 1; j < i; j++)
      for (int k = 1; k < i; k++) {
        if (g[a[j]][a[i]] > g[a[j]][a[k]] + g[a[k]][a[i]]) {
          ans[i] -= g[a[j]][a[i]];
          ans[i] += g[a[j]][a[k]] + g[a[k]][a[i]];
          g[a[j]][a[i]] = g[a[j]][a[k]] + g[a[k]][a[i]];
        }
        if (g[a[i]][a[j]] > g[a[i]][a[k]] + g[a[k]][a[j]]) {
          ans[i] -= g[a[i]][a[j]];
          ans[i] += g[a[i]][a[k]] + g[a[k]][a[j]];
          g[a[i]][a[j]] = g[a[i]][a[k]] + g[a[k]][a[j]];
        }
      }
    for (int j = 1; j < i; j++)
      for (int k = 1; k < i; k++) {
        if (g[a[j]][a[k]] > g[a[j]][a[i]] + g[a[i]][a[k]]) {
          ans[i] -= g[a[j]][a[k]];
          ans[i] += g[a[j]][a[i]] + g[a[i]][a[k]];
          g[a[j]][a[k]] = g[a[j]][a[i]] + g[a[i]][a[k]];
        }
      }
  }
  cout << ans[n];
  for (int i = n - 1; i >= 1; i--) cout << " " << ans[i];
  cout << endl;
  return 0;
}
