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