Greg has a weighed directed graph, consisting of n vertices. In this graph any pair of distinct vertices has an edge between them in both directions. Greg loves playing with the graph and now he has invented a new game:

  * The game consists of n steps. 
  * On the i-th step Greg removes vertex number xi from the graph. As Greg removes a vertex, he also removes all the edges that go in and out of this vertex. 
  * Before executing each step, Greg wants to know the sum of lengths of the shortest paths between all pairs of the remaining vertices. The shortest path can go through any remaining vertex. In other words, if we assume that d(i, v, u) is the shortest path between vertices v and u in the graph that formed before deleting vertex xi, then Greg wants to know the value of the following sum: <image>. 



Help Greg, print the value of the required sum before each step.

Input

The first line contains integer n (1 ≤ n ≤ 500) — the number of vertices in the graph.

Next n lines contain n integers each — the graph adjacency matrix: the j-th number in the i-th line aij (1 ≤ aij ≤ 105, aii = 0) represents the weight of the edge that goes from vertex i to vertex j.

The next line contains n distinct integers: x1, x2, ..., xn (1 ≤ xi ≤ n) — the vertices that Greg deletes.

Output

Print n integers — the i-th number equals the required sum before the i-th step.

Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams of the %I64d specifier.

Examples

Input

1
0
1


Output

0 

Input

2
0 5
4 0
1 2


Output

9 0 

Input

4
0 3 1 1
6 0 400 1
2 4 0 1
1 1 1 0
4 1 2 3


Output

17 23 404 0 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import static java.lang.Math.*;
import java.util.*;
import java.io.PrintStream;
import java.io.PrintWriter;
public class B {
 
	static final int mod = 1000000007;
	static final int temp = 998244353;
	static final long MOD = 1000000007;
	static final long M = (long)1e9+7;
 
	
	static class Pair implements Comparable<Pair>
	{
		int first, second;
		public Pair(int aa, int bb)
		{
			first = aa; second = bb;
		}
		public int compareTo(Pair p)
		{
			if(first == p.first) return (int)(second - p.second);
			return (int)(first - p.first);
		}
	}
	
	/*
	 * IO FOR 2D GRID IN JAVA
	 * 	char[][] grid = new char[n][m];
			for(int i = 0;i<n;i++)
			{
				char[] s = sc.next().toCharArray();
				for(int j = 0;j<m;j++)
				{
					grid[i][j] = s[j];
				}
			}
	 * */
	
	static class Reader {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");
		String next() {
			while (!st.hasMoreTokens())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}
		
		int nextInt() {
			return Integer.parseInt(next());
		}
		
		int[] readArray(int n) throws IOException {
			int[] a=new int[n];
			for (int i=0; i<n; i++) a[i]=nextInt();
			return a;
		}
		
		long[] longReadArray(int n) throws IOException {
			long[] a=new long[n];
			for (int i=0; i<n; i++) a[i]=nextLong();
			return a;
		}
		
		
		long nextLong() {
			return Long.parseLong(next());
		}
		double nextDouble() {
			return Double.parseDouble(next());
		}
	}
	
 
	public static boolean isPrime(long n) {
		if(n == 1)
		{
			return false;
		}
		for(long i = 2;i*i<=n;i++)
		{
			if(n%i == 0)
			{
				return false;
			}
		}
		return true;
	}
	
	public static List<Integer> Sieve(int n)
	{
		boolean prime[] = new boolean[n+1];
		Arrays.fill(prime, true);
		
		List<Integer> l = new ArrayList<>();
		for (int p=2; p*p<=n; p++) 
		{ 
			if (prime[p] == true) 
			{ 	
			    for(int i=p*p; i<=n; i += p) 
				{
				    prime[i] = false; 
				}	
			} 
		} 
 
		for (int p=2; p<=n; p++) 
		{
		    if (prime[p] == true)
		    {
		       l.add(p); 
		    }
		}
		
		return l;
	}
	
	
	public static int gcd(int a, int b)
	{
		if(b == 0)
		 return a;
		
		else
		return gcd(b,a%b);
	} 
 
	public static int lcm(int a, int b)
    {
        return (a / gcd(a, b)) * b;
    }
	
	public static long LongGCD(long a, long b)
	{
		if(b == 0)
			 return a;
			
			else
			return LongGCD(b,a%b);
	}
	
	public static long LongLCM(long a, long b)
    {
        return (a / LongGCD(a, b)) * b;
    }
	
	public static int phi(int n)  //euler totient function
    { 
        int result = 1; 
        for (int i = 2; i < n; i++) 
            if (gcd(i, n) == 1) 
                result++; 
        return result; 
    }
	
