Restaurant Opening

아직 제출이 없습니다시간 제한1초메모리 제한1024 MB

문제

Image by wirestock on Freepik

It is said that the three most important factors for determining whether or not a business will be successful are location, location, and location. The Incredible Cooks Preparing Cuisine are opening a new restaurant in the International City Promoting Cooking, and they have hired you to find the optimal location for their restaurant.

You decide to model the city as a grid, with each grid square having a specified number of people living in it. The distance between two grid squares (r_1,c_1)(r\_1,c\_1) and (r_2,c_2)(r\_2,c\_2) is r_1r_2+c_1c_2.|r\_1-r\_2|+|c\_1-c\_2|. In order to visit the restaurant, each potential customer would incur a cost equal to the minimum distance from the grid square in which they live to the grid square containing the proposed location of the restaurant. The total cost for a given restaurant location is defined as the sum of the costs of everyone living in the city to visit the restaurant.

Given the current city layout, compute the minimum total cost if the Incredible Cooks Preparing Cuisine select their next restaurant location optimally.

입력

The first line of input contains two integers, nn and mm (1<n,m501 < n,m \le 50), where nn is the number of rows in the city grid and mm is the number of columns.

Each of the next nn lines contains mm integers g_ijg\_{ij} (0g_ij500\le g\_{ij}\le 50), which specifies the number of people living in the grid square at row ii, column jj.

출력

Output a single integer, which is the total cost if the restaurant is selected optimally.