Bored Dreamoon

아직 제출이 없습니다시간 제한1초메모리 제한512 MB

문제

Dreamoon serves in the military this year. Everything in the military is boring. In order to make military life more interesting, Dreamoon decided to transform some of his military experiences into programming competition problems.

The leaders usually order soldiers to stand in several rows ordered by their heights. The rules of arrangement of soldiers are as follows:

  • If two soldiers AA and BB stand in different rows, and AA's row is in front of BB's row, AA is shorter than BB.
  • If two soldiers AA and BB stand in the same row, and AA is to the right of BB, AA is shorter than BB.
  • For any two rows, the difference between the number of soldiers in them is at most 11.
  • For any two rows, the number of soldiers in the front row is equal to or larger than the number of soldiers in the back row.

After Dreamoon noticed these properties, the following problem came to his mind:

For two different soldiers AA and BB, we say BB is right front of AA if AA's row is NOT in front of the BB's row, and the number of soldiers to the right of BB in BB's row is not larger than the number of soldiers to the right of AA in AA's row.

You don't know how many soldiers there are in total, and you don't know how many rows these soldiers are arranged into. But you have some information about certain NN soldiers, numbered from 11 through NN. You are given the heights of these soldiers. And for any two distinct numbers ii and jj, you know whether soldier jj is right front of ii. Please inspect whether there exists at least one possible configuration satisfying the given information. If possible, you should calculate the minimum number of soldiers in the first row (the row in front of every other row).

입력

The first line of input contains one integer NN indicating that you have information of NN soldiers. The second line of input consists of NN integers h_1,h_2,,h_Nh\_1, h\_2, \ldots, h\_N. Here, h_ih\_i indicates the height of ii-th soldier. Each of the following NN lines contains NN characters. The jj-th character in the ii-th of these lines is '1' if soldier jj is right front of soldier ii; otherwise, it is '0'.

출력

Output one number indicating the minimum possible number of soldiers in the first row (the row in front of every other row). If there is no possible configuration satisfying the given information, output 1-1 instead.

제한

  • 2N1032 \le N \le 10^3
  • 1h_i1091 \le h\_i \le 10^9
  • all h_ih\_i are distinct
  • s\_{ij} =\~'0' or '1'
  • s\_{ii} =\~'0' for all ii in 1,2,,N1, 2, \ldots, N