Programming question for a new approach
Construct a graph in which the vertices are all cells not marked 'b', and in which two vertices are connected if they are adjacent in the matrix. Use BFS from the 'a' vertex to go over all vertices in order of distance from 'a', until you find the first 'c' vertex. The overall complexity is $O(MN)$.