def parse(inFile):
[R,C,D] = inFile.getInts()
return [inFile.readline() for r in xrange(R)]
def solve(data):
R = len(data)
C = len(data[0])
sums=[[(0,0,0) for i in xrange(C+1)] for j in xrange(R+1)]
for i in xrange(R):
for j in xrange(C):
w = int(data[i][j])
sums[i+1][j+1] = (2 * w * i, 2 * w * j, w)
for j in xrange(C):
sums[i+1][j+1] = tuple(sums[i+1][j][k] + sums[i+1][j+1][k] for k in xrange(3))
for j in xrange(C,0,-1):
sums[i+1][j] = tuple(sums[i][j][k] + sums[i+1][j][k] for k in xrange(3))
for dim in xrange(min(R,C),2,-1):
for i in xrange(R+1-dim):
for j in xrange(C+1-dim):
ins = [(i,j),(i+dim,j+dim)]
outs = [(i+dim,j),(i,j+dim)]
blade = tuple(sum([sums[i[0]][i[1]][k] for i in ins])-sum([sums[o[0]][o[1]][k] for o in outs]) for k in xrange(3))
for pti in [i, i + dim - 1]:
for ptj in [j, j + dim - 1]:
w = int(data[pti][ptj])
blade = (blade[0] - 2 * w * pti, blade[1] - 2 * w * ptj, blade[2] - w)
if (blade == (blade[2]*(2*i+dim-1),blade[2]*(2*j+dim-1),blade[2])):
return dim
return "IMPOSSIBLE"
if __name__ == "__main__":
from GCJ import GCJ
GCJ(parse, solve, "/Users/lpebody/gcj/2011_round2/", "b").run()
ZGVmIHBhcnNlKGluRmlsZSk6CiAgICBbUixDLERdID0gaW5GaWxlLmdldEludHMoKQogICAgcmV0dXJuIFtpbkZpbGUucmVhZGxpbmUoKSBmb3IgciBpbiB4cmFuZ2UoUildCgpkZWYgc29sdmUoZGF0YSk6CiAgICBSID0gbGVuKGRhdGEpCiAgICBDID0gbGVuKGRhdGFbMF0pCiAgICBzdW1zPVtbKDAsMCwwKSBmb3IgaSBpbiB4cmFuZ2UoQysxKV0gZm9yIGogaW4geHJhbmdlKFIrMSldCiAgICBmb3IgaSBpbiB4cmFuZ2UoUik6CiAgICAgICAgZm9yIGogaW4geHJhbmdlKEMpOgogICAgICAgICAgICB3ID0gaW50KGRhdGFbaV1bal0pCiAgICAgICAgICAgIHN1bXNbaSsxXVtqKzFdID0gKDIgKiB3ICogaSwgMiAqIHcgKiBqLCB3KQogICAgICAgIGZvciBqIGluIHhyYW5nZShDKToKICAgICAgICAgICAgc3Vtc1tpKzFdW2orMV0gPSB0dXBsZShzdW1zW2krMV1bal1ba10gKyBzdW1zW2krMV1baisxXVtrXSBmb3IgayBpbiB4cmFuZ2UoMykpCiAgICAgICAgZm9yIGogaW4geHJhbmdlKEMsMCwtMSk6CiAgICAgICAgICAgIHN1bXNbaSsxXVtqXSA9IHR1cGxlKHN1bXNbaV1bal1ba10gKyBzdW1zW2krMV1bal1ba10gZm9yIGsgaW4geHJhbmdlKDMpKQogICAgZm9yIGRpbSBpbiB4cmFuZ2UobWluKFIsQyksMiwtMSk6CiAgICAgICAgZm9yIGkgaW4geHJhbmdlKFIrMS1kaW0pOgogICAgICAgICAgICBmb3IgaiBpbiB4cmFuZ2UoQysxLWRpbSk6CiAgICAgICAgICAgICAgICBpbnMgPSBbKGksaiksKGkrZGltLGorZGltKV0KICAgICAgICAgICAgICAgIG91dHMgPSBbKGkrZGltLGopLChpLGorZGltKV0KICAgICAgICAgICAgICAgIGJsYWRlID0gdHVwbGUoc3VtKFtzdW1zW2lbMF1dW2lbMV1dW2tdIGZvciBpIGluIGluc10pLXN1bShbc3Vtc1tvWzBdXVtvWzFdXVtrXSBmb3IgbyBpbiBvdXRzXSkgZm9yIGsgaW4geHJhbmdlKDMpKQogICAgICAgICAgICAgICAgZm9yIHB0aSBpbiBbaSwgaSArIGRpbSAtIDFdOgogICAgICAgICAgICAgICAgICAgIGZvciBwdGogaW4gW2osIGogKyBkaW0gLSAxXToKICAgICAgICAgICAgICAgICAgICAgICAgdyA9IGludChkYXRhW3B0aV1bcHRqXSkKICAgICAgICAgICAgICAgICAgICAgICAgYmxhZGUgPSAoYmxhZGVbMF0gLSAyICogdyAqIHB0aSwgYmxhZGVbMV0gLSAyICogdyAqIHB0aiwgYmxhZGVbMl0gLSB3KQogICAgICAgICAgICAgICAgaWYgKGJsYWRlID09IChibGFkZVsyXSooMippK2RpbS0xKSxibGFkZVsyXSooMipqK2RpbS0xKSxibGFkZVsyXSkpOgogICAgICAgICAgICAgICAgICAgIHJldHVybiBkaW0KICAgIHJldHVybiAiSU1QT1NTSUJMRSIKCmlmIF9fbmFtZV9fID09ICJfX21haW5fXyI6CiAgICBmcm9tIEdDSiBpbXBvcnQgR0NKCiAgICBHQ0oocGFyc2UsIHNvbHZlLCAiL1VzZXJzL2xwZWJvZHkvZ2NqLzIwMTFfcm91bmQyLyIsICJiIikucnVuKCkKCiAgICAgICAgICAgIAo=