#include <bits/stdc++.h>
using namespace std;
int INP, AM, REACHEOF;
char BUF[(1 << 12) + 1], *inp = BUF;
const long double PI = acos((long double)-1.0);
int c[511][511], f[511][511];
int n, a[511];
long long save[511];
int main() {
  while (scanf("%d", &n) == 1) {
    for (int i = (1); i <= (n); i++)
      for (int j = (1); j <= (n); j++) scanf("%d", &c[i][j]);
    for (int i = (1); i <= (n); i++) scanf("%d", &a[i]);
    for (int t = n; t >= 1; --t) {
      long long res = 0;
      int u = a[t];
      for (int i = (t + 1); i <= (n); i++) {
        int x = a[i];
        f[u][x] = c[u][x];
        f[x][u] = c[x][u];
        for (int j = (t + 1); j <= (n); j++)
          if (j != i) {
            int y = a[j];
            f[u][x] = min(f[u][x], c[u][y] + f[y][x]);
            f[x][u] = min(f[x][u], f[x][y] + c[y][u]);
          }
      }
      for (int i = (t + 1); i <= (n); i++)
        for (int j = (t + 1); j <= (n); j++) {
          int x = a[i], y = a[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[a[i]][a[j]];
      save[t] = res;
    }
    for (int i = (1); i <= (n); i++) cout << save[i] << ' ';
    cout << endl;
  }
  return 0;
}
