Define the prefix sum arrays globally
public static int [ , ] prefix_sum_row = new int [ 50 , 51 ] ; public static int [ , ] prefix_sum_col = new int [ 51 , 50 ] ; public static bool is_valid ( int r , int c , int size , int [ , ] grid ) { int r_end = r + size , c_end = c + size ;