Problem:
Score : 600 points

Problem Statement

There is a board with N rows and M columns.
The information of this board is represented by N strings S_1,S_2,\ldots,S_N.
Specifically, the state of the square at the i-th row from the top and the j-th column from the left is represented as follows:

S_{i,j}=. : the square is empty.

S_{i,j}=# : an obstacle is placed on the square.

S_{i,j}=o : a piece is placed on the square.

Yosupo repeats the following operation:

Choose a piece and move it to its right adjecent square or its down adjacent square.
Moving a piece to squares with another piece or an obstacle is prohibited.
Moving a piece out of the board is also prohibited.

Yosupo wants to perform the operation as many times as possible.
Find the maximum possible number of operations.

Constraints

1 \leq N \leq 50

1 \leq M \leq 50

S_i is a string of length M consisting of ., # and o.

1 \leq ( the number of pieces )\leq 100.
In other words, the number of pairs (i, j) that satisfy S_{i,j}=o is between 1 and 100, both inclusive.

Input

Input is given from Standard Input in the following format:

N M
S_1
S_2
\vdots
S_N

Output

Print the maximum possible number of operations in a line.

Sample Input 1

3 3
o..
...
o.#

Sample Output 1

4

Yosupo can perform operations 4 times as follows:

o.. .o. ..o ... ...
... -> ... -> ... -> ..o -> ..o
o.# o.# o.# o.# .o#

Sample Input 2

9 10
.#....o#..
.#..#..##o
.....#o.##
.###.#o..o
#.#...##.#
..#..#.###
#o.....#..
....###..o
o.......o#

Sample Output 2

24

Solution in Go (1.14.1) (judge verdict: Accepted)

package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
)

func main() {
	N, M := ReadInt(), ReadInt()
	grid := make([]string, N)
	for y := 0; y < N; y++ {
		grid[y] = ReadString()
	}

	sources := 0
	for y := 0; y < N; y++ {
		for x := 0; x < M; x++ {
			if grid[y][x] == 'o' {
				sources++
			}
		}
	}

	s, t := 0, 1+sources+N*M
	adjL = make([][]*Edge, 2+sources+N*M)

	toID := func(x, y int) int { return 1 + sources + x + y*M }
	sourceNum := 0

	var dx = []int{1, 0}
	var dy = []int{0, 1}

	for y := 0; y < N; y++ {
		for x := 0; x < M; x++ {
			if grid[y][x] != '#' {
				for i := 0; i < 2; i++ {
					nx, ny := x+dx[i], y+dy[i]
					if 0 <= nx && nx < M && 0 <= ny && ny < N && grid[ny][nx] != '#' {
						AddEdge(toID(x, y), toID(nx, ny), 10000, -1)
					}
				}
				AddEdge(toID(x, y), t, 1, 0)
			}
			if grid[y][x] == 'o' {
				sourceNum++
				AddEdge(sourceNum, toID(x, y), 1, 0)
			}
		}
	}

	for i := 1; i <= sourceNum; i++ {
		AddEdge(s, i, 1, 0)
	}
	fmt.Println(-MinCostFlow(s, t, sources))
}

const Inf = 1 << 60

type Edge struct {
	To, Cap, Cost, Rev int
}

var adjL [][]*Edge

// 最小費用流 O(F|E|log|V|)
// sからtへの流量fの最小費用流を求める。流せない場合は-1
func MinCostFlow(s, t, f int) int {
	V := len(adjL)
	res := 0
	h, prevv, preve := make([]int, V), make([]int, V), make([]int, V)
	for f > 0 {
		q := Heapq{}
		dist := make([]int, V)
		for i := 0; i < V; i++ {
			dist[i] = Inf
		}
		dist[s] = 0
		q.push(State{Dist: 0, Node: s})
		for len(q) > 0 {
			now := q.pop()
			v := now.Node
			if dist[v] < now.Dist {
				continue
			}
			for i, edge := range adjL[v] {
				if edge.Cap > 0 && dist[edge.To] > dist[v]+edge.Cost+h[v]-h[edge.To] {
					dist[edge.To] = dist[v] + edge.Cost + h[v] - h[edge.To]
					prevv[edge.To] = v
					preve[edge.To] = i
					q.push(State{Dist: dist[edge.To], Node: edge.To})
				}
			}
		}
		if dist[t] == Inf {
			return -1
		}
		for v := 0; v < V; v++ {
			h[v] += dist[v]
		}
		d := f
		for v := t; v != s; v = prevv[v] {
			d = Min(d, adjL[prevv[v]][preve[v]].Cap)
		}
		f -= d
		res += d * h[t]
		for v := t; v != s; v = prevv[v] {
			edge := adjL[prevv[v]][preve[v]]
			edge.Cap -= d
			adjL[v][edge.Rev].Cap += d
		}
	}
	return res
}

func AddEdge(from, to, cap, cost int) {
	adjL[from] = append(adjL[from], &Edge{
		To:   to,
		Cap:  cap,
		Cost: cost,
		Rev:  len(adjL[to]),
	})
	adjL[to] = append(adjL[to], &Edge{
		To:   from,
		Cap:  0,
		Cost: -cost,
		Rev:  len(adjL[from]) - 1,
	})
}

func Min(xs ...int) int {
	min := xs[0]
	for _, x := range xs[1:] {
		if min > x {
			min = x
		}
	}
	return min
}

type State struct {
	Dist, Node int
}
type Heapq []State

func (q Heapq) Less(i, j int) bool    { return q[i].Dist < q[j].Dist }
func (q Heapq) Len() int              { return len(q) }
func (q Heapq) Swap(i, j int)         { q[i], q[j] = q[j], q[i] }
func (q *Heapq) Push(x interface{})   { *q = append(*q, x.(State)) }
func (q *Heapq) Pop() (x interface{}) { *q, x = (*q)[:len(*q)-1], (*q)[len(*q)-1]; return }
func (q *Heapq) push(v State)         { heap.Push(q, v) }
func (q *Heapq) pop() State           { return heap.Pop(q).(State) }

var reader = bufio.NewReader(os.Stdin)

func Scan(a ...interface{}) {
	if _, err := fmt.Fscan(reader, a...); err != nil {
		panic(err)
	}
}
func ReadInt() (i int)       { Scan(&i); return }
func ReadString() (s string) { Scan(&s); return }
func ReadInts(n int) (a []int) {
	for i := 0; i < n; i++ {
		a = append(a, ReadInt())
	}
	return
}