Problem: problem

JOI, who came to Australia for a trip, enjoyed sightseeing in various places and finally returned to Japan. Now, JOI is in the town with the international airport where the return flight departs. The town is divided into north, south, east and west, and each section has roads, souvenir shops, houses, and an international airport. JOI starts from the northwestern section and heads for the international airport in the southeastern section.

JOI can move from the current parcel to an adjacent parcel, but cannot enter a parcel with a house. Also, move only to the east or south section of the current section in order to make it in time for the flight. However, there is some time to spare, so you can move to the north or west section of the section you are up to K times.

When JOI enters the area where the souvenir shop is located, he buys souvenirs for his Japanese friends. JOI did a lot of research on the souvenir shops, so he knows which souvenir shop to buy and how many souvenirs to buy. Create a program to find the maximum number of souvenirs you can buy.

However, the time to buy souvenirs can be ignored, and if you visit the same souvenir shop more than once, you will only buy souvenirs the first time you visit.





Example

Input

5 4 2
...#
.#.#
.#73
8##.
....


Output

11

Solution (in JAVA):

Solution:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

public class Main {
	
	public static class Place{
		int x, y;

		public Place(int x, int y) {
			super();
			this.x = x;
			this.y = y;
		}

		@Override
		public int hashCode() {
			final int prime = 97;
			int result = 1;
			result = prime * result + x;
			result = prime * result + y;
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			Place other = (Place) obj;
			if (x != other.x)
				return false;
			if (y != other.y)
				return false;
			return true;
		}
	}
	
	public static class State{
		int x, y, k;
		Set<Place> visited;
		
		public State(int x, int y, int k, Set<Place> visited) {
			super();
			this.x = x;
			this.y = y;
			this.k = k;
			this.visited = visited;
		}

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + k;
			result = prime * result
					+ ((visited == null) ? 0 : visited.hashCode());
			result = prime * result + x;
			result = prime * result + y;
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			State other = (State) obj;
			if (k != other.k)
				return false;
			if (visited == null) {
				if (other.visited != null)
					return false;
			} else if (!visited.equals(other.visited))
				return false;
			if (x != other.x)
				return false;
			if (y != other.y)
				return false;
			return true;
		}
	}
	
	public static int[] dx = new int[]{1, 0, -1,  0};
	public static int[] dy = new int[]{0, 1,  0, -1};
	
	public static int H;
	public static int W;
	public static Map<State, Integer> memo;
	public static char[][] field;
	
	public static int solve(int cur_x, int cur_y, int k, Set<Place> visited){
		State state = new State(cur_x, cur_y, k, visited);
		
		if(memo.get(state) != null){
			return memo.get(state);
		}
		
		int res = Integer.MIN_VALUE;
		
		if(cur_x == W - 1 && cur_y == H - 1){
			res = 0;
		}
		
		for(int d = 0; d < 4; d++){
			int xx = cur_x + dx[d];
			int yy = cur_y + dy[d];
			int kk = k - (d >= 2 ? 1 : 0);
			
			if(xx < 0 || xx >= W || yy < 0 || yy >= H || kk < 0 || field[yy][xx] == '#'){
				continue;
			}
			
			HashSet<Place> vv = new HashSet<Place>();
			for(Place place : visited){
				if(Math.max(xx - place.x, 0) + Math.max(yy - place.y, 0) <= kk){
					vv.add(place);
				}
			}
			
			if(field[yy][xx] != '.'){
				vv.add(new Place(xx, yy));
			}
			
			int val = (field[yy][xx] != '.' ? field[yy][xx] - '0' : 0);
			for(Place place : visited){
				if(place.x == xx && place.y == yy){
					val = 0;
				}
			}
			
			res = Math.max(res, solve(xx, yy, kk, vv) + val);
		}
		
		memo.put(state, res);
		return res;
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		H = sc.nextInt();
		W = sc.nextInt();
		final int K = sc.nextInt();
		
		field = new char[H][W];
		
		for(int i = 0; i < H; i++){
			char[] input = sc.next().toCharArray();
			
			for(int j = 0; j < W; j++){
				field[i][j] = input[j];
			}
		}
		
		memo = new HashMap<State, Integer>();
		
		System.out.println(solve(0,0,K,new HashSet<Place>()));
	}

}