CodeforcesAug 10, 2025

Okabe and Future Gadget Laboratory

Hazrat Ali

Codeforces

Okabe needs to renovate the Future Gadget Laboratory after he tried doing some crazy experiments! The lab is represented as an n by n square grid of integers. A good lab is defined as a lab in which every number not equal to 1 can be expressed as the sum of a number in the same row and a number in the same column. In other words, for every x, y such that 1 ≤ x, y ≤ n and ax, y ≠ 1, there should exist two indices s and t so that ax, y = ax, s + at, y, where ai, j denotes the integer in i-th row and j-th column.

Help Okabe determine whether a given lab is good!

Input

The first line of input contains the integer n (1 ≤ n ≤ 50) — the size of the lab.

The next n lines contain n space-separated integers denoting a row of the grid. The j-th integer in the i-th row is ai, j (1 ≤ ai, j ≤ 105).

Output

Print "Yes" if the given lab is good and "No" otherwise.

You can output each letter in upper or lower case.

Examples
Input
3
1 1 2
2 3 1
6 4 1
Output
Yes
Input
3
1 5 2
1 1 1
1 2 3
Output
No

Solution

const fs = require("fs");
const input = fs.readFileSync(0, "utf-8").trim().split("\n");

let n = parseInt(input[0]);
let L = [];
for (let i = 1; i <= n; i++) {
    L.push(input[i].split(" ").map(Number));
}

for (let i = 0; i < n; i++) {
    for (let j = 0; j < n; j++) {
        if (L[i][j] !== 1) {
            let good = false;
            for (let k = 0; k < n; k++) {
                for (let l = 0; l < n; l++) {
                    if (k !== j && l !== i && L[i][k] + L[l][j] === L[i][j]) {
                        good = true;
                        break;
                    }
                }
                if (good) break;
            }
            if (!good) {
                console.log("No");
                process.exit(0);
            }
        }
    }
}

console.log("Yes");





Comments