#Lutece3310. Arrange the Desktop
Arrange the Desktop
Description
Charming is a slightly obsessive college student. One day, a bug appears on his phone and the icons on his mobile desktop are out of order. Help Charming arrange the mobile desktop in order!
Specifically, the mobile desktop is a grid of , with rows from top to bottom, columns from left to right. Each icon occupies a grid in the grid diagram.
In order means that all icons are arranged row by row, starting from the first row. That is, for any icon, its position in the grid satisfies: , and is the number of icons on the desktop.
When an icon moves from to , it can be seen as moving distance.
Now Charming wants to know, at least how far does it have to move to put the table in order?
Note: only one icon can be placed in the same square. You can consider that icons can only be moved to the position where there is no icon.
Input
The first line contains three integers , and (), meaning that Charming's mobile desktop has rows and columns with icons.
Each of the next lines contains a string with a length of . If the character in the line and column is , it means that there is an icon in the row and column of the mobile desktop.
Output
Print an integer, indicating the least distance Charming needs to move to arrange the mobile desktop in order.
Samples
1 1 0
0
0
3 2 1
00
10
00
1
4 4 8
0110
0101
0001
1110
10
Note
In the third test case, you can move like this. It can be shown that is the least distance to arrange the mobile desktop in order.
Resources
电子科技大学第十三届 ACM 趣味程序设计竞赛