#include <bits/stdc++.h>   const int MAXN = 1e5 * 5;   const int INF = 1e9 + 7;   using namespace std;   long long f[501][501];   long long a[501][501];   int n, p[501];   long long ans[501];   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> n;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++) cin >> a[i][j];     for (int i = 1; i <= n; i++) cin >> p[i];     for (int t = n; t >= 1; --t) {       int u = p[t];       long long res = 0;       for (int i = t + 1; i <= n; i++) {         int x = p[i];         f[u][x] = a[u][x];         f[x][u] = a[x][u];         for (int j = t + 1; j <= n; j++)           if (i != j) {             int y = p[j];             f[u][x] = min(f[u][x], a[u][y] + f[y][x]);             f[x][u] = min(f[x][u], f[x][y] + a[y][u]);           }       }       for (int i = t + 1; i <= n; i++)         for (int j = t + 1; j <= n; j++) {           int x = p[i], y = p[j];           f[x][y] = min(f[x][y], f[x][u] + f[u][y]);         }       for (int i = t; i <= n; i++)         for (int j = t; j <= n; j++) res += f[p[i]][p[j]];       ans[t] = res;     }     for (int i = 1; i <= n; i++) cout << ans[i] <<    ;     return 0;   }   