Under construction

Still being built. Things may change

Path Finding

Interview

Time limit1sMemory limit256 MB

Summary
Decide for every ordered pair of vertices in a directed graph with up to 100 vertices whether a path of at least one edge connects them.
Level

Easy3 of 10

Topics
Graph, Dynamic programming
Solved
No attempts yet

Problem

You are given an unweighted directed graph GG. Write a program that decides, for every pair of vertices (i,j)(i, j), whether a path of positive length runs from ii to jj.

A path of positive length uses at least one edge. So the answer for (i,i)(i, i) is 1 only when some path leaves ii and comes back to ii.

Input

The first line contains the number of vertices NN (1≤N≤1001 \le N \le 100).

Each of the next NN lines contains one row of the adjacency matrix. If the jj-th number on the ii-th line is 1, an edge goes from ii to jj; if it is 0, no such edge exists. The ii-th number on the ii-th line is always 0.

Output

Print the answer as an adjacency matrix over NN lines. If a path of positive length runs from vertex ii to vertex jj, print 1 as the jj-th number on the ii-th line, otherwise print 0. Separate the numbers on a line with a single space.

Examples2

  1. Example 1

    Input
    3
    0 1 0
    0 0 1
    1 0 0
    
    Expected output
    1 1 1
    1 1 1
    1 1 1
    
  2. Example 2

    Input
    7
    0 0 0 1 0 0 0
    0 0 0 0 0 0 1
    0 0 0 0 0 0 0
    0 0 0 0 1 1 0
    1 0 0 0 0 0 0
    0 0 0 0 0 0 1
    0 0 1 0 0 0 0
    
    Expected output
    1 0 1 1 1 1 1
    0 0 1 0 0 0 1
    0 0 0 0 0 0 0
    1 0 1 1 1 1 1
    1 0 1 1 1 1 1
    0 0 1 0 0 0 1
    0 0 1 0 0 0 0