	public static long fastPow(long x, long n) //include mod at each step if asked and in args of fn too
	{
		if(n == 0)
			return 1;
		else if(n%2 == 0)
			return fastPow(x*x,n/2);
		else
			return x*fastPow(x*x,(n-1)/2);
	}
	
	
	   public static long modPow(long x, long y, long p)
	    {
	 
	        long res = 1;
	 
	        x = x % p;
	 
	        while (y > 0) {
	            if (y % 2 == 1)
	                res = (res * x) % p;
	 
	            y = y >> 1; 
	            x = (x * x) % p;
	        }
	 
	        return res;
	    }
	   
	   static long modInverse(long n, long p)
	    {
	        return modPow(n, p - 2, p);
	    }
	 
	    // Returns nCr % p using Fermat's little theorem.
	    
	    public static long nCr(long n, long r,long p)
	    {
	          if (n<r) 
	              return 0;
	          
	        if (r == 0)
	            return 1;
	 
	        long[] fac = new long[(int)(n) + 1];
	        fac[0] = 1;
	 
	        for (int i = 1; i <= n; i++)
	            fac[i] = fac[i - 1] * i % p;
	 
	        return (fac[(int)(n)] * modInverse(fac[(int)(r)], p)
	                % p * modInverse(fac[(int)(n - r)], p)
	                % p)
	            % p;
	    }
	    
		
		public static long[] computePrefix(int arr[], int n)
		{
			long[] prefix = new long[n];
			prefix[0] = arr[0];
			for(int i = 1;i<n;i++)
			{
				prefix[i] = prefix[i-1]+arr[i];
			}
			
			return prefix;
		}
		
	    
	    public static long[] modInvArr(int n, long mod)
	    {
	    	long[] ans = new long[n+1];
			ans[1] = 1;
			for(int i = 2;i <= n;i++)
			{
				ans[i] = (mod - (mod/i) * ans[(int)(mod%i)])%mod;
			}
			return ans;
	    }
 
		public static void Sort(int[] a) {
			List<Integer> l=new ArrayList<>();
			for (int i:a) l.add((i-1));
			//Collections.sort(l);
			Collections.reverse(l);  //Use to Sort decreasingly 
			for (int i=0; i<a.length; i++) a[i]=l.get(i);
		}
		
		public static void ssort(char[] a) {
			List<Character> l=new ArrayList<>();
			for (char ch : a) l.add(ch);
			Collections.sort(l);
			//Collections.reverse(l);  //Use to Sort decreasingly 
			for (int i=0; i<a.length; i++) a[i]=l.get(i);
		}
		
		//Modular Operations for Addition and Multiplication. 
		
		   public static long perfomMod(long x){
		        return ((x%M + M)%M);
		    }
		   public static long addMod(long a, long  b){
		        return perfomMod(perfomMod(a)+perfomMod(b));
		    }
		   public static long mulMod(long  a, long b){
		        return perfomMod(perfomMod(a)*perfomMod(b));
		   }
		
		   public static int LowerBound(int a[], int x) 
		   {
				  int l=-1,r=a.length;
				  while(l+1<r) {
				    int m=(l+r)>>>1;
				    if(a[m]>=x) r=m;
				    else l=m;
				  }
				  return r;
			}
		
		   
			public static int UpperBound(int a[], int x) 
			{
			    int l=-1;
			    int r=a.length;
			    while(l+1<r) {
			       int m=(l+r)>>>1;
			       if(a[m]<=x) l=m;
			       else r=m;
			    }
			    return l+1;
			}
	
		
	public static void main(String[] args) throws IOException 
	{
		Reader sc=new Reader();
		PrintWriter out = new PrintWriter(System.out);
		
		int n = sc.nextInt();
		int[][] grid = new int[n][n];
		for(int i = 0;i < n;i++)
		{
			for(int j = 0;j < n;j++)
			{
				grid[i][j] = sc.nextInt();
			}
		}
		
		
		int[] x = sc.readArray(n);
		
		int[] idx = new int[n];
		
		for(int i = 0;i < n;i++)
		{
			idx[x[n-1-i]-1] = i;
		}
		
 
		int[][] dp = new int[n][n];
		for(int i = 0;i < n;i++)
		{
			for(int j = 0;j < n;j++)
			{
				dp[idx[i]][idx[j]] = grid[i][j];
			}
		}
		
		List<Long> list = new ArrayList<>();
 
		for(int k = 0;k < n;k++)
		{
			for(int i = 0;i < n;i++)
			{
				for(int j = 0;j < n;j++)
				{
					dp[i][j] = min(dp[i][j],dp[i][k] + dp[k][j]);
				}
			}
			
			long sum = 0L;
			for(int i = 0;i <= k;i++)
			{
				for(int j = 0;j <= k;j++)
				{
					sum += dp[i][j];
				}
			}
			list.add(sum);
		}
		
		Collections.reverse(list);
		for(long it : list)
		{
			System.out.print(it + " ");
		}
		System.out.println();
		
		out.close();
		
	}
	
}