#include <bits/stdc++.h>
using namespace std;
long long matrix[505][505], result[505];
int removing[505], n;
bool finished[505];
int main() {
  while (scanf("%d", &n) == 1) {
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++) {
        cin >> matrix[i][j];
        result[0] += matrix[i][j];
      }
    for (int i = n; i >= 1; i--) scanf("%d", &removing[i]);
    for (int i = 0; i <= n; i++) finished[i] = false;
    for (int x = 1; x <= n; x++) {
      result[x] = 0;
      int k = removing[x];
      finished[k] = true;
      for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++) {
          matrix[i][j] = min(matrix[i][j], matrix[i][k] + matrix[k][j]);
          if (finished[i] && finished[j]) result[x] += matrix[i][j];
        }
    }
    for (int i = n; i >= 1; i--) cout << result[i] << " ";
    cout << endl;
  }
  return 0;
}
