#include <bits/stdc++.h>
using namespace std;
typedef long long int LL;
typedef unsigned long long int uLL;
inline int _Int() { int x; scanf("%d",&x); return x; }
LL bigMod(LL A,LL P,int M){ LL R=1; for(A%=M; P; P>>=1) { if(P&1) R=(R*A)%M; A=(A*A)%M; } return R; } /** (A^P) % M **/
LL bigMul(LL A,LL B,LL M) { LL R=0; for(A%=M; B; B>>=1) { if(B&1) R=(R+A)%M; A=(A+A)%M; } return R; } /** (A*B) % M **/
LL negMod(LL A,LL B) { return ( ( ( A % B ) + B) % B ); } /** (A % B) when A is negative or positive **/
LL invMod(LL A,LL M) { return bigMod( A , M-2, M ); } /** (A^(-1)) % M */
uLL _pow(uLL A,int P) { uLL R=1; for(; P; P>>=1) { if(P&1) R=(R*A); A=(A*A); } return R; } /** (A^P) **/
template<class T>T GCD(T x, T y) { while(x) x^=y^=x^=y%=x; return y; } /** Greatest Common Divisor( a , b ) **/
template<class T> bool inRng( T u, T v, T x ) { return u<=x && x<=v; } /** check ( u <= x <=v ) */
#define myMemset(a,b) memset(a,b,sizeof(a))
#define pi acos(-1)
#define pb push_back
#define myDebug(x) cout<<#x<<" : "<<x<<"\n"
/*************************************************************************************************************************
** Syed Zafrul Lipu (ShockProof) *
** CSE, University of Asia Pacific *
**************************************************************************************************************************/
const int M = 500 + 7;
const int inf = 1000000;
int inp[M][M];
int G[M][M];
bool present[M];
void Main()
{
int n = _Int();
for(int i = 0; i < n; i ++ ) {
for(int j = 0; j < n ; j ++ ) {
G[i][j] = inf;
inp[i][j] = _Int();
}
}
stack<int> st, Ans;
for(int i = 0; i < n ; i ++ ) {
st.push( _Int() );
}
while( st.size() ) {
int x = st.top()-1;st.pop();
present[ x ] = 1;
for(int i = 0; i < n; i ++ ) {
if( !present[i] ) continue;
G[i][i] = 0;
G[i][x] = inp[i][x];
G[x][i] = inp[x][i];
}
int now = 0;
for(int i = 0; i < n; i ++ ) {
for(int j = 0; j < n; j ++ ) {
G[i][j] = min( G[i][j] , G[i][x]+G[x][j] );
if( G[i][j] < inf ) now += G[i][j];
}
}
Ans.push( now );
}
while( Ans.size() ) {
printf("%d ", Ans.top() );
Ans.pop();
}
putchar(10);
}
int main()
{
Main();
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnR5cGVkZWYgbG9uZyBsb25nIGludCBMTDsKdHlwZWRlZiB1bnNpZ25lZCBsb25nIGxvbmcgaW50IHVMTDsKaW5saW5lIGludCBfSW50KCkgeyBpbnQgeDsgc2NhbmYoIiVkIiwmeCk7IHJldHVybiB4OyB9CkxMIGJpZ01vZChMTCBBLExMIFAsaW50IE0peyBMTCBSPTE7IGZvcihBJT1NOyBQOyBQPj49MSkgeyBpZihQJjEpIFI9KFIqQSklTTsgQT0oQSpBKSVNOyB9IHJldHVybiBSOyB9IC8qKiAoQV5QKSAlIE0gKiovCkxMIGJpZ011bChMTCBBLExMIEIsTEwgTSkgeyBMTCBSPTA7IGZvcihBJT1NOyBCOyBCPj49MSkgeyBpZihCJjEpIFI9KFIrQSklTTsgQT0oQStBKSVNOyB9IHJldHVybiBSOyB9IC8qKiAoQSpCKSAlIE0gKiovCkxMIG5lZ01vZChMTCBBLExMIEIpIHsgcmV0dXJuICggKCAoIEEgJSBCICkgKyBCKSAlIEIgKTsgfSAvKiogKEEgJSBCKSB3aGVuIEEgaXMgbmVnYXRpdmUgb3IgcG9zaXRpdmUgKiovCkxMIGludk1vZChMTCBBLExMIE0pIHsgcmV0dXJuIGJpZ01vZCggQSAsIE0tMiwgTSApOyB9IC8qKiAoQV4oLTEpKSAlIE0gKi8KdUxMIF9wb3codUxMIEEsaW50IFApIHsgdUxMIFI9MTsgZm9yKDsgUDsgUD4+PTEpIHsgaWYoUCYxKSBSPShSKkEpOyBBPShBKkEpOyB9IHJldHVybiBSOyB9IC8qKiAoQV5QKSAqKi8KdGVtcGxhdGU8Y2xhc3MgVD5UIEdDRChUIHgsIFQgeSkgeyB3aGlsZSh4KSB4Xj15Xj14Xj15JT14OyByZXR1cm4geTsgfSAvKiogR3JlYXRlc3QgQ29tbW9uIERpdmlzb3IoIGEgLCBiICkgKiovCnRlbXBsYXRlPGNsYXNzIFQ+IGJvb2wgaW5SbmcoIFQgdSwgVCB2LCBUIHggKSB7IHJldHVybiB1PD14ICYmIHg8PXY7IH0gLyoqIGNoZWNrICggdSA8PSB4IDw9diApICovCiNkZWZpbmUgbXlNZW1zZXQoYSxiKSBtZW1zZXQoYSxiLHNpemVvZihhKSkKI2RlZmluZSBwaSAgICAgICAgICAgIGFjb3MoLTEpCiNkZWZpbmUgcGIgICAgICAgICAgICBwdXNoX2JhY2sKI2RlZmluZSBteURlYnVnKHgpICAgIGNvdXQ8PCN4PDwiIDogIjw8eDw8IlxuIgovKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKgoqKiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgU3llZCBaYWZydWwgTGlwdSAoU2hvY2tQcm9vZikgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgKgoqKiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgQ1NFLCBVbml2ZXJzaXR5IG9mIEFzaWEgUGFjaWZpYyAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgKgoqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKi8KCmNvbnN0IGludCBNID0gNTAwICsgNzsKY29uc3QgaW50IGluZiA9IDEwMDAwMDA7CgppbnQgaW5wW01dW01dOwppbnQgR1tNXVtNXTsKCmJvb2wgcHJlc2VudFtNXTsKCnZvaWQgTWFpbigpCnsKICAgICAgICBpbnQgbiA9IF9JbnQoKTsKICAgICAgICBmb3IoaW50IGkgPSAwOyBpIDwgbjsgaSArKyApIHsKICAgICAgICAgICAgICAgIGZvcihpbnQgaiA9IDA7IGogPCBuIDsgIGogKysgKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIEdbaV1bal0gPSBpbmY7CiAgICAgICAgICAgICAgICAgICAgICAgIGlucFtpXVtqXSA9IF9JbnQoKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgc3RhY2s8aW50PiBzdCwgQW5zOwogICAgICAgIGZvcihpbnQgaSA9IDA7IGkgPCBuIDsgaSArKyApIHsKICAgICAgICAgICAgICAgIHN0LnB1c2goIF9JbnQoKSApOwogICAgICAgIH0KCiAgICAgICAgd2hpbGUoIHN0LnNpemUoKSApIHsKCiAgICAgICAgICAgICAgICBpbnQgeCA9IHN0LnRvcCgpLTE7c3QucG9wKCk7CiAgICAgICAgICAgICAgICBwcmVzZW50WyB4IF0gPSAxOwoKICAgICAgICAgICAgICAgIGZvcihpbnQgaSA9IDA7IGkgPCBuOyBpICsrICkgewogICAgICAgICAgICAgICAgICAgICAgICBpZiggIXByZXNlbnRbaV0gKSBjb250aW51ZTsKICAgICAgICAgICAgICAgICAgICAgICAgR1tpXVtpXSA9IDA7CiAgICAgICAgICAgICAgICAgICAgICAgIEdbaV1beF0gPSBpbnBbaV1beF07CiAgICAgICAgICAgICAgICAgICAgICAgIEdbeF1baV0gPSBpbnBbeF1baV07CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpbnQgbm93ID0gMDsKICAgICAgICAgICAgICAgIGZvcihpbnQgaSA9IDA7IGkgPCBuOyBpICsrICkgewogICAgICAgICAgICAgICAgICAgICAgICBmb3IoaW50IGogPSAwOyBqIDwgbjsgaiArKyApIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBHW2ldW2pdID0gbWluKCBHW2ldW2pdICwgR1tpXVt4XStHW3hdW2pdICk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYoIEdbaV1bal0gPCBpbmYgKSBub3cgKz0gR1tpXVtqXTsKICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgQW5zLnB1c2goIG5vdyApOwogICAgICAgIH0KICAgICAgICB3aGlsZSggQW5zLnNpemUoKSApIHsKICAgICAgICAgICAgICAgIHByaW50ZigiJWQgIiwgQW5zLnRvcCgpICk7CiAgICAgICAgICAgICAgICBBbnMucG9wKCk7CiAgICAgICAgfQogICAgICAgIHB1dGNoYXIoMTApOwp9CgppbnQgbWFpbigpCnsKICAgICAgICBNYWluKCk7CiAgICAgICAgcmV0dXJuIDA7Cn0=