Variable visited is used to keep track of all the visited positions Variable dp is used to store maximum sum till current position
static int dp [ ] [ ] = new int [ N ] [ N ] ; static int visited [ ] [ ] = new int [ N ] [ N ] ;