import random
import socket
import sys
from copy import deepcopy
my_hostnames = [ 'N551J' , 'F551C' ]
if socket.gethostname ( ) in my_hostnames:
sys.stdin = open( 'c1.in' )
sys.stdout = open( 'out.txt' , 'w' )
def read_str( ) :
return input( )
def read_str_list( ) :
return list( input( ) .split ( ) )
def read_int( ) :
return int ( input( ) )
def read_int_list( ) :
return list( map( int , input( ) .split ( ) ) )
def read_grid( n_rows) :
return [ read_str( ) for i in range( n_rows) ]
def read_float( ) :
return float ( input( ) )
def read_float_list( ) :
return list( map( float , input( ) .split ( ) ) )
def read_input( ) :
R, C = read_int_list( )
a = [ read_int_list( ) for i in range( R) ]
return R, C, a
def random_input( ) :
M = 10 ** 6
MR, MC = 5 , 5
MR, MC = 1 , 1
R, C = random.randint ( 1 , MR) , random.randint ( 1 , MC)
a = [ [ random.randint ( 1 , M) for j in range( C) ] for i in range( R) ]
return R, C, a
def judge( R, C, a) :
di = [ 0 , 0 , - 1 , 1 ]
dj = [ - 1 , 1 , 0 , 0 ]
res = 0
updated = True
while updated:
updated = [ ]
for i in range( R) :
for j in range( C) :
res += a[ i] [ j]
for i in range( R) :
for j in range( C) :
if a[ i] [ j] == 0 :
continue
s = 0
m = 0
for k in range( 4 ) :
ii = i + di[ k]
jj = j + dj[ k]
while 0 <= ii < R and 0 <= jj < C and a[ ii] [ jj] == 0 :
ii += di[ k]
jj += dj[ k]
if 0 <= ii < R and 0 <= jj < C and a[ ii] [ jj] > 0 :
m += 1
s += a[ ii] [ jj]
if m * a[ i] [ j] < s:
updated.append ( ( i, j) )
for ( i, j) in updated:
a[ i] [ j] = 0
return res
def solve_it( R, C, a) :
neighbor = [ [ [ ( - 1 , - 1 ) for k in range( 4 ) ] for j in range( C) ] for i in range( R) ]
for i in range( R) :
last = None
for j in range( C) :
if a[ i] [ j] :
if last is not None:
neighbor[ i] [ j] [ 0 ] = ( i, last)
neighbor[ i] [ last] [ 1 ] = ( i, j)
last = j
for j in range( C) :
last = None
for i in range( R) :
if a[ i] [ j] :
if last is not None:
neighbor[ i] [ j] [ 2 ] = ( last, j)
neighbor[ last] [ j] [ 3 ] = ( i, j)
last = i
total = 0
look = set( )
for i in range( R) :
for j in range( C) :
if a[ i] [ j] == 0 :
continue
total += a[ i] [ j]
look.add ( ( i, j) )
res = 0
eliminated = True
while eliminated:
eliminated = set( )
next_look = set( )
res += total
for ( i, j) in look:
s = 0
m = 0
for k in range( 4 ) :
ii, jj = neighbor[ i] [ j] [ k]
if 0 <= ii < R and 0 <= jj < C:
m += 1
s += a[ ii] [ jj]
if m * a[ i] [ j] < s:
eliminated.add ( ( i, j) )
# print('look:', look)
# print('eliminated:', eliminated)
for ( i, j) in eliminated:
total -= a[ i] [ j]
for k in range( 4 ) :
if neighbor[ i] [ j] [ k] != ( - 1 , - 1 ) :
if neighbor[ i] [ j] [ k] not in eliminated:
next_look.add ( neighbor[ i] [ j] [ k] )
for ( i, j) in eliminated:
for k in [ 0 , 2 ] :
ii, jj = neighbor[ i] [ j] [ k]
iii, jjj = neighbor[ i] [ j] [ k + 1 ]
if ( ii, jj) != ( - 1 , - 1 ) :
neighbor[ ii] [ jj] [ k + 1 ] = iii, jjj
if ( iii, jjj) != ( - 1 , - 1 ) :
neighbor[ iii] [ jjj] [ k] = ii, jj
look = next_look
return res
def debug( ) :
random.seed = 1
n_steps = 10000
for i in range( n_steps) :
R, C, a = random_input( )
aa = deepcopy( a)
res = solve_it( R, C, a)
expected = judge( R, C, aa)
if res != expected:
print( 1 )
print( R, C)
for i in range( R) :
print( * a[ i] )
def main( ) :
n_cases = int ( input( ) )
for test_case in range( 1 , n_cases + 1 ) :
print( test_case, file= sys.stderr , end= ' ' )
R, C, a = read_input( )
res = solve_it( R, C, a)
print( 'Case #' + str( test_case) + ':' , res)
print( file= sys.stderr )
if __name__ == '__main__' :
# debug()
main( )
aW1wb3J0IHJhbmRvbQppbXBvcnQgc29ja2V0CmltcG9ydCBzeXMKZnJvbSBjb3B5IGltcG9ydCBkZWVwY29weQoKbXlfaG9zdG5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BpIGluIHJhbmdlKFIpXQogICAgcmV0dXJuIFIsIEMsIGEKCgpkZWYganVkZ2UoUiwgQywgYSk6CiAgICBkaSA9IFswLCAwLCAtMSwgMV0KICAgIGRqID0gWy0xLCAxLCAwLCAwXQogICAgcmVzID0gMAogICAgdXBkYXRlZCA9IFRydWUKICAgIHdoaWxlIHVwZGF0ZWQ6CgogICAgICAgIHVwZGF0ZWQgPSBbXQogICAgICAgIGZvciBpIGluIHJhbmdlKFIpOgogICAgICAgICAgICBmb3IgaiBpbiByYW5nZShDKToKICAgICAgICAgICAgICAgIHJlcyArPSBhW2ldW2pdCgogICAgICAgIGZvciBpIGluIHJhbmdlKFIpOgogICAgICAgICAgICBmb3IgaiBpbiByYW5nZShDKToKICAgICAgICAgICAgICAgIGlmIGFbaV1bal0gPT0gMDoKICAgICAgICAgICAgICAgICAgICBjb250aW51ZQogICAgICAgICAgICAgICAgcyA9IDAKICAgICAgICAgICAgICAgIG0gPSAwCiAgICAgICAgICAgICAgICBmb3IgayBpbiByYW5nZSg0KToKICAgICAgICAgICAgICAgICAgICBpaSA9IGkgKyBkaVtrXQogICAgICAgICAgICAgICAgICAgIGpqID0gaiArIGRqW2tdCiAgICAgICAgICAgICAgICAgICAgd2hpbGUgMCA8PSBpaSA8IFIgYW5kIDAgPD0gamogPCBDIGFuZCBhW2lpXVtqal0gPT0gMDoKICAgICAgICAgICAgICAgICAgICAgICAgaWkgKz0gZGlba10KICAgICAgICAgICAgICAgICAgICAgICAgamogKz0gZGpba10KICAgICAgICAgICAgICAgICAgICBpZiAwIDw9IGlpIDwgUiBhbmQgMCA8PSBqaiA8IEMgYW5kIGFbaWldW2pqXSA+IDA6CiAgICAgICAgICAgICAgICAgICAgICAgIG0gKz0gMQogICAgICAgICAgICAgICAgICAgICAgICBzICs9IGFbaWldW2pqXQogICAgICAgICAgICAgICAgaWYgbSAqIGFbaV1bal0gPCBzOgogICAgICAgICAgICAgICAgICAgIHVwZGF0ZWQuYXBwZW5kKChpLCBqKSkKICAgICAgICBmb3IgKGksIGopIGluIHVwZGF0ZWQ6CiAgICAgICAgICAgIGFbaV1bal0gPSAwCiAgICByZXR1cm4gcmVzCgoKZGVmIHNvbHZlX2l0KFIsIEMsIGEpOgogICAgbmVpZ2hib3IgPSBbW1soLTEsIC0xKSBmb3IgayBpbiByYW5nZSg0KV0gZm9yIGogaW4gcmFuZ2UoQyldIGZvciBpIGluIHJhbmdlKFIpXQogICAgZm9yIGkgaW4gcmFuZ2UoUik6CiAgICAgICAgbGFzdCA9IE5vbmUKICAgICAgICBmb3IgaiBpbiByYW5nZShDKToKICAgICAgICAgICAgaWYgYVtpXVtqXToKICAgICAgICAgICAgICAgIGlmIGxhc3QgaXMgbm90IE5vbmU6CiAgICAgICAgICAgICAgICAgICAgbmVpZ2hib3JbaV1bal1bMF0gPSAoaSwgbGFzdCkKICAgICAgICAgICAgICAgICAgICBuZWlnaGJvcltpXVtsYXN0XVsxXSA9IChpLCBqKQogICAgICAgICAgICAgICAgbGFzdCA9IGoKICAgIGZvciBqIGluIHJhbmdlKEMpOgogICAgICAgIGxhc3QgPSBOb25lCiAgICAgICAgZm9yIGkgaW4gcmFuZ2UoUik6CiAgICAgICAgICAgIGlmIGFbaV1bal06CiAgICAgICAgICAgICAgICBpZiBsYXN0IGlzIG5vdCBOb25lOgogICAgICAgICAgICAgICAgICAgIG5laWdoYm9yW2ldW2pdWzJdID0gKGxhc3QsIGopCiAgICAgICAgICAgICAgICAgICAgbmVpZ2hib3JbbGFzdF1bal1bM10gPSAoaSwgaikKICAgICAgICAgICAgICAgIGxhc3QgPSBpCiAgICB0b3RhbCA9IDAKICAgIGxvb2sgPSBzZXQoKQogICAgZm9yIGkgaW4gcmFuZ2UoUik6CiAgICAgICAgZm9yIGogaW4gcmFuZ2UoQyk6CiAgICAgICAgICAgIGlmIGFbaV1bal0gPT0gMDoKICAgICAgICAgICAgICAgIGNvbnRpbnVlCiAgICAgICAgICAgIHRvdGFsICs9IGFbaV1bal0KICAgICAgICAgICAgbG9vay5hZGQoKGksIGopKQoKICAgIHJlcyA9IDAKICAgIGVsaW1pbmF0ZWQgPSBUcnVlCiAgICB3aGlsZSBlbGltaW5hdGVkOgogICAgICAgIGVsaW1pbmF0ZWQgPSBzZXQoKQogICAgICAgIG5leHRfbG9vayA9IHNldCgpCiAgICAgICAgcmVzICs9IHRvdGFsCiAgICAgICAgZm9yIChpLCBqKSBpbiBsb29rOgogICAgICAgICAgICBzID0gMAogICAgICAgICAgICBtID0gMAogICAgICAgICAgICBmb3IgayBpbiByYW5nZSg0KToKICAgICAgICAgICAgICAgIGlpLCBqaiA9IG5laWdoYm9yW2ldW2pdW2tdCiAgICAgICAgICAgICAgICBpZiAwIDw9IGlpIDwgUiBhbmQgMCA8PSBqaiA8IEM6CiAgICAgICAgICAgICAgICAgICAgbSArPSAxCiAgICAgICAgICAgICAgICAgICAgcyArPSBhW2lpXVtqal0KICAgICAgICAgICAgaWYgbSAqIGFbaV1bal0gPCBzOgogICAgICAgICAgICAgICAgZWxpbWluYXRlZC5hZGQoKGksIGopKQoKICAgICAgICAjIHByaW50KCdsb29rOicsIGxvb2spCiAgICAgICAgIyBwcmludCgnZWxpbWluYXRlZDonLCBlbGltaW5hdGVkKQoKICAgICAgICBmb3IgKGksIGopIGluIGVsaW1pbmF0ZWQ6CiAgICAgICAgICAgIHRvdGFsIC09IGFbaV1bal0KCiAgICAgICAgICAgIGZvciBrIGluIHJhbmdlKDQpOgogICAgICAgICAgICAgICAgaWYgbmVpZ2hib3JbaV1bal1ba10gIT0gKC0xLCAtMSk6CiAgICAgICAgICAgICAgICAgICAgaWYgbmVpZ2hib3JbaV1bal1ba10gbm90IGluIGVsaW1pbmF0ZWQ6CiAgICAgICAgICAgICAgICAgICAgICAgIG5leHRfbG9vay5hZGQobmVpZ2hib3JbaV1bal1ba10pCgogICAgICAgIGZvciAoaSwgaikgaW4gZWxpbWluYXRlZDoKCiAgICAgICAgICAgIGZvciBrIGluIFswLCAyXToKICAgICAgICAgICAgICAgIGlpLCBqaiA9IG5laWdoYm9yW2ldW2pdW2tdCiAgICAgICAgICAgICAgICBpaWksIGpqaiA9IG5laWdoYm9yW2ldW2pdW2sgKyAxXQogICAgICAgICAgICAgICAgaWYgKGlpLCBqaikgIT0gKC0xLCAtMSk6CiAgICAgICAgICAgICAgICAgICAgbmVpZ2hib3JbaWldW2pqXVtrICsgMV0gPSBpaWksIGpqagogICAgICAgICAgICAgICAgaWYgKGlpaSwgampqKSAhPSAoLTEsIC0xKToKICAgICAgICAgICAgICAgICAgICBuZWlnaGJvcltpaWldW2pqal1ba10gPSBpaSwgamoKICAgICAgICBsb29rID0gbmV4dF9sb29rCiAgICByZXR1cm4gcmVzCgoKZGVmIGRlYnVnKCk6CiAgICByYW5kb20uc2VlZCA9IDEKICAgIG5fc3RlcHMgPSAxMDAwMAogICAgZm9yIGkgaW4gcmFuZ2Uobl9zdGVwcyk6CiAgICAgICAgUiwgQywgYSA9IHJhbmRvbV9pbnB1dCgpCiAgICAgICAgYWEgPSBkZWVwY29weShhKQogICAgICAgIHJlcyA9IHNvbHZlX2l0KFIsIEMsIGEpCiAgICAgICAgZXhwZWN0ZWQgPSBqdWRnZShSLCBDLCBhYSkKICAgICAgICBpZiByZXMgIT0gZXhwZWN0ZWQ6CiAgICAgICAgICAgIHByaW50KDEpCiAgICAgICAgICAgIHByaW50KFIsIEMpCiAgICAgICAgICAgIGZvciBpIGluIHJhbmdlKFIpOgogICAgICAgICAgICAgICAgcHJpbnQoKmFbaV0pCiAgICAgICAgYXNzZXJ0IHJlcyA9PSBleHBlY3RlZAoKCmRlZiBtYWluKCk6CiAgICBuX2Nhc2VzID0gaW50KGlucHV0KCkpCiAgICBmb3IgdGVzdF9jYXNlIGluIHJhbmdlKDEsIG5fY2FzZXMgKyAxKToKICAgICAgICBwcmludCh0ZXN0X2Nhc2UsIGZpbGU9c3lzLnN0ZGVyciwgZW5kPScgJykKICAgICAgICBSLCBDLCBhID0gcmVhZF9pbnB1dCgpCiAgICAgICAgcmVzID0gc29sdmVfaXQoUiwgQywgYSkKICAgICAgICBwcmludCgnQ2FzZSAjJyArIHN0cih0ZXN0X2Nhc2UpICsgJzonLCByZXMpCgogICAgcHJpbnQoZmlsZT1zeXMuc3RkZXJyKQoKCmlmIF9fbmFtZV9fID09ICdfX21haW5fXyc6CiAgICAjIGRlYnVnKCkKICAgIG1haW4oKQo=