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++) {
for (int l = -1; l <= 1; l++) {
sum += a[(i+k+m)%m][(j+l+n)%n];
}
}
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("");
}*/
}
}
aW1wb3J0IGphdmEudXRpbC4qOwoKY2xhc3MgTWFpbgp7CiAgICBwdWJsaWMgc3RhdGljIHZvaWQgbGlmZShpbnQgYVtdW10sIGludCBiW11bXSwgaW50IG0sIGludCBuKSB7CiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBtOyBpKyspIHsKICAgICAgICAgICAgZm9yIChpbnQgaiA9IDA7IGogPCBuOyBqKyspIHsKICAgICAgICAgICAgICAgIGludCBzdW0gPSAwOwogICAgICAgICAgICAgICAgZm9yIChpbnQgayA9IC0xOyBrIDw9IDE7IGsrKykgewogICAgICAgICAgICAgICAgICAgIGZvciAoaW50IGwgPSAtMTsgbCA8PSAxOyBsKyspIHsKICAgICAgICAgICAgICAgICAgICAgICAgc3VtICs9IGFbKGkrayttKSVtXVsoaitsK24pJW5dOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGlmIChzdW0gPT0gMyB8fCAoc3VtID09IDQgJiYgYVtpXVtqXSA9PSAxKSkgewogICAgICAgICAgICAgICAgICAgIGJbaV1bal0gPSAxOwogICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICBiW2ldW2pdID0gMDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZyBhcmdzW10pCiAgICB7CiAgICAgICAgaW50IHNpemUgPSA1MDA7CiAgICAgICAgaW50IGFbXVtdLCBiW11bXTsKICAgICAgICBhID0gbmV3IGludFtzaXplXVtzaXplXTsKICAgICAgICBiID0gbmV3IGludFtzaXplXVtzaXplXTsKICAgICAgICBSYW5kb20gciA9IG5ldyBSYW5kb20oKTsKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IHNpemU7IGkrKykgewogICAgICAgICAgICBmb3IgKGludCBqID0gMDsgaiA8IHNpemU7IGorKykgewogICAgICAgICAgICAgICAgYVtpXVtqXSA9IHIubmV4dEludCgyKTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBmb3IgKGludCBuID0gMDsgbiA8IDUwOyBuKyspIHsKICAgICAgICAgICAgbGlmZShhLCBiLCBzaXplLCBzaXplKTsKICAgICAgICAgICAgaW50IHRtcFtdW107CiAgICAgICAgICAgIHRtcCA9IGE7IGEgPSBiOyBiID0gdG1wOwogICAgICAgIH0KICAgICAgICAvKgogICAgICAgIGZvciAoaW50W10geCA6IGEpIHsKICAgICAgICAgICAgZm9yIChpbnQgbiA6IHgpIHsKICAgICAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRmKCIlZCwiLCBuKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oIiIpOwogICAgICAgIH0qLwogICAgfQp9