import java.util.Scanner;
public class Main {
public static void main
(String[] args
) { Scanner scanner
= new Scanner
(System.
in); int n = scanner.nextInt();
int[][] connectivityMatrix = new int[n][n];
int[] maintenanceCosts = new int[n];
scanner.nextLine();
for (int i = 0; i < n; i++) {
String line
= scanner.
nextLine(); for (int j = 0; j < n; j++) {
connectivityMatrix[i][j] = line.charAt(j) - '0';
}
}
for (int i = 0; i < n; i++) {
maintenanceCosts[i] = scanner.nextInt();
}
scanner.close();
long totalCost = 0;
for (int i = 0; i < n; i++) {
if (isSuperTunnel(connectivityMatrix, i)) {
totalCost += maintenanceCosts[i];
}
}
System.
out.
println(totalCost
); }
private static boolean isSuperTunnel(int[][] connectivityMatrix, int tunnel) {
int n = connectivityMatrix.length;
for (int i = 0; i < n; i++) {
if (i != tunnel && connectivityMatrix[tunnel][i] == 1) {
for (int j = 0; j < n; j++) {
if (j != tunnel && j != i && connectivityMatrix[tunnel][j] == 1 && connectivityMatrix[i][j] == 0) {
return false;
}
}
}
}
return true;
}
}
aW1wb3J0IGphdmEudXRpbC5TY2FubmVyOwoKcHVibGljIGNsYXNzIE1haW4gewoKICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpIHsKICAgICAgICBTY2FubmVyIHNjYW5uZXIgPSBuZXcgU2Nhbm5lcihTeXN0ZW0uaW4pOwogICAgICAgIGludCBuID0gc2Nhbm5lci5uZXh0SW50KCk7CiAgICAgICAgaW50W11bXSBjb25uZWN0aXZpdHlNYXRyaXggPSBuZXcgaW50W25dW25dOwogICAgICAgIGludFtdIG1haW50ZW5hbmNlQ29zdHMgPSBuZXcgaW50W25dOwogICAgICAgIHNjYW5uZXIubmV4dExpbmUoKTsKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IG47IGkrKykgewogICAgICAgICAgICBTdHJpbmcgbGluZSA9IHNjYW5uZXIubmV4dExpbmUoKTsKICAgICAgICAgICAgZm9yIChpbnQgaiA9IDA7IGogPCBuOyBqKyspIHsKICAgICAgICAgICAgICAgIGNvbm5lY3Rpdml0eU1hdHJpeFtpXVtqXSA9IGxpbmUuY2hhckF0KGopIC0gJzAnOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgICAgIG1haW50ZW5hbmNlQ29zdHNbaV0gPSBzY2FubmVyLm5leHRJbnQoKTsKICAgICAgICB9CiAgICAgICAgc2Nhbm5lci5jbG9zZSgpOwogICAgICAgIGxvbmcgdG90YWxDb3N0ID0gMDsKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IG47IGkrKykgewogICAgICAgICAgICBpZiAoaXNTdXBlclR1bm5lbChjb25uZWN0aXZpdHlNYXRyaXgsIGkpKSB7CiAgICAgICAgICAgICAgICB0b3RhbENvc3QgKz0gbWFpbnRlbmFuY2VDb3N0c1tpXTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4odG90YWxDb3N0KTsKICAgIH0KCiAgICBwcml2YXRlIHN0YXRpYyBib29sZWFuIGlzU3VwZXJUdW5uZWwoaW50W11bXSBjb25uZWN0aXZpdHlNYXRyaXgsIGludCB0dW5uZWwpIHsKICAgICAgICBpbnQgbiA9IGNvbm5lY3Rpdml0eU1hdHJpeC5sZW5ndGg7CiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyBpKyspIHsKICAgICAgICAgICAgaWYgKGkgIT0gdHVubmVsICYmIGNvbm5lY3Rpdml0eU1hdHJpeFt0dW5uZWxdW2ldID09IDEpIHsKICAgICAgICAgICAgICAgIGZvciAoaW50IGogPSAwOyBqIDwgbjsgaisrKSB7CiAgICAgICAgICAgICAgICAgICAgaWYgKGogIT0gdHVubmVsICYmIGogIT0gaSAmJiBjb25uZWN0aXZpdHlNYXRyaXhbdHVubmVsXVtqXSA9PSAxICYmIGNvbm5lY3Rpdml0eU1hdHJpeFtpXVtqXSA9PSAwKSB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