import java.util.*;
class Main
{
public static void life(int a[][], int b[][], int m, int n) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int sum = 0;
for (int k = -1; k <= 1; k++) {
int I = i + k;
if (I == -1) { I = m - 1; } else if (I == m) { I = 0; }
for (int l = -1; l <= 1; l++) {
int J = j + l;
if (J == -1) { J = n - 1; } else if (J == n) { J = 0; }
sum += a[I][J];
}
}
if (sum == 3 || (sum == 4 && a[i][j] == 1)) {
b[i][j] = 1;
} else {
b[i][j] = 0;
}
}
}
}
public static void main
(String args
[]) {
int size = 500;
int a[][], b[][];
a = new int[size][size];
b = new int[size][size];
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
a[i][j] = r.nextInt(2);
}
}
for (int n = 0; n < 50; n++) {
life(a, b, size, size);
int tmp[][];
tmp = a; a = b; b = tmp;
}
/*
for (int[] x : a) {
for (int n : x) {
System.out.printf("%d,", n);
}
System.out.println("");
}*/
}
}
aW1wb3J0IGphdmEudXRpbC4qOwoKY2xhc3MgTWFpbgp7CiAgICBwdWJsaWMgc3RhdGljIHZvaWQgbGlmZShpbnQgYVtdW10sIGludCBiW11bXSwgaW50IG0sIGludCBuKSB7CiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBtOyBpKyspIHsKICAgICAgICAgICAgZm9yIChpbnQgaiA9IDA7IGogPCBuOyBqKyspIHsKICAgICAgICAgICAgICAgIGludCBzdW0gPSAwOwogICAgICAgICAgICAgICAgZm9yIChpbnQgayA9IC0xOyBrIDw9IDE7IGsrKykgewogICAgICAgICAgICAgICAgICAgIGludCBJID0gaSArIGs7CiAgICAgICAgICAgICAgICAgICAgaWYgKEkgPT0gLTEpIHsgSSA9IG0gLSAxOyB9IGVsc2UgaWYgKEkgPT0gbSkgeyBJID0gMDsgfQogICAgICAgICAgICAgICAgICAgIGZvciAoaW50IGwgPSAtMTsgbCA8PSAxOyBsKyspIHsKICAgICAgICAgICAgICAgICAgICAgICAgaW50IEogPSBqICsgbDsKICAgICAgICAgICAgICAgICAgICAgICAgaWYgKEogPT0gLTEpIHsgSiA9IG4gLSAxOyB9IGVsc2UgaWYgKEogPT0gbikgeyBKID0gMDsgfQogICAgICAgICAgICAgICAgICAgICAgICBzdW0gKz0gYVtJXVtKXTsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAoc3VtID09IDMgfHwgKHN1bSA9PSA0ICYmIGFbaV1bal0gPT0gMSkpIHsKICAgICAgICAgICAgICAgICAgICBiW2ldW2pdID0gMTsKICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgYltpXVtqXSA9IDA7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CiAgICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmcgYXJnc1tdKQogICAgewogICAgICAgIGludCBzaXplID0gNTAwOwogICAgICAgIGludCBhW11bXSwgYltdW107CiAgICAgICAgYSA9IG5ldyBpbnRbc2l6ZV1bc2l6ZV07CiAgICAgICAgYiA9IG5ldyBpbnRbc2l6ZV1bc2l6ZV07CiAgICAgICAgUmFuZG9tIHIgPSBuZXcgUmFuZG9tKCk7CiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBzaXplOyBpKyspIHsKICAgICAgICAgICAgZm9yIChpbnQgaiA9IDA7IGogPCBzaXplOyBqKyspIHsKICAgICAgICAgICAgICAgIGFbaV1bal0gPSByLm5leHRJbnQoMik7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgZm9yIChpbnQgbiA9IDA7IG4gPCA1MDsgbisrKSB7CiAgICAgICAgICAgIGxpZmUoYSwgYiwgc2l6ZSwgc2l6ZSk7CiAgICAgICAgICAgIGludCB0bXBbXVtdOwogICAgICAgICAgICB0bXAgPSBhOyBhID0gYjsgYiA9IHRtcDsKICAgICAgICB9CiAgICAgICAgLyoKICAgICAgICBmb3IgKGludFtdIHggOiBhKSB7CiAgICAgICAgICAgIGZvciAoaW50IG4gOiB4KSB7CiAgICAgICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50ZigiJWQsIiwgbik7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCIiKTsKICAgICAgICB9Ki8KICAgIH0KfQ==