Recently Polycarpus has learned the "bitwise AND" operation (which is also called "AND") of non-negative integers. Now he wants to demonstrate the school IT teacher his superb manipulation with the learned operation.
For that Polycarpus came to school a little earlier and wrote on the board a sequence of non-negative integers a1, a2, ..., an. He also wrote a square matrix b of size n × n. The element of matrix b that sits in the i-th row in the j-th column (we'll denote it as bij) equals:
the "bitwise AND" of numbers ai and aj (that is, bij = ai & aj), if i ≠ j;
-1, if i = j.
Having written out matrix b, Polycarpus got very happy and wiped a off the blackboard. But the thing is, the teacher will want this sequence to check whether Polycarpus' calculations were correct. Polycarus urgently needs to restore the removed sequence of integers, or else he won't prove that he can count correctly.
Help Polycarpus, given matrix b, restore the sequence of numbers a1, a2, ..., an, that he has removed from the board. Polycarpus doesn't like large numbers, so any number in the restored sequence mustn't exceed 109.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the size of square matrix b. Next n lines contain matrix b. The i-th of these lines contains n space-separated integers: the j-th number represents the element of matrix bij. It is guaranteed, that for all i (1 ≤ i ≤ n) the following condition fulfills: bii = -1. It is guaranteed that for all i, j (1 ≤ i, j ≤ n; i ≠ j) the following condition fulfills: 0 ≤ bij ≤ 109, bij = bji.
Output
Print n non-negative integers a1, a2, ..., an (0 ≤ ai ≤ 109) — the sequence that Polycarpus wiped off the board. Separate the numbers by whitespaces.
It is guaranteed that there is sequence a that satisfies the problem conditions. If there are multiple such sequences, you are allowed to print any of them.
We need to reconstruct sequence a of length n from matrix b, where b_ij = a_i & a_j for i≠j, and b_ii = -1. Input constraints: n up to 100; b entries for i≠j are between 0 and 1e9; symmetric; guarantee existence.
We need to output any a satisfying conditions.
Let's think.
For each pair (i,j), we have a_i & a_j = b_ij.
Thus b_ij is a lower bound on a_i and a_j: since bitwise AND gives bits that are set in both numbers, b_ij must have bits that are subsets of bits in a_i and a_j.
In fact, for each bit position k, we have: if b_ij has bit k set to 1, then both a_i and a_j must have that bit as 1. Conversely, if b_ij has bit k = 0, then at least one of a_i or a_j has bit 0. So the constraints are: for each k, the set of indices i such that a_i has bit k = 1 must be a subset of the condition that for any i,j in that set, b_ij must have bit k=1 (since a_i & a_j would have bit 1). Actually, if a_i has bit k=1 and a_j has bit k=1 then b_ij must have that bit 1. And if for some i,j, b_ij has bit 0, then not both can have bit 1 (i.e., at least one has 0).
Thus, given b, we can infer for each bit k which indices can have bit 1.
Given that a_i must be <= 1e9, which is up to 30 bits.
We can attempt to reconstruct a_i as the bitwise OR of all b_ij for j != i? Not exactly: For each i, consider that a_i must contain all bits that appear in any b_ij for that i? Since a_i & a_j = b_ij, so b_ij is the intersection of bits of a_i and a_j. So a_i must have at least all bits that appear in b_ij for any j. But careful: b_ij bits are subset of a_i. So for each i, a_i must be a superset of the union over j of b_ij? Actually, for a fixed i, for any j, b_ij = a_i & a_j. So every bit set in b_ij must be set in a_i. So indeed, for each i, a_i must have all bits that appear in any b_ij (j != i). So we can set a_i = OR over j of b_ij? But OR over j of b_ij would be the union of bits that appear in any b_ij. Since each bit in b_ij is in a_i, then the OR over j of b_ij is a subset of a_i (bitwise). However, a_i may have additional bits that never appear in any b_ij because for all other a_j, that bit is not set, thus a_i & a_j does not have it. But if a_i has a bit that is not present in any other a_j, then for all j, b_ij would have that bit 0. So that bit could be present in a_i but not forced by any b_ij. But could such bits be determined? Possibly multiple solutions exist. But does setting a_i = OR of b_ij always satisfy all b_ij? Let's test with examples:
Compute OR for each:
i=1: 128|128|128 = 128 -> a1=128
i=2: 128|148|160 = (128|148) = 148? Actually 128 decimal = 10000000 binary (bit7). 148 = 10010100? Let's compute: 128 (2^7) and 148 = 128+16+4 = 10010100 binary. 160=128+32 = 10100000. So OR = 128|148|160 = 128|148 = 148, then 148|160 = 148+? 160 has bits 7 (128) and 5 (32). 148 has bits 7,4(16),2(4). OR => bits 7,5,4,2 => 128+32+16+4=180. So a2=180. i=3: 128|148|128 = 128|148 = 148? Actually 128|148 = 148, and 148|128 = 148, so a3=148. i=4: 128|160|128 = 128|160=160, so a4=160. Output in example: 128 180 148 160. Indeed matches: a1=128, a2=180, a3=148, a4=160.
So this works for these examples.
But is it always valid? Let's test other scenarios.
We need to verify: Given b derived from some unknown a*, if we define a_i = OR_{j != i} b_ij, will it satisfy that for all i,j, a_i & a_j = b_ij? Possibly yes, given that b_ij = a_i* & a_j*, and we compute OR of b_ij over j. Does that equal a_i*? Not necessarily; but maybe it's a valid solution.
We need to see if for any i, a_i defined as OR over j of b_ij is such that a_i & a_j = b_ij. Let's denote A_i = OR_{k != i} b_ik. Since b_ik = a_i* & a_k*, and a_i* is fixed, b_ik ⊆ a_i* (bitwise). So OR_{k} b_ik ⊆ a_i*. So A_i ≤ a_i* (bitwise). However, could A_i be strictly smaller than a_i*? Possibly if there are bits in a_i* that are never present in any a_k* for k≠i. Since then for all k, a_i* & a_k* has that bit 0, so b_ik doesn't have that bit. So A_i would miss those bits. So A_i would be a_i* with those private bits removed. Then would A_i & A_j still equal b_ij? If we remove private bits from both, maybe b_ij stays same because private bits were not shared anyway. Indeed, if we define a_i' = a_i* minus bits that are not in any other a_k, then for any pair i,j, a_i' & a_j' = (a_i* & a_j*) because the removed bits were not common. Let's reason: For a particular bit position, if it is set in a_i* and a_j*, then it must be set in b_ij, so it's present in both. But also, if it's set in a_i* but not in a_j*, then it's not in b_ij. However, could it be that it is set in a_i* but not in any other a_k*? That means it's absent in all a_j*, j≠i. So for any j, b_ij doesn't have that bit. So if we remove it from a_i', then for any pair i,j, the AND remains unchanged because it was 0 in a_j anyway. Similarly, if it's set in both i and j, then it must be present in b_ij, and therefore also present in OR_{k} b_ik? Actually, if it's set in both i and j, then b_ij has that bit, so it's included in A_i (since OR includes b_ij) and also in A_j. So that bit remains. So indeed, A_i is a_i* with private bits removed, while shared bits are kept. So then for any i,j, A_i & A_j = a_i* & a_j* = b_ij. So the A_i defined as OR of b_ik indeed satisfy the condition! So it's a valid solution.
But need to be careful: Could it be that some bit is set in a_i* and a_j* but b_ij is missing that bit due to some error? No, by definition b_ij = a_i* & a_j*, so if bit set in both, then b_ij has it. So OR includes it. So A_i will contain that bit. Good.
Thus the construction works for any underlying a* that produced b. But we must also check that A_i computed from b doesn't exceed 1e9? Since b entries ≤1e9, OR of up to 99 numbers each ≤1e9 is still ≤ (2^30-1) approx 1e9. So fine.
But is it always true that OR_{j≠i} b_ij = a_i with private bits removed? Yes, as argued. So solution: For each i, compute bitwise OR over all b_ij (j≠i). Output these numbers. That's it? Let's test with some edge cases.
Edge case n=1: matrix has only -1. There are no j≠i. OR over empty set? Need to define. For n=1, there is no constraint, any a1 works? Actually b only contains -1, no constraints. The problem says output any sequence a. Our construction would need to compute OR of zero numbers. Typically, OR over empty set is 0. So output 0. That matches example 1. So we can output 0.
But need to be careful: Is there any scenario where the OR construction yields a number that doesn't satisfy b_ij? Let's try a counterexample. Suppose we have a_i* = 5 (101) and a_j* = 3 (011) => b_ij = 1 (001). For i, OR_{j} b_ij = 1, so A_i = 1. a_i* = 5, private bits: bit 2 (value 4) not present in j, removed. So A_i = 1, A_j =? OR_{i} b_ji = 1 (since only one other?). So A_j = 1. Then A_i & A_j = 1, matches b_ij. So good.
Now suppose three numbers: a1=6 (110), a2=4 (100), a3=2 (010). Then:
b12 = 4 (100), b13 = 2 (010), b23 = 0 (000). Compute OR for each:
A1 = b12|b13 = 100|010=110 (6) -> includes shared bits? Actually a1 had 110; both bits 2 and 1 are shared with at least one other? bit 2 (value 4) is shared with a2; bit 1 (value 2) shared with a3. So A1 =6, same as a1.
A2 = b21|b23 = 100|0 =4, same as a2.
A3 = b31|b32 = 010|0=2, same as a3.
Works.
Now consider a scenario where a_i has a bit that is shared with some but not all others; still OR includes it from that particular j, so okay.
Thus the simple OR solution seems always valid given that b is derived from some a*.
But is the problem guarantee that b is exactly derived from some a*? Yes, "It is guaranteed that there is sequence a that satisfies the problem conditions." So b is consistent. Therefore, we can always take a_i = OR_{j≠i} b_ij. This yields a valid a (maybe not the original a*, but still satisfies conditions). So answer is straightforward.
But wait, need to verify: Is there any subtlety about bits that appear only in diagonal? No, diagonal is -1.
Thus the solution: For each i, compute bitwise OR of all b_ij for j from 1 to n, ignoring -1 on diagonal. Print these numbers.
Complexity O(n^2) which is fine.
But let's double-check with more tricky cases: Possibly there exist a_i that are 0? Works.
Now, we should also consider that b_ij may be up to 1e9, but OR works with integers.
Edge: n=2, b = [-1, x; x, -1]. Then a1 = x, a2 = x. Check: a1 & a2 = x. So works.
Now, what if b_ij entries are such that OR for i yields a number that when & with OR for j yields something larger than b_ij? Could that happen? Let's test: Suppose b from some unknown a* yields OR_i as we define, then we argued A_i ≤ a_i* (bitwise). So A_i & A_j ≤ a_i* & a_j* = b_ij? Wait, if A_i is subset of a_i* (bitswise), then A_i & A_j ≤ a_i* & a_j* = b_ij. But we need equality: A_i & A_j must equal b_ij. Since b_ij is exactly a_i* & a_j*. However A_i & A_j may be less than b_ij if we removed bits that were present in both a_i* and a_j* but somehow missing from A_i or A_j. But as argued, if a bit is present in both a_i* and a_j*, then it's in b_ij, so OR of b_ik includes b_ij, so that bit is set in A_i. And similarly for A_j. So both contain that bit. So A_i & A_j includes that bit. So indeed A_i & A_j = a_i* & a_j* = b_ij. So equality holds.
Thus proof: For each bit k, consider whether it is set in b_ij. If it is set, then it must be set in A_i and A_j, so it's set in AND. Conversely, if it is set in A_i & A_j, then it is set in A_i and A_j. A_i = OR_{l} b_il, so if bit set in A_i, there exists some l such that b_il has that bit set. That means that in a_i* & a_l*, that bit is set, so a_i* has it, and a_l* has it. But does that imply that b_ij has it? Not necessarily; b_ij might not have it if a_j* doesn't have that bit. However if bit is set in both A_i and A_j, we have: A_i includes bit because ∃ l with b_il having bit; similarly ∃ m with b_jm having bit. This means a_i* has bit and a_l* has bit; a_j* has bit and a_m* has bit. But does that guarantee that a_i* & a_j* has the bit? Since a_i* and a_j* both have the bit, yes because both have it. But wait: a_i* might have the bit, a_j* might have the bit, so the AND of a_i* and a_j* includes that bit, so b_ij should have it. But is it guaranteed that b_ij includes it? Yes, because b_ij = a_i* & a_j*. So b_ij would have that bit. However, is it possible that b_ij does not have that bit due to input inconsistency? But we assume b is consistent with some a*, so it must. So indeed A_i & A_j ⊆ b_ij. And we also have b_ij ⊆ A_i & A_j (since b_ij is included in ORs). So they are equal.
Thus the construction is valid.
So the problem is trivial? That seems too easy. But let's verify with more complex constraints: n up to 100, b given. There is no trap. So solution: For i from 1 to n: a_i = 0; for j from 1 to n: if i != j then a_i |= b_ij. Print a_i.
But wait: The input matrix is symmetric and diagonal is -1. So we just compute OR.
But need to ensure that a_i does not exceed 1e9? The OR of up to 99 numbers each ≤ 1e9 may exceed 1e9? 1e9 is less than 2^30 ≈ 1.07e9. Actually 2^30 = 1073741824, which is > 1e9. The constraint says any number in the restored sequence mustn't exceed 10^9. However b_ij can be up to 10^9, but if we OR many numbers, the result might exceed 10^9? Let's check: 10^9 in binary is about 111011100110101100101000000000? Actually 10^9 = 1,000,000,000. The maximum number less than or equal to 1e9 is 1e9 itself. But the OR of several numbers each ≤1e9 could potentially produce a number >1e9 if they have bits beyond the 1e9 range? However 1e9 < 2^30. The largest 30-bit number (2^30-1) is 1073741823, which is > 1e9. So it's possible that OR of numbers each ≤1e9 yields a number up to (2^30-1) which is about 1.07e9, which exceeds 1e9. The problem says any number in the restored sequence mustn't exceed 10^9. So we need to output numbers ≤ 1e9. Is it guaranteed that there exists a solution with numbers ≤ 1e9? Yes, the problem statement: "Polycarpus doesn't like large numbers, so any number in the restored sequence mustn't exceed 10^9." Actually that's a requirement on output; we must produce numbers ≤ 1e9. But they guarantee that there exists a sequence a that satisfies the conditions (the matrix b) and also a_i ≤ 10^9. Since b is derived from such a sequence, our constructed a_i as OR of b_ij may exceed 1e9? But does it? If original a_i were ≤1e9, then b_ij = a_i & a_j ≤ a_i ≤ 1e9. So b_ij ≤1e9. OR of numbers ≤1e9 could be up to something maybe > 1e9 if the original a_i used bits up to 2^29 (512M) but combined could produce number with bit 30 set? Actually if any b_ij has bit 29, that means both a_i and a_j had bit 29 set, so a_i might have had bit 29, and maybe also bit 30? But a_i ≤ 1e9, so the 30th bit (2^29 is 536,870,912; 2^30 is 1,073,741,824) which is >1e9? Actually 2^30 = 1,073,741,824 > 1e9. So if a_i ≤ 1e9, then its bits cannot exceed 2^29? Let's check: 2^29 = 536,870,912; 2^30 = 1,073,741,824 > 1,000,000,000. So the highest possible bit that can be set in a number ≤ 1e9 is 2^29 (since 2^29 < 1e9) or maybe 2^29 plus lower bits sum to ≤1e9. But 2^29 = 536,870,912, 2^30 = 1,073,741,824 > 1e9, so bit 30 (value 2^29) is actually 2^29, sorry indexing: bits from 0: 2^0=1, 2^1=2,... 2^29=536M, 2^30=1.07B. So numbers ≤1e9 can have bit 29 set, but not bit 30? Actually 2^30 is >1e9, so bit 30 (value 2^30) cannot be set because that would be at least 1,073,741,824 > 1e9. So the highest bit index possible is 29 (value 2^29) or possibly also bit 30? But 2^30 > 1e9, so cannot. So OR of numbers that are ≤1e9 cannot have bit 30 set because none of them have bit 30 set. But could OR produce a number that has bits set such that total value >1e9? Possibly yes, e.g., numbers 600,000,000 and 500,000,000? Let's check max: If we OR two numbers, each ≤1e9, the OR result could be as high as the bitwise OR of all possible bits up to 2^29. The maximum OR value if we combine all bits up to 2^29 would be sum_{i=0}^{29} 2^i = 2^30 - 1 = 1,073,741,823, which is >1e9. But can that OR result actually occur given that b_ij come from a consistent a? Since b_ij = a_i & a_j, AND reduces bits, so b_ij cannot have bits that are not present in both. So b_ij may be missing some bits that appear in a_i but not in a_j. But OR over j of b_ij will only collect bits that appear in at least one other a_j. If all bits of a_i are present in at least one other a_j, then OR of b_ij = a_i (since each bit present in a_i will appear in b_ij for some j where a_j also has that bit). Then a_i itself is ≤ 1e9. So OR result ≤ a_i ≤1e9. If a_i has some private bits, then OR result is a_i minus those bits, which is less than a_i, so ≤ a_i ≤1e9. So OR result cannot exceed the original a_i, which is ≤1e9. So OR result ≤1e9. But we don't know original a_i, but we know there exists some a_i ≤1e9. So OR result must also be ≤1e9 because it's a subset of bits of some a_i ≤1e9? Actually careful: OR_{j} b_ij = OR_{j} (a_i* & a_j*). This is a subset of a_i* (since each term is a_i* & a_j*). So OR result is ≤ a_i* bitwise, i.e., it's less than or equal to a_i* in numeric value (since bits are subset). Therefore, OR result ≤ a_i* ≤ 1e9. So indeed our constructed numbers are ≤1e9. So it's fine.
Thus the simple solution works.
But before finalizing, let's test with random consistent data to be sure.
We can quickly test with random a_i ≤ 1e9, compute b_ij, then compute OR per i, verify that it gives a_i' satisfying b_ij = a_i' & a_j'. Also verify that a_i' is ≤ original a_i, and that b_ij consistent. That seems plausible.
But there might be a nuance: For i, OR over j of b_ij uses all j ≠ i. However, is it possible that for some i, there are no j? Only n=1 case, we output 0. That's fine.
Thus the solution is trivial.
But wait, I recall that there is a Codeforces problem "Find a Number" maybe? Actually the problem is from Codeforces 376A? Or "Polycarpus and Bits"? I think it's CF #286? Let's search memory: Problem "AND Graph"? No. Actually this problem is known: Codeforces 429A? Not sure. But I recall a problem where you reconstruct numbers from pairwise AND, and the solution is to take OR of each row. Yes, that's a known solution.
Thus answer: just compute a_i = OR over j (b_ij for j != i). Print.
Now, we need to implement in C++.
Implementation steps:
Read integer n.
Initialize vector a(n, 0) or vector but within int range up to 1e9 (fits 32-bit).
For i from 0 to n-1:
for j from 0 to n-1:
read integer x.
if i != j:
a[i] |= x.
Print a[0..n-1] separated by spaces.
That's it.
But is there any scenario where the diagonal -1 might be read as -1 and we need to treat it specially? Since we skip i==j, we can ignore the -1.
Make sure to use 0-based indices.
Complexity O(n^2). Input up to 100, so fine.
But we must ensure using integer type that can hold up to bitwise OR of up to 1e9; int (32-bit) works.
Edge Cases: n=1; loop: for i=0, inner j from 0 to 0: read -1, but skip because i==j; a[0] remains 0, output 0.
Check with example 3: Works.
Thus answer is straightforward.
But I suspect there might be a catch: What about overflow? Not.
Therefore, final solution: compute row-wise OR.
Let's produce final answer.
We should also mention reasoning.
Now, produce final ChatGPT response with explanation and code in C++.
Make sure to handle reading quickly.
Implementation in C++:
cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n, 0);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
int x;
cin >> x;
if (i != j) {
a[i] |= x;
}
}
}
for (int i = 0; i < n; ++i) {
cout << a[i] << " \n"[i == n-1];
}
return 0;
}
That solves the problem.
But is there any guarantee that the OR yields numbers that satisfy b_ij exactly? Yes, as argued. Let's just double-check with a more complicated example: Suppose a = [3,5,6] (binary: 011, 101, 110). Compute b:
b12 = 3&5 = 1 (001)
b13 = 3&6 = 2 (010)
b23 = 5&6 = 4 (100)
Matrix:
-1 1 2
1 -1 4
2 4 -1
Now OR: a1' = 1|2 = 3 (011) => original a1 was 3, matches.
a2' = 1|4 = 5 (101)
a3' = 2|4 = 6 (110)
Works.
Now suppose a = [7,1,2] (111, 001, 010). b12 = 1, b13=2, b23=0. OR: a1'=1|2=3 (011) not 7. Check: a1' & a2' = 3&1 = 1 which equals b12=1; a1'&a3' =3&2=2 matches b13=2; a2'&a3' =1&2=0 matches b23=0. So (3,1,2) is a valid solution, and numbers ≤1e9. So the OR method gave a valid solution, not necessarily the original, but okay. So the problem allows any solution.
Thus answer is correct.
Now we can finalize.
But wait: The problem statement says: "It is guaranteed that there is sequence a that satisfies the problem conditions." So yes.
Now, consider a potential pitfall: b_ij might be large, up to 10^9, but OR of many may exceed 2^31? But 2^31-1 is about 2.1e9, so still within 32-bit signed int? Actually 2^31-1 = 2147483647 > 1e9. So int can hold up to 2.147e9. So okay. Use int.
Now produce final answer.
But maybe there is a hidden nuance: The matrix entries are given as -1 for diagonal. When reading, we need to skip those. However, if we use signed int, -1 read, but we skip i!=j so we never OR -1. But if we accidentally OR -1 for off-diagonal? Not possible because off-diagonal entries are guaranteed to be non-negative. So fine.
Thus we can implement.
Now, let's prepare final solution explanation.
We'll provide:
Observation: For any i, a_i must contain all bits present in any b_ij, because b_ij = a_i & a_j implies bits set in b_ij are set in a_i.
So we can define a_i as the bitwise OR of all b_ij for j≠i.
Then show that this works: For any i,j, a_i & a_j = b_ij because any bit in b_ij is obviously set in both a_i and a_j; conversely, if a bit is set in both a_i and a_j, then it must be present in some b_ik and some b_jl? Actually we need to argue that it's present in b_ij, but we can argue using consistency: Since the matrix comes from some original a*, our a_i is a subset of the original a_i*, and any bit set in both a_i and a_j implies it was set in both original a_i* and a_j*, thus in b_ij; and our a_i contains exactly those bits of a_i* that appear in at least one b_ik, i.e., bits that are shared. Therefore a_i & a_j = b_ij.
But simpler: One can directly verify that this construction satisfies the condition: Because for any i,j, a_i & a_j = (OR_k b_ik) & (OR_l b_jl). But it's not immediate; we rely on consistency. However we can prove that b_ij is exactly the AND of these ORs because b_ij = a_i & a_j where a_i is defined as OR of b_ik; we need to show that for any consistent b, the defined a_i yields a_i & a_j = b_ij. That can be shown: b_ij ≤ a_i and a_j (since b_ij is included in OR). So b_ij ≤ a_i & a_j. Conversely, suppose a bit p is set in a_i & a_j. Then p is set in a_i = OR_k b_ik, so exists k such that b_ik has p; similarly exists l such that b_jl has p. Since b_ik = a_i* & a_k*, and b_jl = a_j* & a_l*, we deduce that a_i* and a_j* have p. So b_ij = a_i* & a_j* has p. Therefore a_i & a_j ≤ b_ij. So equality holds.
Thus solution is valid.
Now we output.
Now, I will produce final answer with code and explanation. We can reconstruct the sequence by setting each (a_i) to the bitwise OR of all non‑diagonal entries in the (i)-th row of matrix (b).
This works because:
Every bit appearing in any (b_{ij}) must be present in (a_i) (since (b_{ij}=a_i ,&, a_j)).
Therefore the OR of row (i) gives a number that contains exactly the bits of the original (a_i) that are shared with at least one other number.
For any pair ((i,j)), the AND of these reconstructed numbers equals (b_{ij}):
(b_{ij}) is obviously a subset of both ORs, so (b_{ij} \le a_i ,&, a_j).
If a bit is set in both (a_i) and (a_j), then the original numbers had that bit, hence it appears in (b_{ij}) (by consistency of the input). Thus (a_i ,&, a_j \le b_{ij}).
The resulting numbers never exceed (10^9) because they are subsets of bits of the original (guaranteed) sequence.
Time complexity: (O(n^2)).
Memory: (O(n)).
cpp
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n, 0);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
int x;
cin >> x;
if (i != j) {
a[i] |= x;
}
}
}
for (int i = 0; i < n; ++i) {
cout << a[i] << " \n"[i == n - 1];
}
return 0;
}