SPOJ problem TGRID solution